@rushstack/eslint-bulk
Version:
Roll out new ESLint rules in a large monorepo without cluttering up your code with "eslint-ignore-next-line"
135 lines • 5.83 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var _a;
import { execSync, spawnSync } from 'node:child_process';
import * as process from 'node:process';
import * as fs from 'node:fs';
const ESLINT_BULK_STDOUT_START_DELIMETER = 'RUSHSTACK_ESLINT_BULK_START';
const ESLINT_BULK_STDOUT_END_DELIMETER = 'RUSHSTACK_ESLINT_BULK_END';
const ESLINT_PACKAGE_NAME_ENV_VAR_NAME = '_RUSHSTACK_ESLINT_PACKAGE_NAME';
const BULK_SUPPRESSIONS_CLI_ESLINT_PACKAGE_NAME = (_a = process.env[ESLINT_PACKAGE_NAME_ENV_VAR_NAME]) !== null && _a !== void 0 ? _a : 'eslint';
const ESLINT_CONFIG_FILES = [
'eslint.config.js',
'eslint.config.cjs',
'eslint.config.mjs',
'.eslintrc.js',
'.eslintrc.cjs'
];
function findPatchPath() {
const candidatePaths = ESLINT_CONFIG_FILES.map((fileName) => `${process.cwd()}/${fileName}`);
let eslintConfigPath;
for (const candidatePath of candidatePaths) {
if (fs.existsSync(candidatePath)) {
eslintConfigPath = candidatePath;
break;
}
}
if (!eslintConfigPath) {
console.error('@rushstack/eslint-bulk: Please run this command from the directory that contains one of the following ' +
`ESLint configuration files: ${ESLINT_CONFIG_FILES.join(', ')}`);
process.exit(1);
}
const env = { ...process.env, _RUSHSTACK_ESLINT_BULK_DETECT: 'true' };
let eslintPackageJsonPath;
try {
eslintPackageJsonPath = require.resolve(`${BULK_SUPPRESSIONS_CLI_ESLINT_PACKAGE_NAME}/package.json`, {
paths: [process.cwd()]
});
}
catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
}
let eslintBinPath;
if (eslintPackageJsonPath) {
eslintPackageJsonPath = eslintPackageJsonPath.replace(/\\/g, '/');
const packagePath = eslintPackageJsonPath.substring(0, eslintPackageJsonPath.lastIndexOf('/'));
const { bin: { eslint: relativeEslintBinPath } = {} } = require(eslintPackageJsonPath);
if (relativeEslintBinPath) {
eslintBinPath = `${packagePath}/${relativeEslintBinPath}`;
}
else {
console.warn(`@rushstack/eslint-bulk: The eslint package resolved at "${packagePath}" does not contain an eslint bin path. ` +
'Attempting to use a globally-installed eslint instead.');
}
}
else {
console.log('@rushstack/eslint-bulk: Unable to resolve the eslint package as a dependency of the current project. ' +
'Attempting to use a globally-installed eslint instead.');
}
const eslintArgs = ['--stdin', '--config'];
const spawnOrExecOptions = {
env,
input: '',
stdio: 'pipe'
};
let runEslintFn;
if (eslintBinPath) {
runEslintFn = () => spawnSync(process.argv0, [eslintBinPath, ...eslintArgs, eslintConfigPath], spawnOrExecOptions).stdout;
}
else {
// Try to use a globally-installed eslint if a local package was not found
runEslintFn = () => execSync(`eslint ${eslintArgs.join(' ')} "${eslintConfigPath}"`, spawnOrExecOptions);
}
let stdout;
try {
stdout = runEslintFn();
}
catch (e) {
console.error('@rushstack/eslint-bulk: Error finding patch path: ' + e.message);
process.exit(1);
}
const regex = new RegExp(`${ESLINT_BULK_STDOUT_START_DELIMETER}(.*?)${ESLINT_BULK_STDOUT_END_DELIMETER}`);
const match = stdout.toString().match(regex);
if (match) {
// The configuration data will look something like this:
//
// RUSHSTACK_ESLINT_BULK_START{"minCliVersion":"0.0.0","cliEntryPoint":"path/to/eslint-bulk.js"}RUSHSTACK_ESLINT_BULK_END
const configurationJson = match[1].trim();
let configuration;
try {
configuration = JSON.parse(configurationJson);
if (!configuration.minCliVersion || !configuration.cliEntryPoint) {
throw new Error('Required field is missing');
}
}
catch (e) {
console.error('@rushstack/eslint-bulk: Error parsing patch configuration object:' + e.message);
process.exit(1);
}
const myVersion = require('../package.json').version;
const myVersionParts = myVersion.split('.').map((x) => parseInt(x, 10));
const minVersion = configuration.minCliVersion;
const minVersionParts = minVersion.split('.').map((x) => parseInt(x, 10));
if (myVersionParts.length !== 3 ||
minVersionParts.length !== 3 ||
myVersionParts.some((x) => isNaN(x)) ||
minVersionParts.some((x) => isNaN(x))) {
console.error(`@rushstack/eslint-bulk: Unable to compare versions "${myVersion}" and "${minVersion}"`);
process.exit(1);
}
for (let i = 0; i < 3; ++i) {
if (myVersionParts[i] > minVersionParts[i]) {
break;
}
if (myVersionParts[i] < minVersionParts[i]) {
console.error(`@rushstack/eslint-bulk: The @rushstack/eslint-bulk version ${myVersion} is too old;` +
` please upgrade to ${minVersion} or newer.`);
process.exit(1);
}
}
return configuration.cliEntryPoint;
}
console.error('@rushstack/eslint-bulk: Error finding patch path. Are you sure the package you are in has @rushstack/eslint-patch as a direct or indirect dependency?');
process.exit(1);
}
const patchPath = findPatchPath();
try {
require(patchPath);
}
catch (e) {
console.error(`@rushstack/eslint-bulk: Error running patch at ${patchPath}:\n` + e.message);
process.exit(1);
}
//# sourceMappingURL=start.js.map