| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
|
|---|
| 2 | // See LICENSE in the project root for license information.
|
|---|
| 3 | import fs from 'node:fs';
|
|---|
| 4 | import { printPruneHelp } from './utils/print-help';
|
|---|
| 5 | import { runEslintAsync } from './runEslint';
|
|---|
| 6 | import { ESLINT_BULK_PRUNE_ENV_VAR_NAME } from '../constants';
|
|---|
| 7 | import { deleteBulkSuppressionsFileInEslintConfigFolder, getSuppressionsConfigForEslintConfigFolderPath } from '../bulk-suppressions-file';
|
|---|
| 8 | export async function pruneAsync() {
|
|---|
| 9 | const args = process.argv.slice(3);
|
|---|
| 10 | if (args.includes('--help') || args.includes('-h')) {
|
|---|
| 11 | printPruneHelp();
|
|---|
| 12 | process.exit(0);
|
|---|
| 13 | }
|
|---|
| 14 | if (args.length > 0) {
|
|---|
| 15 | throw new Error(`@rushstack/eslint-bulk: Unknown arguments: ${args.join(' ')}`);
|
|---|
| 16 | }
|
|---|
| 17 | const normalizedCwd = process.cwd().replace(/\\/g, '/');
|
|---|
| 18 | const allFiles = await getAllFilesWithExistingSuppressionsForCwdAsync(normalizedCwd);
|
|---|
| 19 | if (allFiles.length > 0) {
|
|---|
| 20 | process.env[ESLINT_BULK_PRUNE_ENV_VAR_NAME] = '1';
|
|---|
| 21 | console.log(`Pruning suppressions for ${allFiles.length} files...`);
|
|---|
| 22 | await runEslintAsync(allFiles, 'prune');
|
|---|
| 23 | }
|
|---|
| 24 | else {
|
|---|
| 25 | console.log('No files with existing suppressions found.');
|
|---|
| 26 | deleteBulkSuppressionsFileInEslintConfigFolder(normalizedCwd);
|
|---|
| 27 | }
|
|---|
| 28 | }
|
|---|
| 29 | async function getAllFilesWithExistingSuppressionsForCwdAsync(normalizedCwd) {
|
|---|
| 30 | const { jsonObject: bulkSuppressionsConfigJson } = getSuppressionsConfigForEslintConfigFolderPath(normalizedCwd);
|
|---|
| 31 | const allFiles = new Set();
|
|---|
| 32 | for (const { file: filePath } of bulkSuppressionsConfigJson.suppressions) {
|
|---|
| 33 | allFiles.add(filePath);
|
|---|
| 34 | }
|
|---|
| 35 | const allFilesArray = Array.from(allFiles);
|
|---|
| 36 | const allExistingFiles = [];
|
|---|
| 37 | // TODO: limit parallelism here with something similar to `Async.forEachAsync` from `node-core-library`.
|
|---|
| 38 | await Promise.all(allFilesArray.map(async (filePath) => {
|
|---|
| 39 | try {
|
|---|
| 40 | await fs.promises.access(filePath, fs.constants.F_OK);
|
|---|
| 41 | allExistingFiles.push(filePath);
|
|---|
| 42 | }
|
|---|
| 43 | catch (_a) {
|
|---|
| 44 | // Doesn't exist - ignore
|
|---|
| 45 | }
|
|---|
| 46 | }));
|
|---|
| 47 | console.log(`Found ${allExistingFiles.length} files with existing suppressions.`);
|
|---|
| 48 | const deletedCount = allFilesArray.length - allExistingFiles.length;
|
|---|
| 49 | if (deletedCount > 0) {
|
|---|
| 50 | console.log(`${deletedCount} files with suppressions were deleted.`);
|
|---|
| 51 | }
|
|---|
| 52 | return allExistingFiles;
|
|---|
| 53 | }
|
|---|
| 54 | //# sourceMappingURL=prune.js.map |
|---|