From c1531cbf23fe5dc38a71dac0c4dd4d2e742f6073 Mon Sep 17 00:00:00 2001 From: Bryce Gibson Date: Thu, 5 Oct 2017 09:11:38 +1100 Subject: [PATCH] Pause logger before and resume after spawn. --- src/compiler.ts | 19 +++++++++++++++---- src/logger.ts | 10 +++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/compiler.ts b/src/compiler.ts index 5ca3b60..9d8898c 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -4,7 +4,7 @@ import { createHash } from 'crypto' import { createReadStream } from 'fs' import { Readable } from 'stream' import { spawn } from 'child_process' -import { Logger } from './logger' +import { Logger, LogStep } from './logger' import { readFileAsync, writeFileAsync, pathExistsAsync, dequote, isWindows, bound } from './util' import { NexeOptions, version } from './options' import { NexeTarget } from './target' @@ -32,7 +32,7 @@ interface NexeHeader { export class NexeCompiler { private start = Date.now() private env = { ...process.env } - private compileStep: { modify: Function; log: Function } + private compileStep: LogStep public log = new Logger(this.options.loglevel) public src: string public files: NexeFile[] = [] @@ -132,14 +132,25 @@ export class NexeCompiler { } private _runBuildCommandAsync(command: string, args: string[]) { + if (this.log.verbose) { + this.compileStep.pause() + } return new Promise((resolve, reject) => { spawn(command, args, { cwd: this.src, env: this.env, - stdio: this.options.loglevel === 'verbose' ? 'inherit' : 'ignore' + stdio: this.log.verbose ? 'inherit' : 'ignore' }) - .once('error', reject) + .once('error', (e: Error) => { + if (this.log.verbose) { + this.compileStep.resume() + } + reject(e) + }) .once('close', (code: number) => { + if (this.log.verbose) { + this.compileStep.resume() + } if (code != 0) { const error = `${command} ${args.join(' ')} exited with code: ${code}` reject(new Error(error)) diff --git a/src/logger.ts b/src/logger.ts index 2f7a159..7f8bd73 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -6,10 +6,12 @@ const frameLength = 120 export interface LogStep { modify(text: string, color?: string): void log(text: string, color?: string): void + pause(): void + resume(): void } export class Logger { - private verbose: boolean + public verbose: boolean private silent: boolean private ora: any private modify: Function @@ -48,7 +50,7 @@ export class Logger { step(text: string, method: string = 'succeed'): LogStep { if (this.silent) { - return { modify() {}, log() {} } + return { modify() {}, log() {}, pause() {}, resume() {} } } if (!this.ora.id) { this.ora.start().text = text @@ -62,7 +64,9 @@ export class Logger { return { modify: this.modify, - log: this.verbose ? this.write : this.modify + log: this.verbose ? this.write : this.modify, + pause: () => this.ora.stopAndPersist(), + resume: () => this.ora.start() } as LogStep } }