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 os
= require("os");
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 * @throws {Error} As may be collected by `cross-spawn.sync`.
44 * @returns {string} The version returned by the command.
46 function execCommand(cmd
, args
) {
47 const key
= [cmd
, ...args
].join(" ");
50 return cache
.get(key
);
53 const process
= spawn
.sync(cmd
, args
, { encoding
: "utf8" });
59 const result
= process
.stdout
.trim();
61 cache
.set(key
, result
);
66 * Normalizes a version number.
67 * @param {string} versionStr The string to normalize.
68 * @returns {string} The normalized version number.
70 function normalizeVersionStr(versionStr
) {
71 return versionStr
.startsWith("v") ? versionStr
: `v${versionStr}`;
76 * @param {string} bin The bin to check.
77 * @throws {Error} As may be collected by `cross-spawn.sync`.
78 * @returns {string} The normalized version returned by the command.
80 function getBinVersion(bin
) {
81 const binArgs
= ["--version"];
84 return normalizeVersionStr(execCommand(bin
, binArgs
));
86 log
.error(`Error finding ${bin} version running the command \`${bin} ${binArgs.join(" ")}
\``);
92 * Gets installed npm package version.
93 * @param {string} pkg The package to check.
94 * @param {boolean} global Whether to check globally or not.
95 * @throws {Error} As may be collected by `cross-spawn.sync`.
96 * @returns {string} The normalized version returned by the command.
98 function getNpmPackageVersion(pkg
, { global
= false } = {}) {
99 const npmBinArgs
= ["bin", "-g"];
100 const npmLsArgs
= ["ls", "--depth=0", "--json", pkg
];
103 npmLsArgs
.push("-g");
107 const parsedStdout
= JSON
.parse(execCommand("npm", npmLsArgs
));
110 * Checking globally returns an empty JSON object, while local checks
111 * include the name and version of the local project.
113 if (Object
.keys(parsedStdout
).length
=== 0 || !(parsedStdout
.dependencies
&& parsedStdout
.dependencies
.eslint
)) {
117 const [, processBinPath
] = process
.argv
;
121 npmBinPath
= execCommand("npm", npmBinArgs
);
123 log
.error(`Error finding npm binary path when running command \`npm ${npmBinArgs.join(" ")}
\``);
127 const isGlobal
= isChildOfDirectory(npmBinPath
, processBinPath
);
128 let pkgVersion
= parsedStdout
.dependencies
.eslint
.version
;
130 if ((global
&& isGlobal
) || (!global
&& !isGlobal
)) {
131 pkgVersion
+= " (Currently used)";
134 return normalizeVersionStr(pkgVersion
);
136 log
.error(`Error finding ${pkg} version running the command \`npm ${npmLsArgs.join(" ")}
\``);
144 `Node version: ${getBinVersion("node")}`,
145 `npm version: ${getBinVersion("npm")}`,
146 `Local ESLint version: ${getNpmPackageVersion("eslint", { global: false })}`,
147 `Global ESLint version: ${getNpmPackageVersion("eslint", { global: true })}`,
148 `Operating System: ${os.platform()} ${os.release()}`
153 * Returns version of currently executing ESLint.
154 * @returns {string} The version from the currently executing ESLint's package.json.
157 return `v${packageJson.version}`;
160 //------------------------------------------------------------------------------
162 //------------------------------------------------------------------------------