Skip to content

Commit

Permalink
feat(deps): use exec2
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Aug 31, 2024
1 parent 9506fb9 commit f43cab8
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 184 deletions.
6 changes: 2 additions & 4 deletions scripts/eslintPrintConfig.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ Changes are visible in git diff every time they are observed.
*/

import { _sortObjectDeep } from '@naturalcycles/js-lib'
import { execVoidCommandSync, fs2, runScript } from '@naturalcycles/nodejs-lib'
import { exec2, fs2, runScript } from '@naturalcycles/nodejs-lib'
import { testDir } from '../src/paths'

runScript(async () => {
const outputPath = `${testDir}/cfg/eslint.config.dump.json`

execVoidCommandSync(`eslint --print-config src/index.ts > ${outputPath}`, [], {
shell: true,
})
exec2.spawn(`eslint --print-config src/index.ts > ${outputPath}`)

// execVoidCommandSync(`eslint --config ./eslint.config.js --parser-options=project:./scripts/tsconfig.json --print-config scripts/eslintPrintConfig.script.ts > ${outputPath}`, [], {
// shell: true,
Expand Down
69 changes: 37 additions & 32 deletions src/build.util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'node:fs'
import { _since } from '@naturalcycles/js-lib'
import { boldGrey, dimGrey, execVoidCommand, fs2, kpySync } from '@naturalcycles/nodejs-lib'
import { exec2, fs2, kpySync } from '@naturalcycles/nodejs-lib'

export async function buildEsmCjs(): Promise<void> {
// You cannot have a shared `tsconfig.prod.json` because of relative paths for `include`
Expand All @@ -21,28 +20,34 @@ export async function buildEsmCjs(): Promise<void> {
const esmPath = esmExists ? TSCONF_ESM_PATH : TSCONF_PROD_PATH

await Promise.all([
execVoidCommand('tsc', [
'-P',
cjsPath,
'--outDir',
'./dist',
'--module',
'nodenext',
'--moduleResolution',
'nodenext',
]),
execVoidCommand('tsc', [
'-P',
esmPath,
'--outDir',
'./dist-esm',
'--module',
'esnext',
'--moduleResolution',
'bundler',
'--declaration',
'false',
]),
exec2.spawnAsync('tsc', {
args: [
'-P',
cjsPath,
'--outDir',
'./dist',
'--module',
'nodenext',
'--moduleResolution',
'nodenext',
],
shell: false,
}),
exec2.spawnAsync('tsc', {
args: [
'-P',
esmPath,
'--outDir',
'./dist-esm',
'--module',
'esnext',
'--moduleResolution',
'bundler',
'--declaration',
'false',
],
shell: false,
}),
])
}

Expand Down Expand Up @@ -78,19 +83,19 @@ export async function runTSCInFolder(tsconfigPath: string, args: string[] = []):
return
}

const started = Date.now()
await execVoidCommand(`tsc`, ['-P', tsconfigPath, ...args])
console.log(`${boldGrey(`tsc ${tsconfigPath}`)} ${dimGrey(`took ` + _since(started))}`)
await exec2.spawnAsync(`tsc`, {
args: ['-P', tsconfigPath, ...args],
shell: false,
})
}

export async function runTSCProd(): Promise<void> {
const tsconfigPath = [`./tsconfig.prod.json`].find(p => fs.existsSync(p)) || 'tsconfig.json'

const args: string[] = ['-P', tsconfigPath]

const started = Date.now()
await execVoidCommand(`tsc`, args)
console.log(`${boldGrey('tsc prod')} ${dimGrey(`took ` + _since(started))}`)
await exec2.spawnAsync(`tsc`, {
args: ['-P', tsconfigPath],
shell: false,
})
}

export function buildCopy(): void {
Expand Down
82 changes: 29 additions & 53 deletions src/lint.util.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
import cp, { type ExecSyncOptions } from 'node:child_process'
import cp from 'node:child_process'
import fs from 'node:fs'
import { _isTruthy, _since, _truncate } from '@naturalcycles/js-lib'
import {
boldGrey,
commitMessageToTitleMessage,
dimGrey,
execVoidCommand,
execVoidCommandSync,
getLastGitCommitMsg,
gitCommitAll,
gitCurrentBranchName,
gitPull,
gitPush,
} from '@naturalcycles/nodejs-lib'
import { boldGrey, dimGrey, exec2, git2 } from '@naturalcycles/nodejs-lib'
import yargs from 'yargs'
import { cfgDir, scriptsDir } from './paths'
const {
Expand Down Expand Up @@ -73,12 +62,13 @@ export async function lintAllCommand(): Promise<void> {
const gitStatusAfter = gitStatus()
const hasChanges = gitStatusAfter !== gitStatusAtStart
if (!hasChanges) return
const msg = 'style(ci): ' + _truncate(commitMessageToTitleMessage(getLastGitCommitMsg()), 60)
const msg =
'style(ci): ' + _truncate(git2.commitMessageToTitleMessage(git2.getLastGitCommitMsg()), 60)

// pull, commit, push changes
gitPull()
gitCommitAll(msg)
gitPush()
git2.pull()
git2.commitAll(msg)
git2.push()

// fail on changes
if (failOnChanges) {
Expand Down Expand Up @@ -176,9 +166,8 @@ async function runESLint(
): Promise<void> {
if (!eslintConfigPath || !fs.existsSync(dir)) return // faster to bail-out like this

await execVoidCommand(
'eslint',
[
await exec2.spawnAsync('eslint', {
args: [
`--config`,
eslintConfigPath,
`${dir}/**/*.{${extensions.join(',')}}`,
Expand All @@ -187,7 +176,8 @@ async function runESLint(
`--report-unused-disable-directives`, // todo: unnecessary with flat, as it's defined in the config
fix ? `--fix` : '',
].filter(Boolean),
)
shell: false,
})
}

const prettierPaths = [
Expand All @@ -206,13 +196,10 @@ export function runPrettier(): void {
if (!prettierConfigPath) return

// prettier --write 'src/**/*.{js,ts,css,scss,graphql}'
execVoidCommandSync('prettier', [
`--write`,
`--log-level=warn`,
`--config`,
prettierConfigPath,
...prettierPaths,
])
exec2.spawn('prettier', {
args: [`--write`, `--log-level=warn`, `--config`, prettierConfigPath, ...prettierPaths],
shell: false,
})
}

const stylelintPaths = [
Expand All @@ -234,12 +221,12 @@ export function stylelintAll(): void {
const config = [`./stylelint.config.js`].find(f => fs.existsSync(f))
if (!config) return

execVoidCommandSync(
'stylelint',
[fix ? `--fix` : '', `--allow-empty-input`, `--config`, config, ...stylelintPaths].filter(
exec2.spawn('stylelint', {
args: [fix ? `--fix` : '', `--allow-empty-input`, `--config`, config, ...stylelintPaths].filter(
Boolean,
),
)
shell: false,
})
}

export async function lintStagedCommand(): Promise<void> {
Expand Down Expand Up @@ -277,12 +264,14 @@ export function runCommitlintCommand(): void {

const env = {
...process.env, // important to pass it through, to preserve $PATH
GIT_BRANCH: gitCurrentBranchName(),
GIT_BRANCH: git2.getCurrentBranchName(),
}

// await execWithArgs(`commitlint`, [`--edit`, editMsg, `--config`, config], { env })
execSync(`node ./node_modules/.bin/commitlint --edit ${editMsg} --config ${config}`, {
exec2.spawn(`node ./node_modules/.bin/commitlint --edit ${editMsg} --config ${config}`, {
env,
passProcessEnv: false,
forceColor: false,
})
}

Expand All @@ -297,9 +286,7 @@ function runActionLint(): void {
if (!fs.existsSync('.github/workflows')) return

if (canRunBinary('actionlint')) {
const started = Date.now()
execVoidCommandSync(`actionlint`)
console.log(`${boldGrey('actionlint')} ${dimGrey(`took ` + _since(started))}`)
exec2.spawn(`actionlint`)
} else {
console.log(
`actionlint is not installed and won't be run.\nThis is how to install it: https://github.com/rhysd/actionlint/blob/main/docs/install.md`,
Expand All @@ -323,12 +310,13 @@ export function runBiome(fix = true): void {

const dirs = [`src`, `scripts`, `e2e`, `playwright`].filter(d => fs.existsSync(d))

execVoidCommandSync(
`biome`,
[`lint`, fix && '--write', fix && '--unsafe', '--no-errors-on-unmatched', ...dirs].filter(
exec2.spawn(`biome`, {
args: [`lint`, fix && '--write', fix && '--unsafe', '--no-errors-on-unmatched', ...dirs].filter(
_isTruthy,
),
)
logFinish: false,
shell: false,
})
}

function canRunBinary(name: string): boolean {
Expand All @@ -347,15 +335,3 @@ function gitStatus(): string | undefined {
})
} catch {}
}

function execSync(cmd: string, opt?: ExecSyncOptions): void {
try {
cp.execSync(cmd, {
...opt,
encoding: 'utf8',
stdio: 'inherit',
})
} catch {
process.exit(1)
}
}
12 changes: 9 additions & 3 deletions src/test.util.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'node:fs'
import { _range, _uniq } from '@naturalcycles/js-lib'
import { dimGrey, execVoidCommandSync, white } from '@naturalcycles/nodejs-lib'
import { dimGrey, exec2, white } from '@naturalcycles/nodejs-lib'
import { cfgDir } from './paths'

interface RunJestOpt {
Expand Down Expand Up @@ -100,12 +100,18 @@ export function runJest(opt: RunJestOpt = {}): void {
const shards = _range(1, totalShards + 1)

for (const shard of shards) {
execVoidCommandSync('jest', _uniq([...args, `--shard=${shard}/${totalShards}`]), {
exec2.spawn('jest', {
args: _uniq([...args, `--shard=${shard}/${totalShards}`]),
logFinish: false,
shell: false,
env,
})
}
} else {
execVoidCommandSync('jest', _uniq(args), {
exec2.spawn('jest', {
args: _uniq(args),
logFinish: false,
shell: false,
env,
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/cfg/eslint.config.dump.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"0": ".vue",
"1": ".html"
},
"parser": "typescript-eslint/parser@8.0.1",
"parser": "typescript-eslint/parser@8.3.0",
"project": "tsconfig.json"
},
"sourceType": "module"
Expand All @@ -18,13 +18,13 @@
},
"plugins": [
"@",
"@typescript-eslint:@typescript-eslint/eslint-plugin@8.0.1",
"@typescript-eslint:@typescript-eslint/eslint-plugin@8.3.0",
"unicorn:eslint-plugin-unicorn@55.0.0",
"vue:eslint-plugin-vue@9.27.0",
"import-x",
"simple-import-sort:eslint-plugin-simple-import-sort@12.1.1",
"jsdoc",
"jest:eslint-plugin-jest@28.8.0"
"jest:eslint-plugin-jest@28.8.1"
],
"processor": "vue/vue",
"rules": {
Expand Down
16 changes: 8 additions & 8 deletions src/yarn.util.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import fs from 'node:fs'
import { execVoidCommandSync } from '@naturalcycles/nodejs-lib'
import { exec2 } from '@naturalcycles/nodejs-lib'

export function up(): void {
execVoidCommandSync('yarn', ['upgrade'])
execVoidCommandSync('yarn-deduplicate')
execVoidCommandSync('yarn')
exec2.spawn('yarn upgrade')
exec2.spawn('yarn-deduplicate')
exec2.spawn('yarn')

if (fs.existsSync(`node_modules/patch-package`)) {
execVoidCommandSync('patch-package')
exec2.spawn('patch-package')
}
}

export function upnc(): void {
execVoidCommandSync('yarn', ['upgrade', '--pattern', `@naturalcycles`])
execVoidCommandSync('yarn-deduplicate')
execVoidCommandSync('yarn')
exec2.spawn('yarn upgrade --pattern @naturalcycles')
exec2.spawn('yarn-deduplicate')
exec2.spawn('yarn')
}
Loading

0 comments on commit f43cab8

Please sign in to comment.