Skip to content

Commit

Permalink
fix: use exec utils from nodejs-lib
Browse files Browse the repository at this point in the history
Also, run eslint-all in parallel, unless --no-fix
  • Loading branch information
kirillgroshkov committed Aug 2, 2023
1 parent 31b9b21 commit 713df88
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 93 deletions.
3 changes: 1 addition & 2 deletions src/cmd/build-prod-esm-cjs.command.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
30 changes: 20 additions & 10 deletions src/cmd/eslint-all.command.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
const { ext, fix } = yargs.options({
ext: {
type: 'string',
Expand Down Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion src/cmd/lint-all.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function lintAllCommand(): Promise<void> {

const hadChangesBefore = gitHasUncommittedChanges()

eslintAllCommand()
await eslintAllCommand()

if (
fs.existsSync(`node_modules/stylelint`) &&
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/tsc-prod.command.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
70 changes: 0 additions & 70 deletions src/util/exec.util.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/util/jest.util.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
33 changes: 28 additions & 5 deletions src/util/lint.util.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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<void> {
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(',')}}`,
Expand All @@ -30,6 +55,4 @@ export function runESLint(
`--report-unused-disable-directives`,
fix ? `--fix` : '',
].filter(Boolean)

execVoidCommandSync('eslint', args)
}
2 changes: 1 addition & 1 deletion src/util/prettier.util.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion src/util/stylelint.util.ts
Original file line number Diff line number Diff line change
@@ -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')

Expand Down
2 changes: 1 addition & 1 deletion src/util/tsc.util.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
await Promise.all([tscAsync(noEmit), tscScriptsAsync()])
Expand Down

0 comments on commit 713df88

Please sign in to comment.