2 * @fileoverview Utility to get information about the execution environment.
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const path
= require("path");
13 const spawn
= require("cross-spawn");
14 const { isEmpty
} = require("lodash");
15 const log
= require("../shared/logging");
16 const packageJson
= require("../../package.json");
18 //------------------------------------------------------------------------------
20 //------------------------------------------------------------------------------
23 * Generates and returns execution environment information.
24 * @returns {string} A string that contains execution environment information.
26 function environment() {
27 const cache
= new Map();
30 * Checks if a path is a child of a directory.
31 * @param {string} parentPath The parent path to check.
32 * @param {string} childPath The path to check.
33 * @returns {boolean} Whether or not the given path is a child of a directory.
35 function isChildOfDirectory(parentPath
, childPath
) {
36 return !path
.relative(parentPath
, childPath
).startsWith("..");
40 * Synchronously executes a shell command and formats the result.
41 * @param {string} cmd The command to execute.
42 * @param {Array} args The arguments to be executed with the command.
43 * @returns {string} The version returned by the command.
45 function execCommand(cmd
, args
) {
46 const key
= [cmd
, ...args
].join(" ");
49 return cache
.get(key
);
52 const process
= spawn
.sync(cmd
, args
, { encoding
: "utf8" });
58 const result
= process
.stdout
.trim();
60 cache
.set(key
, result
);
65 * Normalizes a version number.
66 * @param {string} versionStr The string to normalize.
67 * @returns {string} The normalized version number.
69 function normalizeVersionStr(versionStr
) {
70 return versionStr
.startsWith("v") ? versionStr
: `v${versionStr}`;
75 * @param {string} bin The bin to check.
76 * @returns {string} The normalized version returned by the command.
78 function getBinVersion(bin
) {
79 const binArgs
= ["--version"];
82 return normalizeVersionStr(execCommand(bin
, binArgs
));
84 log
.error(`Error finding ${bin} version running the command \`${bin} ${binArgs.join(" ")}
\``);
90 * Gets installed npm package version.
91 * @param {string} pkg The package to check.
92 * @param {boolean} global Whether to check globally or not.
93 * @returns {string} The normalized version returned by the command.
95 function getNpmPackageVersion(pkg
, { global
= false } = {}) {
96 const npmBinArgs
= ["bin", "-g"];
97 const npmLsArgs
= ["ls", "--depth=0", "--json", "eslint"];
100 npmLsArgs
.push("-g");
104 const parsedStdout
= JSON
.parse(execCommand("npm", npmLsArgs
));
107 * Checking globally returns an empty JSON object, while local checks
108 * include the name and version of the local project.
110 if (isEmpty(parsedStdout
) || !(parsedStdout
.dependencies
&& parsedStdout
.dependencies
.eslint
)) {
114 const [, processBinPath
] = process
.argv
;
118 npmBinPath
= execCommand("npm", npmBinArgs
);
120 log
.error(`Error finding npm binary path when running command \`npm ${npmBinArgs.join(" ")}
\``);
124 const isGlobal
= isChildOfDirectory(npmBinPath
, processBinPath
);
125 let pkgVersion
= parsedStdout
.dependencies
.eslint
.version
;
127 if ((global
&& isGlobal
) || (!global
&& !isGlobal
)) {
128 pkgVersion
+= " (Currently used)";
131 return normalizeVersionStr(pkgVersion
);
133 log
.error(`Error finding ${pkg} version running the command \`npm ${npmLsArgs.join(" ")}
\``);
141 `Node version: ${getBinVersion("node")}`,
142 `npm version: ${getBinVersion("npm")}`,
143 `Local ESLint version: ${getNpmPackageVersion("eslint", { global: false })}`,
144 `Global ESLint version: ${getNpmPackageVersion("eslint", { global: true })}`
149 * Returns version of currently executing ESLint.
150 * @returns {string} The version from the currently executing ESLint's package.json.
153 return `v${packageJson.version}`;
156 //------------------------------------------------------------------------------
158 //------------------------------------------------------------------------------