Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Adds new info flag (fixes #11958)
New: add test case fix: remove showing up browser information fix: show up local version of eslint fix: test fix: update local package info glob fix: explicitly return a promise fix: update local package glob tweaks fix: fixup async behavior fix: update test case fix: handle promise rejection fix: revert switch over to synchronous version WIP: spike info flag implementation
- Loading branch information
1 parent
f5e0cc4
commit 9806ebc
Showing
5 changed files
with
204 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/** | ||
* @fileoverview Log information for debugging purposes | ||
* @author Kai Cataldo | ||
*/ | ||
|
||
"use strict"; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const path = require("path"); | ||
const spawn = require("cross-spawn"); | ||
const { isEmpty } = require("lodash"); | ||
const logger = require("../shared/logging"); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Helpers | ||
//------------------------------------------------------------------------------ | ||
|
||
const STRIP_VERSION_REGEX = /v(\d)/u; | ||
|
||
/** | ||
* Checks if a given path is in a given directory. | ||
* @param {string} parentPath - The parent path to check. | ||
* @param {string} childPath - The path to check. | ||
* @returns {boolean} Whether or not the given path is in the given directory. | ||
*/ | ||
function isInDirectory(parentPath, childPath) { | ||
return !path.relative(parentPath, childPath).startsWith(".."); | ||
} | ||
|
||
/** | ||
* Synchronously executes a shell command and formats the result. | ||
* @param {string} cmd - The command to execute. | ||
* @param {Array} args - The arguments to be executed with the command. | ||
* @returns {string} The version returned by the command. | ||
*/ | ||
function execCommand(cmd, args) { | ||
const process = spawn.sync(cmd, args, { encoding: "utf8" }); | ||
|
||
if (process.error) { | ||
throw process.error; | ||
} | ||
|
||
return process.stdout.trim(); | ||
} | ||
|
||
/** | ||
* Normalizes a version number. | ||
* @param {string} version - The string to normalize. | ||
* @returns {string} The normalized version number. | ||
*/ | ||
function normalizeVersionNumber(version) { | ||
return version.replace(STRIP_VERSION_REGEX, "$1"); | ||
} | ||
|
||
/** | ||
* Gets bin version. | ||
* @param {string} bin - The bin to check. | ||
* @returns {string} The normalized version returned by the command. | ||
*/ | ||
function getBinVersion(bin) { | ||
const binArgs = ["--version"]; | ||
|
||
try { | ||
return execCommand(bin, binArgs); | ||
} catch (e) { | ||
logger.error(`Error finding ${bin} version running the command ${bin} ${binArgs.join(" ")}`); | ||
throw e; | ||
} | ||
} | ||
|
||
/** | ||
* Gets installed npm package version. | ||
* @param {string} pkg - The package to check. | ||
* @param {boolean} global - Whether to check globally or not. | ||
* @returns {string} The normalized version returned by the command. | ||
*/ | ||
function getNpmPackageVersion(pkg, { global = false } = {}) { | ||
const npmBinArgs = ["bin", "-g"]; | ||
const npmLsArgs = ["ls", "--depth=0", "--json", "eslint"]; | ||
|
||
if (global) { | ||
npmLsArgs.push("-g"); | ||
} | ||
|
||
try { | ||
const parsedStdout = JSON.parse(execCommand("npm", npmLsArgs)); | ||
|
||
if (isEmpty(parsedStdout)) { | ||
return "Not found"; | ||
} | ||
|
||
const [, processBinPath] = process.argv; | ||
let npmBinPath; | ||
|
||
try { | ||
npmBinPath = execCommand("npm", npmBinArgs); | ||
} catch (e) { | ||
logger.error(`Error finding npm binary path when running command npm ${npmBinArgs.join(" ")}`); | ||
throw e; | ||
} | ||
|
||
const isGlobal = isInDirectory(npmBinPath, processBinPath); | ||
let version = parsedStdout.dependencies.eslint.version; | ||
|
||
if ((global && isGlobal) || (!global && !isGlobal)) { | ||
version += " (Currently used)"; | ||
} | ||
|
||
return version; | ||
} catch (e) { | ||
logger.error(`Error finding ${pkg} version running the command npm ${npmLsArgs.join(" ")}`); | ||
throw e; | ||
} | ||
} | ||
|
||
/** | ||
* Generates and returns execution environment information. | ||
* @returns {string} A string that contains execution environment information | ||
*/ | ||
function generateInfo() { | ||
return [ | ||
"Environment Info:", | ||
"", | ||
`Node version: ${normalizeVersionNumber(getBinVersion("node"))}`, | ||
`npm version: ${normalizeVersionNumber(getBinVersion("npm"))}`, | ||
`Local ESLint version: ${normalizeVersionNumber(getNpmPackageVersion("eslint", { global: false }))}`, | ||
`Global ESLint version: ${normalizeVersionNumber(getNpmPackageVersion("eslint", { global: true }))}` | ||
].join("\n"); | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Public Interface | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
log() { | ||
logger.info(generateInfo()); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters