Skip to content

Commit

Permalink
fix(exec2): spawn/spawnAsync will log by default, as they already pri…
Browse files Browse the repository at this point in the history
…nt output
  • Loading branch information
kirillgroshkov committed Aug 31, 2024
1 parent c597e31 commit ae9dcca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion scripts/exec2.script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { exec2 } from '../src/util/exec2'
runScript(async () => {
await exec2.spawnAsync('node', {
args: ['scripts/dot.script.js', '--error'],
log: true,
// log: true,
shell: true,
// forceColor: false,
// passProcessEnv: true,
Expand Down
16 changes: 9 additions & 7 deletions src/util/exec2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class Exec2 {
* log: true
*/
async spawnAsync(cmd: string, opt: SpawnAsyncOptions = {}): Promise<SpawnOutput> {
const started = Date.now()
this.logStart(cmd, opt)
const {
shell = true,
printWhileRunning = true,
Expand All @@ -63,10 +61,13 @@ class Exec2 {
passProcessEnv = true,
forceColor = hasColors,
} = opt
opt.log ??= printWhileRunning // by default log should be true, as we are printing the output
const started = Date.now()
this.logStart(cmd, opt)
let stdout = ''
let stderr = ''

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

return await new Promise<SpawnOutput>((resolve, reject) => {
const p = cp.spawn(cmd, opt.args || [], {
Expand Down Expand Up @@ -99,7 +100,7 @@ class Exec2 {
})

p.on('close', code => {
if (printWhileRunning) console.log('') // 1-line padding after the output
// if (printWhileRunning) console.log('') // 1-line padding after the output
const isSuccessful = !code
this.logFinish(cmd, opt, started, isSuccessful)
const exitCode = code || 0
Expand Down Expand Up @@ -130,10 +131,11 @@ class Exec2 {
* log: true
*/
spawn(cmd: string, opt: SpawnOptions = {}): void {
const { shell = true, cwd, env, passProcessEnv = true, forceColor = hasColors } = opt
opt.log ??= true // by default log should be true, as we are printing the output
const started = Date.now()
this.logStart(cmd, opt)
const { shell = true, cwd, env, passProcessEnv = true, forceColor = hasColors } = opt
console.log('') // 1-line padding before the output
// console.log('') // 1-line padding before the output

const r = cp.spawnSync(cmd, opt.args, {
encoding: 'utf8',
Expand All @@ -147,7 +149,7 @@ class Exec2 {
},
})

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

Expand Down

0 comments on commit ae9dcca

Please sign in to comment.