Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Adds new info flag #12060

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 24 additions & 11 deletions bin/eslint.js
Expand Up @@ -16,9 +16,10 @@ require("v8-compile-cache");
// Helpers
//------------------------------------------------------------------------------

const useStdIn = (process.argv.indexOf("--stdin") > -1),
init = (process.argv.indexOf("--init") > -1),
debug = (process.argv.indexOf("--debug") > -1);
const useStdIn = process.argv.includes("--stdin"),
init = process.argv.includes("--init"),
debug = process.argv.includes("--debug"),
info = ["--info", "-i"].some(flag => process.argv.includes(flag));

// must do this initialization *before* other requires in order to work
if (debug) {
Expand All @@ -30,9 +31,10 @@ if (debug) {
//------------------------------------------------------------------------------

// now we can safely include the other modules that use debug
const cli = require("../lib/cli"),
path = require("path"),
fs = require("fs");
const path = require("path"),
fs = require("fs"),
cli = require("../lib/cli"),
logger = require("../lib/shared/logging");

//------------------------------------------------------------------------------
// Execution
Expand All @@ -47,11 +49,11 @@ process.once("uncaughtException", err => {
const template = lodash.template(fs.readFileSync(path.resolve(__dirname, `../messages/${err.messageTemplate}.txt`), "utf-8"));
const pkg = require("../package.json");

console.error("\nOops! Something went wrong! :(");
console.error(`\nESLint: ${pkg.version}.\n\n${template(err.messageData || {})}`);
logger.error("\nOops! Something went wrong! :(");
logger.error(`\nESLint: ${pkg.version}.\n\n${template(err.messageData || {})}`);
} else {

console.error(err.stack);
logger.error(err.stack);
}

process.exitCode = 2;
Expand All @@ -67,15 +69,26 @@ if (useStdIn) {
const STDIN_FILE_DESCRIPTOR = 0;

process.exitCode = cli.execute(process.argv, fs.readFileSync(STDIN_FILE_DESCRIPTOR, "utf8"));
} else if (info) {
const infoLogger = require("../lib/info");

try {
infoLogger.log();
process.exitCode = 0;
} catch (err) {
process.exitCode = 1;
logger.error(err.message);
logger.error(err.stack);
}
} else if (init) {
const configInit = require("../lib/init/config-initializer");

configInit.initializeConfig().then(() => {
process.exitCode = 0;
}).catch(err => {
process.exitCode = 1;
console.error(err.message);
console.error(err.stack);
logger.error(err.message);
logger.error(err.stack);
});
} else {
process.exitCode = cli.execute(process.argv);
Expand Down
4 changes: 2 additions & 2 deletions lib/cli.js
Expand Up @@ -67,7 +67,8 @@ function translateOptions(cliOptions) {
fixTypes: cliOptions.fixType,
allowInlineConfig: cliOptions.inlineConfig,
reportUnusedDisableDirectives: cliOptions.reportUnusedDisableDirectives,
resolvePluginsRelativeTo: cliOptions.resolvePluginsRelativeTo
resolvePluginsRelativeTo: cliOptions.resolvePluginsRelativeTo,
info: cliOptions.info
};
}

Expand Down Expand Up @@ -165,7 +166,6 @@ const cli = {
if (currentOptions.version) { // version from package.json

log.info(`v${require("../package.json").version}`);

} else if (currentOptions.printConfig) {
if (files.length) {
log.error("The --print-config option must be used with exactly one file name.");
Expand Down
142 changes: 142 additions & 0 deletions lib/info/index.js
@@ -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());
}
};
6 changes: 6 additions & 0 deletions lib/options.js
Expand Up @@ -246,6 +246,12 @@ module.exports = optionator({
option: "print-config",
type: "path::String",
description: "Print the configuration for the given file"
},
{
option: "info",
alias: "i",
type: "Boolean",
description: "Print debugging information concerning the local environmet"
}
]
});
7 changes: 7 additions & 0 deletions tests/lib/cli.js
Expand Up @@ -1112,4 +1112,11 @@ describe("cli", () => {
});
});

describe("when passing --info", () => {
it("should print out debugging information concerning the local environment", () => {
cli.execute("--info");
assert.isTrue(log.info.called);
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
});
});

});