|
|
|
|
|
|
|
|
|
|
|
"use strict"; |
|
|
|
|
|
|
|
|
|
|
|
const path = require("path"); |
|
const spawn = require("cross-spawn"); |
|
const os = require("os"); |
|
const log = require("../shared/logging"); |
|
const packageJson = require("../../package.json"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function environment() { |
|
const cache = new Map(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isChildOfDirectory(parentPath, childPath) { |
|
return !path.relative(parentPath, childPath).startsWith(".."); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function execCommand(cmd, args) { |
|
const key = [cmd, ...args].join(" "); |
|
|
|
if (cache.has(key)) { |
|
return cache.get(key); |
|
} |
|
|
|
const process = spawn.sync(cmd, args, { encoding: "utf8" }); |
|
|
|
if (process.error) { |
|
throw process.error; |
|
} |
|
|
|
const result = process.stdout.trim(); |
|
|
|
cache.set(key, result); |
|
return result; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function normalizeVersionStr(versionStr) { |
|
return versionStr.startsWith("v") ? versionStr : `v${versionStr}`; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getBinVersion(bin) { |
|
const binArgs = ["--version"]; |
|
|
|
try { |
|
return normalizeVersionStr(execCommand(bin, binArgs)); |
|
} catch (e) { |
|
log.error(`Error finding ${bin} version running the command \`${bin} ${binArgs.join(" ")}\``); |
|
throw e; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getNpmPackageVersion(pkg, { global = false } = {}) { |
|
const npmBinArgs = ["bin", "-g"]; |
|
const npmLsArgs = ["ls", "--depth=0", "--json", pkg]; |
|
|
|
if (global) { |
|
npmLsArgs.push("-g"); |
|
} |
|
|
|
try { |
|
const parsedStdout = JSON.parse(execCommand("npm", npmLsArgs)); |
|
|
|
|
|
|
|
|
|
|
|
if (Object.keys(parsedStdout).length === 0 || !(parsedStdout.dependencies && parsedStdout.dependencies.eslint)) { |
|
return "Not found"; |
|
} |
|
|
|
const [, processBinPath] = process.argv; |
|
let npmBinPath; |
|
|
|
try { |
|
npmBinPath = execCommand("npm", npmBinArgs); |
|
} catch (e) { |
|
log.error(`Error finding npm binary path when running command \`npm ${npmBinArgs.join(" ")}\``); |
|
throw e; |
|
} |
|
|
|
const isGlobal = isChildOfDirectory(npmBinPath, processBinPath); |
|
let pkgVersion = parsedStdout.dependencies.eslint.version; |
|
|
|
if ((global && isGlobal) || (!global && !isGlobal)) { |
|
pkgVersion += " (Currently used)"; |
|
} |
|
|
|
return normalizeVersionStr(pkgVersion); |
|
} catch (e) { |
|
log.error(`Error finding ${pkg} version running the command \`npm ${npmLsArgs.join(" ")}\``); |
|
throw e; |
|
} |
|
} |
|
|
|
return [ |
|
"Environment Info:", |
|
"", |
|
`Node version: ${getBinVersion("node")}`, |
|
`npm version: ${getBinVersion("npm")}`, |
|
`Local ESLint version: ${getNpmPackageVersion("eslint", { global: false })}`, |
|
`Global ESLint version: ${getNpmPackageVersion("eslint", { global: true })}`, |
|
`Operating System: ${os.platform()} ${os.release()}` |
|
].join("\n"); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function version() { |
|
return `v${packageJson.version}`; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
module.exports = { |
|
environment, |
|
version |
|
}; |
|
|