UNPKG

@rushstack/eslint-bulk

Version:

Roll out new ESLint rules in a large monorepo without cluttering up your code with "eslint-ignore-next-line"

165 lines 6.96 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const child_process_1 = require("child_process"); const process = __importStar(require("process")); const fs = __importStar(require("fs")); 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('eslint/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 = () => (0, child_process_1.spawnSync)(process.argv0, [eslintBinPath, ...eslintArgs, eslintConfigPath], spawnOrExecOptions).stdout; } else { // Try to use a globally-installed eslint if a local package was not found runEslintFn = () => (0, child_process_1.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 startDelimiter = 'RUSHSTACK_ESLINT_BULK_START'; const endDelimiter = 'RUSHSTACK_ESLINT_BULK_END'; const regex = new RegExp(`${startDelimiter}(.*?)${endDelimiter}`); 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