Skip to content

Commit

Permalink
Convert to new flat config for eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilafian committed Aug 14, 2024
1 parent cefdcd6 commit 1e9c8d3
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 59 deletions.
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const error =
invalidOnlyUse ? 'The --only flag does not support multiple groups of commands.' :
null;
if (error)
throw Error('[run-scripts-util] ' + error);
throw new Error('[run-scripts-util] ' + error);
const options = {
continueOnError: cli.flagOn.continueOnError,
only: cli.flagOn.only ? Number(cli.flagMap.only) : null,
Expand Down
22 changes: 22 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// @ts-check

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';

export default [
eslint.configs.recommended,
...tseslint.configs.strictTypeChecked,
{ ignores: ['**/*.js'] },
{
languageOptions: { parserOptions: { projectService: true } },
rules: {
'@typescript-eslint/no-confusing-void-expression': 'off', //prefer minimal arrow functions
'@typescript-eslint/no-floating-promises': 'off', //annimations may be fire-and-forget
'@typescript-eslint/no-misused-promises': 'off', //annimations may be fire-and-forget
'@typescript-eslint/no-non-null-assertion': 'off', //ts cannot always know value exists
'@typescript-eslint/restrict-template-expressions': 'off', //numbers in templates are natural
'@typescript-eslint/unbound-method': 'off', //safer to not use 'this'
'@typescript-eslint/use-unknown-in-catch-callback-variable': 'off', //clarity over theoretical exceptions
},
},
];
33 changes: 7 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "run-scripts-util",
"version": "1.3.0",
"description": "Organize npm package.json scripts into named groups of easy to manage commands (CLI tool designed for use in npm package.json scripts)",
"description": "Organize npm package.json scripts into groups of easy to manage commands (CLI tool designed for use in npm package.json scripts)",
"license": "MIT",
"type": "module",
"module": "dist/run-scripts.js",
Expand Down Expand Up @@ -45,25 +45,6 @@
"node": true,
"mocha": true
},
"eslintConfig": {
"ignorePatterns": [
"build",
"dist",
"node_modules"
],
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"@typescript-eslint/no-non-null-assertion": "off"
}
},
"runScriptsConfig": {
"spec-a": [
"copy-folder bin spec/fixtures/target/a",
Expand Down Expand Up @@ -100,7 +81,7 @@
"scripts": {
"step:01": "rimraf build dist spec/fixtures/target",
"step:02": "jshint . --exclude-path .gitignore",
"step:03": "eslint --max-warnings 0 . --ext .ts",
"step:03": "eslint --max-warnings 0",
"step:10": "tsc",
"step:20": "add-dist-header build dist",
"pretest": "npm-run-all step:*",
Expand All @@ -114,19 +95,19 @@
"fancy-log": "~2.0"
},
"devDependencies": {
"@eslint/js": "~9.7",
"@eslint/js": "~9.9",
"@types/fancy-log": "~2.0",
"@types/node": "~20.14",
"@types/node": "~22.2",
"add-dist-header": "~1.4",
"assert-deep-strict-equal": "~1.2",
"copy-file-util": "~1.2",
"copy-folder-util": "~1.1",
"eslint": "8.57.0",
"eslint": "~9.9",
"jshint": "~2.13",
"mocha": "~10.6",
"mocha": "~10.7",
"npm-run-all2": "~6.2",
"rimraf": "~6.0",
"typescript": "~5.5",
"typescript-eslint": "~7.16"
"typescript-eslint": "~8.1"
}
}
23 changes: 14 additions & 9 deletions run-scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export type ProcessInfo = {
code: number,
ms: number,
};
type Pkg = {
runScriptsConfig?: { [key: string]: string | { [key: string]: string[] } },
scripts?: { [key: string]: string },
};

// Reporting
const arrow = chalk.gray.bold('→');
Expand Down Expand Up @@ -55,11 +59,11 @@ const runScripts = {
verbose: false,
};
const settings = { ...defaults, ...options };
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
const pkg = <Pkg>JSON.parse(fs.readFileSync('package.json', 'utf-8'));
const commands = pkg.runScriptsConfig?.[group] ?? [pkg.scripts?.[group]];
const logger = createLogger(settings);
if (!Array.isArray(commands) || commands.some(command => typeof command !== 'string'))
throw Error('[run-scripts-util] Cannot find commands: ' + group);
throw new Error('[run-scripts-util] Cannot find commands: ' + group);
const execCommand = (step: number, command: string) => {
const startTime = Date.now();
if (!settings.quiet)
Expand All @@ -73,7 +77,7 @@ const runScripts = {
if (task.status !== 0 && settings.continueOnError)
logger(chalk.red('ERROR'), chalk.white('-->'), errorMessage());
if (task.status !== 0 && !settings.continueOnError)
throw Error('[run-scripts-util] ' + errorMessage() + '\nCommand: ' + command);
throw new Error('[run-scripts-util] ' + errorMessage() + '\nCommand: ' + command);
logger(...logItems, chalk.green('done'), chalk.white(`(${Date.now() - startTime}ms)`));
};
const skip = (step: number, command: string) => {
Expand All @@ -83,8 +87,9 @@ const runScripts = {
logger(chalk.yellow('skipping:'), command);
return !active || commentedOut;
};
commands.forEach((command: string, index: number) =>
!skip(index + 1, command) && execCommand(index + 1, command));
const processCommand = (command: string, index: number) =>
!skip(index + 1, command) && execCommand(index + 1, command);
(<string[]>commands).forEach(processCommand);
},

execParallel(group: string, options?: Partial<Settings>) {
Expand All @@ -95,10 +100,10 @@ const runScripts = {
verbose: false,
};
const settings = { ...defaults, ...options };
const pkg = JSON.parse(fs.readFileSync('package.json', 'utf-8'));
const pkg = <Pkg>JSON.parse(fs.readFileSync('package.json', 'utf-8'));
const commands = pkg.runScriptsConfig?.[group] ?? [pkg.scripts?.[group]];
if (!Array.isArray(commands) || commands.some(command => typeof command !== 'string'))
throw Error('[run-scripts-util] Cannot find commands: ' + group);
throw new Error('[run-scripts-util] Cannot find commands: ' + group);
const logger = createLogger(settings);
const active = (step: number) => settings.only === null || step === settings.only;
const process = (step: number, command: string): Promise<ProcessInfo> => new Promise((resolve) => {
Expand All @@ -107,7 +112,7 @@ const runScripts = {
const pid = task.pid ?? null;
const logItems = [chalk.white(group), chalk.yellow(step)];
if (settings.verbose)
logItems.push(chalk.magenta('pid: ' + pid), arrow);
logItems.push(chalk.magenta('pid: ' + String(pid)), arrow);
logger(...logItems, chalk.cyanBright(command));
const processInfo = (code: number, ms: number): ProcessInfo =>
({ group, step, pid, start, code, ms });
Expand All @@ -118,7 +123,7 @@ const runScripts = {
const createProcess = (command: string, index: number): Promise<ProcessInfo | null> =>
active(index + 1) ? process(index + 1, command) : Promise.resolve(null);
logger(chalk.white(group), chalk.blue('--parallel'));
return Promise.all(commands.map(createProcess));
return Promise.all((<string[]>commands).map(createProcess));
},

};
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/target/a/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const error =
invalidOnlyUse ? 'The --only flag does not support multiple groups of commands.' :
null;
if (error)
throw Error('[run-scripts-util] ' + error);
throw new Error('[run-scripts-util] ' + error);
const options = {
continueOnError: cli.flagOn.continueOnError,
only: cli.flagOn.only ? Number(cli.flagMap.only) : null,
Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/target/a/cli2.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const error =
invalidOnlyUse ? 'The --only flag does not support multiple groups of commands.' :
null;
if (error)
throw Error('[run-scripts-util] ' + error);
throw new Error('[run-scripts-util] ' + error);
const options = {
continueOnError: cli.flagOn.continueOnError,
only: cli.flagOn.only ? Number(cli.flagMap.only) : null,
Expand Down
6 changes: 3 additions & 3 deletions spec/fixtures/target/b/1/w.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
"target": "ES2021",
"module": "ES2020",
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"outDir": "build",
"newLine": "lf",
"removeComments": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"removeComments": true
"noUnusedLocals": true
}
}
6 changes: 3 additions & 3 deletions spec/fixtures/target/b/1/x.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
"target": "ES2021",
"module": "ES2020",
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"outDir": "build",
"newLine": "lf",
"removeComments": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"removeComments": true
"noUnusedLocals": true
}
}
6 changes: 3 additions & 3 deletions spec/fixtures/target/b/1/y.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
"target": "ES2021",
"module": "ES2020",
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"outDir": "build",
"newLine": "lf",
"removeComments": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"removeComments": true
"noUnusedLocals": true
}
}
6 changes: 3 additions & 3 deletions spec/fixtures/target/b/1/z.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
"target": "ES2021",
"module": "ES2020",
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"outDir": "build",
"newLine": "lf",
"removeComments": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"removeComments": true
"noUnusedLocals": true
}
}
6 changes: 3 additions & 3 deletions spec/fixtures/target/b/2/w.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
"target": "ES2021",
"module": "ES2020",
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"outDir": "build",
"newLine": "lf",
"removeComments": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"removeComments": true
"noUnusedLocals": true
}
}
6 changes: 3 additions & 3 deletions spec/fixtures/target/b/2/x.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
"target": "ES2021",
"module": "ES2020",
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"outDir": "build",
"newLine": "lf",
"removeComments": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"removeComments": true
"noUnusedLocals": true
}
}
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
"target": "ES2021",
"module": "ES2020",
"moduleResolution": "node",
"esModuleInterop": true,
"declaration": true,
"outDir": "build",
"newLine": "lf",
"removeComments": true,
"strict": true,
"allowSyntheticDefaultImports": true,
"alwaysStrict": true,
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": true,
"removeComments": true
"noUnusedLocals": true
}
}

0 comments on commit 1e9c8d3

Please sign in to comment.