|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +import { run } from '@kbn/dev-cli-runner'; |
| 11 | +import { REPO_ROOT } from '@kbn/repo-info'; |
| 12 | +import { ToolingLog } from '@kbn/tooling-log'; |
| 13 | +import execa from 'execa'; |
| 14 | + |
| 15 | +const batchSize = 250; |
| 16 | +const maxParallelism = 8; |
| 17 | + |
| 18 | +run( |
| 19 | + async ({ log, flags }) => { |
| 20 | + const bail = !!(flags.bail || false); |
| 21 | + |
| 22 | + const { batches, files } = getLintableFileBatches(); |
| 23 | + log.info(`Found ${files.length} files in ${batches.length} batches to lint.`); |
| 24 | + |
| 25 | + const eslintArgs = |
| 26 | + // Unexpected will contain anything meant for ESLint directly, like `--fix` |
| 27 | + flags.unexpected |
| 28 | + // ESLint has no cache by default |
| 29 | + .concat([flags.cache ? '--cache' : '--no-cache']); |
| 30 | + log.info( |
| 31 | + `Running ESLint with args: ${pretty({ |
| 32 | + args: eslintArgs, |
| 33 | + batchSize, |
| 34 | + maxParallelism, |
| 35 | + })}` |
| 36 | + ); |
| 37 | + |
| 38 | + const lintPromiseThunks = batches.map( |
| 39 | + (batch, idx) => () => |
| 40 | + lintFileBatch({ batch, idx, eslintArgs, batchCount: batches.length, bail, log }) |
| 41 | + ); |
| 42 | + const results = await runBatchedPromises(lintPromiseThunks, maxParallelism); |
| 43 | + |
| 44 | + const failedBatches = results.filter((result) => !result.success); |
| 45 | + if (failedBatches.length > 0) { |
| 46 | + log.error(`Linting errors found ❌`); |
| 47 | + process.exit(1); |
| 48 | + } else { |
| 49 | + log.info('Linting successful ✅'); |
| 50 | + } |
| 51 | + }, |
| 52 | + { |
| 53 | + description: 'Run ESLint on all JavaScript/TypeScript files in the repository', |
| 54 | + flags: { |
| 55 | + boolean: ['bail', 'cache'], |
| 56 | + default: { |
| 57 | + bail: false, |
| 58 | + cache: true, // Enable caching by default |
| 59 | + }, |
| 60 | + allowUnexpected: true, |
| 61 | + help: ` |
| 62 | + --bail Stop on the first linting error |
| 63 | + --no-cache Disable ESLint caching |
| 64 | + `, |
| 65 | + }, |
| 66 | + } |
| 67 | +); |
| 68 | + |
| 69 | +function getLintableFileBatches() { |
| 70 | + const files = execa |
| 71 | + .sync('git', ['ls-files'], { |
| 72 | + cwd: REPO_ROOT, |
| 73 | + encoding: 'utf8', |
| 74 | + }) |
| 75 | + .stdout.trim() |
| 76 | + .split('\n') |
| 77 | + .filter((file) => file.match(/\.(js|mjs|ts|tsx)$/)); |
| 78 | + const batches = []; |
| 79 | + for (let i = 0; i < files.length; i += batchSize) { |
| 80 | + batches.push(files.slice(i, i + batchSize)); |
| 81 | + } |
| 82 | + return { batches, files }; |
| 83 | +} |
| 84 | + |
| 85 | +async function lintFileBatch({ |
| 86 | + batch, |
| 87 | + bail, |
| 88 | + idx, |
| 89 | + eslintArgs, |
| 90 | + batchCount, |
| 91 | + log, |
| 92 | +}: { |
| 93 | + batch: string[]; |
| 94 | + bail: boolean; |
| 95 | + idx: number; |
| 96 | + eslintArgs: string[]; |
| 97 | + batchCount: number; |
| 98 | + log: ToolingLog; |
| 99 | +}) { |
| 100 | + log.info(`Running batch ${idx + 1}/${batchCount} with ${batch.length} files...`); |
| 101 | + |
| 102 | + const timeBefore = Date.now(); |
| 103 | + const args = ['scripts/eslint'].concat(eslintArgs).concat(batch); |
| 104 | + const { stdout, stderr, exitCode } = await execa('node', args, { |
| 105 | + cwd: REPO_ROOT, |
| 106 | + env: { |
| 107 | + // Disable CI stats for individual runs, to avoid overloading ci-stats |
| 108 | + CI_STATS_DISABLED: 'true', |
| 109 | + }, |
| 110 | + reject: bail, // Don't throw on non-zero exit code |
| 111 | + }); |
| 112 | + |
| 113 | + const time = Date.now() - timeBefore; |
| 114 | + if (exitCode !== 0) { |
| 115 | + const errorMessage = stderr?.toString() || stdout?.toString(); |
| 116 | + log.error(`Batch ${idx + 1}/${batchCount} failed (${time}ms) ❌: ${errorMessage}`); |
| 117 | + return { |
| 118 | + success: false, |
| 119 | + idx, |
| 120 | + time, |
| 121 | + error: errorMessage, |
| 122 | + }; |
| 123 | + } else { |
| 124 | + log.info(`Batch ${idx + 1}/${batchCount} success (${time}ms) ✅: ${stdout.toString()}`); |
| 125 | + return { |
| 126 | + success: true, |
| 127 | + idx, |
| 128 | + time, |
| 129 | + }; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +function runBatchedPromises<T>( |
| 134 | + promiseCreators: Array<() => Promise<T>>, |
| 135 | + maxParallel: number |
| 136 | +): Promise<T[]> { |
| 137 | + const results: T[] = []; |
| 138 | + let i = 0; |
| 139 | + |
| 140 | + const next: () => Promise<any> = () => { |
| 141 | + if (i >= promiseCreators.length) { |
| 142 | + return Promise.resolve(); |
| 143 | + } |
| 144 | + |
| 145 | + const promiseCreator = promiseCreators[i++]; |
| 146 | + return Promise.resolve(promiseCreator()).then((result) => { |
| 147 | + results.push(result); |
| 148 | + return next(); |
| 149 | + }); |
| 150 | + }; |
| 151 | + |
| 152 | + const tasks = Array.from({ length: maxParallel }, () => next()); |
| 153 | + return Promise.all(tasks).then(() => results); |
| 154 | +} |
| 155 | + |
| 156 | +function pretty(obj: any) { |
| 157 | + return JSON.stringify(obj, null, 2); |
| 158 | +} |
0 commit comments