From 713df8812f76a28a4d0edbdfb5b1a175d27cdf7e Mon Sep 17 00:00:00 2001 From: kirillgroshkov Date: Wed, 2 Aug 2023 11:59:27 +0200 Subject: [PATCH] fix: use exec utils from nodejs-lib Also, run eslint-all in parallel, unless --no-fix --- src/cmd/build-prod-esm-cjs.command.ts | 3 +- src/cmd/eslint-all.command.ts | 30 ++++++++---- src/cmd/lint-all.command.ts | 2 +- src/cmd/tsc-prod.command.ts | 2 +- src/util/exec.util.ts | 70 --------------------------- src/util/jest.util.ts | 2 +- src/util/lint.util.ts | 33 +++++++++++-- src/util/prettier.util.ts | 2 +- src/util/stylelint.util.ts | 2 +- src/util/tsc.util.ts | 2 +- 10 files changed, 55 insertions(+), 93 deletions(-) delete mode 100644 src/util/exec.util.ts diff --git a/src/cmd/build-prod-esm-cjs.command.ts b/src/cmd/build-prod-esm-cjs.command.ts index b18223c..a2c3058 100644 --- a/src/cmd/build-prod-esm-cjs.command.ts +++ b/src/cmd/build-prod-esm-cjs.command.ts @@ -1,5 +1,4 @@ -import { _emptyDirSync, _pathExistsSync } from '@naturalcycles/nodejs-lib' -import { execVoidCommand } from '../util/exec.util' +import { _emptyDirSync, _pathExistsSync, execVoidCommand } from '@naturalcycles/nodejs-lib' // You cannot have a shared `tsconfig.prod.json` because of relative paths for `include` const TSCONF_CJS_PATH = `./tsconfig.cjs.prod.json` diff --git a/src/cmd/eslint-all.command.ts b/src/cmd/eslint-all.command.ts index 5bddeac..8d7acfc 100644 --- a/src/cmd/eslint-all.command.ts +++ b/src/cmd/eslint-all.command.ts @@ -1,12 +1,12 @@ import * as fs from 'node:fs' import * as yargs from 'yargs' import { cfgDir } from '../cnst/paths.cnst' -import { getTSConfigPathScripts, runESLint } from '../util/lint.util' +import { getTSConfigPathScripts, runESLintAsync } from '../util/lint.util' /** * Runs `eslint` command for all predefined paths (e.g /src, /scripts, etc). */ -export function eslintAllCommand(): void { +export async function eslintAllCommand(): Promise { const { ext, fix } = yargs.options({ ext: { type: 'string', @@ -34,13 +34,23 @@ export function eslintAllCommand(): void { const tsconfigPathE2e = `./e2e/tsconfig.json` // todo: run on other dirs too, e.g pages, components, layouts - // /src - // await runESLint(`./src`, eslintConfigPathRoot, tsconfigPath, extensions) - runESLint(`./src`, eslintConfigPathRoot, undefined, extensions, fix) - // /scripts - runESLint(`./scripts`, eslintConfigPathScripts, tsconfigPathScripts, undefined, fix) - - // /e2e - runESLint(`./e2e`, eslintConfigPathE2e, tsconfigPathE2e, undefined, fix) + if (fix) { + await Promise.all([ + // /src + runESLintAsync(`./src`, eslintConfigPathRoot, undefined, extensions, fix), + // /scripts + runESLintAsync(`./scripts`, eslintConfigPathScripts, tsconfigPathScripts, undefined, fix), + // /e2e + runESLintAsync(`./e2e`, eslintConfigPathE2e, tsconfigPathE2e, undefined, fix), + ]) + } else { + // with no-fix - let's run serially + // /src + await runESLintAsync(`./src`, eslintConfigPathRoot, undefined, extensions, fix) + // /scripts + await runESLintAsync(`./scripts`, eslintConfigPathScripts, tsconfigPathScripts, undefined, fix) + // /e2e + await runESLintAsync(`./e2e`, eslintConfigPathE2e, tsconfigPathE2e, undefined, fix) + } } diff --git a/src/cmd/lint-all.command.ts b/src/cmd/lint-all.command.ts index e08f85f..8744f25 100644 --- a/src/cmd/lint-all.command.ts +++ b/src/cmd/lint-all.command.ts @@ -30,7 +30,7 @@ export async function lintAllCommand(): Promise { const hadChangesBefore = gitHasUncommittedChanges() - eslintAllCommand() + await eslintAllCommand() if ( fs.existsSync(`node_modules/stylelint`) && diff --git a/src/cmd/tsc-prod.command.ts b/src/cmd/tsc-prod.command.ts index 7ff34ec..657c386 100644 --- a/src/cmd/tsc-prod.command.ts +++ b/src/cmd/tsc-prod.command.ts @@ -1,6 +1,6 @@ import { _since } from '@naturalcycles/js-lib' +import { execVoidCommandSync } from '@naturalcycles/nodejs-lib' import { boldGrey, dimGrey } from '@naturalcycles/nodejs-lib/dist/colors' -import { execVoidCommandSync } from '../util/exec.util' export function tscProdCommand(): void { // You cannot have a shared `tsconfig.prod.json` because of relative paths for `include` diff --git a/src/util/exec.util.ts b/src/util/exec.util.ts deleted file mode 100644 index 52f6e40..0000000 --- a/src/util/exec.util.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { ProcessEnvOptions, SpawnOptions } from 'node:child_process' -import * as cp from 'node:child_process' -import { grey } from '@naturalcycles/nodejs-lib/dist/colors' - -export async function execVoidCommand( - cmd: string, - args: string[] = [], - opt: SpawnOptions = {}, -): Promise { - logExec(cmd, args, opt) - - await new Promise(resolve => { - const p = cp.spawn(cmd, [...args], { - stdio: 'inherit', - // shell: true, - ...opt, - env: { - ...process.env, - ...opt.env, - }, - }) - - p.on('close', code => { - if (code) { - console.log(`${cmd} exited with code ${code}`) - process.exit(code) - } - resolve() - }) - }) -} - -export function execVoidCommandSync( - cmd: string, - args: string[] = [], - opt: SpawnOptions = {}, -): void { - logExec(cmd, args, opt) - - const r = cp.spawnSync(cmd, [...args], { - encoding: 'utf8', - stdio: 'inherit', - // shell: true, // removing shell breaks executing `tsc` - ...opt, - env: { - ...process.env, - ...opt.env, - }, - }) - - if (r.status) { - console.log(`${cmd} exited with code ${r.status}`) - process.exit(r.status) - } - - if (r.error) { - console.log(r.error) - process.exit((r.error as NodeJS.ErrnoException).errno || 1) - } -} - -function logExec(cmd: string, args: string[] = [], opt: ProcessEnvOptions = {}): void { - const cmdline = [ - ...Object.entries(opt.env || {}).map(([k, v]) => [k, v].join('=')), - cmd, - ...args, - ].join(' ') - - console.log(grey(cmdline)) -} diff --git a/src/util/jest.util.ts b/src/util/jest.util.ts index 8ec7357..851b7fa 100644 --- a/src/util/jest.util.ts +++ b/src/util/jest.util.ts @@ -1,9 +1,9 @@ import * as fs from 'node:fs' import * as os from 'node:os' +import { execVoidCommandSync } from '@naturalcycles/nodejs-lib' import { _range, _uniq } from '@naturalcycles/js-lib' import { dimGrey, white } from '@naturalcycles/nodejs-lib/dist/colors' import { cfgDir } from '../cnst/paths.cnst' -import { execVoidCommandSync } from './exec.util' import { nodeModuleExists } from './test.util' export function getJestConfigPath(): string { diff --git a/src/util/lint.util.ts b/src/util/lint.util.ts index a59ce46..a2ce4b9 100644 --- a/src/util/lint.util.ts +++ b/src/util/lint.util.ts @@ -1,6 +1,6 @@ import * as fs from 'node:fs' +import { execVoidCommand, execVoidCommandSync } from '@naturalcycles/nodejs-lib' import { scriptsDir } from '../cnst/paths.cnst' -import { execVoidCommandSync } from './exec.util' export function getTSConfigPath(): string { // this is to support "Solution style tsconfig.json" (as used in Angular10, for example) @@ -15,13 +15,38 @@ export function getTSConfigPathScripts(): string { export function runESLint( dir: string, eslintConfigPath: string, - tsconfigPath: string | undefined, + tsconfigPath?: string, extensions = ['ts', 'tsx', 'vue'], fix = true, ): void { if (!fs.existsSync(dir)) return // faster to bail-out like this - const args = [ + execVoidCommandSync('eslint', getEslintArgs(dir, eslintConfigPath, tsconfigPath, extensions, fix)) +} + +export async function runESLintAsync( + dir: string, + eslintConfigPath: string, + tsconfigPath?: string, + extensions = ['ts', 'tsx', 'vue'], + fix = true, +): Promise { + if (!fs.existsSync(dir)) return // faster to bail-out like this + + await execVoidCommand( + 'eslint', + getEslintArgs(dir, eslintConfigPath, tsconfigPath, extensions, fix), + ) +} + +function getEslintArgs( + dir: string, + eslintConfigPath: string, + tsconfigPath?: string, + extensions = ['ts', 'tsx', 'vue'], + fix = true, +): string[] { + return [ `--config`, eslintConfigPath, `${dir}/**/*.{${extensions.join(',')}}`, @@ -30,6 +55,4 @@ export function runESLint( `--report-unused-disable-directives`, fix ? `--fix` : '', ].filter(Boolean) - - execVoidCommandSync('eslint', args) } diff --git a/src/util/prettier.util.ts b/src/util/prettier.util.ts index b70cb7e..df76d66 100644 --- a/src/util/prettier.util.ts +++ b/src/util/prettier.util.ts @@ -1,6 +1,6 @@ import * as fs from 'node:fs' +import { execVoidCommandSync } from '@naturalcycles/nodejs-lib' import { cfgDir } from '../cnst/paths.cnst' -import { execVoidCommandSync } from './exec.util' const { prettierDirs, prettierExtensionsAll, lintExclude } = require('../../cfg/_cnst') const prettierPaths = [ diff --git a/src/util/stylelint.util.ts b/src/util/stylelint.util.ts index c371b0c..b81a902 100644 --- a/src/util/stylelint.util.ts +++ b/src/util/stylelint.util.ts @@ -1,7 +1,7 @@ import * as fs from 'node:fs' +import { execVoidCommandSync } from '@naturalcycles/nodejs-lib' import * as yargs from 'yargs' import { cfgDir } from '../cnst/paths.cnst' -import { execVoidCommandSync } from './exec.util' const { prettierDirs, stylelintExtensions, lintExclude } = require('../../cfg/_cnst') diff --git a/src/util/tsc.util.ts b/src/util/tsc.util.ts index 3628e52..9e8b902 100644 --- a/src/util/tsc.util.ts +++ b/src/util/tsc.util.ts @@ -1,9 +1,9 @@ import * as fs from 'node:fs' +import { execVoidCommand, execVoidCommandSync } from '@naturalcycles/nodejs-lib' import { _isTruthy, _since } from '@naturalcycles/js-lib' import { boldGrey, dimGrey } from '@naturalcycles/nodejs-lib/dist/colors' import { kpySync } from '@naturalcycles/nodejs-lib/dist/fs' import { cfgDir } from '../cnst/paths.cnst' -import { execVoidCommand, execVoidCommandSync } from './exec.util' export async function tscMainAndScripts(noEmit = false): Promise { await Promise.all([tscAsync(noEmit), tscScriptsAsync()])