Skip to content

Commit

Permalink
fix(exec2): line paddings and FORCE_COLOR
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Aug 31, 2024
1 parent b095337 commit 5ac4224
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
1 change: 1 addition & 0 deletions scripts/dot.script.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const { count: countStr, error } = parseArgs({

const count = Number(countStr)

console.log('very first line')
console.log({
count,
error,
Expand Down
19 changes: 11 additions & 8 deletions scripts/exec2.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import { runScript } from '../src'
import { exec2 } from '../src/util/exec2'

runScript(async () => {
// exec2.spawn({
// cmd: 'node scripts/dot.script.js --error',
// log: true,
// })
await exec2.spawnAsync('node', {
args: ['scripts/dot.script.js', '--error'],
log: true,
shell: true,
// forceColor: false,
// passProcessEnv: true,
})

// const s = exec2.exec({
// cmd: 'node scripts/dot.script.js --error',
Expand All @@ -33,8 +36,8 @@ runScript(async () => {
// cmd: 'git status',
// log: true,
// })
const { stdout } = await exec2.spawnAsync('git status', {
log: true,
})
console.log(stdout)
// const { stdout } = await exec2.spawnAsync('git status', {
// log: true,
// })
// console.log(stdout)
})
35 changes: 27 additions & 8 deletions src/util/exec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
NumberOfMilliseconds,
UnixTimestampMillisNumber,
} from '@naturalcycles/js-lib'
import { dimGrey, white } from '../colors/colors'
import { dimGrey, dimRed, hasColors, white } from '../colors/colors'

/**
* Set of utility functions to work with Spawn / Exec.
Expand Down Expand Up @@ -60,17 +60,21 @@ class Exec2 {
throwOnNonZeroCode = true,
cwd,
env,
forceColor = hasColors,
} = opt
let stdout = ''
let stderr = ''

if (printWhileRunning) console.log('') // 1-line padding before the output

return await new Promise<SpawnOutput>((resolve, reject) => {
const p = cp.spawn(cmd, opt.args || [], {
shell,
cwd,
env: {
...env,
...(opt.passProcessEnv ? process.env : {}),
...(forceColor ? { FORCE_COLOR: '1' } : {}),
},
})

Expand All @@ -94,7 +98,9 @@ class Exec2 {
})

p.on('close', code => {
this.logFinish(cmd, opt, started)
if (printWhileRunning) console.log('') // 1-line padding after the output
const isSuccessful = !code
this.logFinish(cmd, opt, started, isSuccessful)
const exitCode = code || 0
const o: SpawnOutput = {
exitCode,
Expand Down Expand Up @@ -125,7 +131,8 @@ class Exec2 {
spawn(cmd: string, opt: SpawnOptions = {}): void {
const started = Date.now()
this.logStart(cmd, opt)
const { shell = true, cwd, env } = opt
const { shell = true, cwd, env, forceColor = hasColors } = opt
console.log('') // 1-line padding before the output

const r = cp.spawnSync(cmd, opt.args, {
encoding: 'utf8',
Expand All @@ -135,10 +142,13 @@ class Exec2 {
env: {
...env,
...(opt.passProcessEnv ? process.env : {}),
...(forceColor ? { FORCE_COLOR: '1' } : {}),
},
})

this.logFinish(cmd, opt, started)
console.log('') // 1-line padding after the output
const isSuccessful = !r.error && !r.status
this.logFinish(cmd, opt, started, isSuccessful)

if (r.error) {
throw r.error
Expand Down Expand Up @@ -168,7 +178,7 @@ class Exec2 {
const { cwd, env, timeout } = opt

try {
return cp
const s = cp
.execSync(cmd, {
encoding: 'utf8',
// stdio: 'inherit', // no, otherwise we don't get the output returned
Expand All @@ -182,6 +192,9 @@ class Exec2 {
},
})
.trim()

this.logFinish(cmd, opt, started, true)
return s
} catch (err) {
// Not logging stderr, as it's printed by execSync by default (somehow)
// stdout is not printed by execSync though, therefor we print it here
Expand All @@ -191,9 +204,8 @@ class Exec2 {
if ((err as any).stdout) {
process.stdout.write((err as any).stdout)
}
this.logFinish(cmd, opt, started, false)
throw new Error(`exec exited with code ${(err as any).status}: ${cmd}`)
} finally {
this.logFinish(cmd, opt, started)
}
}

Expand Down Expand Up @@ -221,14 +233,16 @@ class Exec2 {
cmd: string,
opt: SpawnOptions | ExecOptions,
started: UnixTimestampMillisNumber,
isSuccessful: boolean,
): void {
if (!opt.logFinish && !opt.log) return
if (isSuccessful && !opt.logFinish && !opt.log) return

console.log(
[
white(opt.name || cmd),
...((!opt.name && (opt as SpawnOptions).args) || []),
dimGrey('took ' + _since(started)),
!isSuccessful && dimGrey('and ') + dimRed('failed'),
]
.filter(Boolean)
.join(' '),
Expand Down Expand Up @@ -316,6 +330,11 @@ export interface SpawnOptions {
* Set to true to pass `process.env` to the spawned process.
*/
passProcessEnv?: boolean
/**
* Defaults to "auto detect colors".
* Set to false or true to override.
*/
forceColor?: boolean
}

export interface ExecOptions {
Expand Down

0 comments on commit 5ac4224

Please sign in to comment.