Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
fix: fixed logging on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 28, 2018
1 parent 9e7bc1a commit ed1f207
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 66 deletions.
5 changes: 1 addition & 4 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as EventEmitter from 'events'
import * as semver from 'semver'

import {ActionBase} from './action/base'
Expand All @@ -25,22 +24,20 @@ const actionType = (

const Action = actionType === 'spinner' ? require('./action/spinner').default : require('./action/simple').default

export class Config extends EventEmitter {
export class Config {
logLevel: Levels = 'warn'
outputLevel: Levels = 'info'
_debug = false
action: ActionBase = new Action()
errorsHandled = false

constructor() {
super()
this.debug = process.env.DEBUG === '*'
}

get errlog(): string | undefined { return globals.errlog }
set errlog(errlog: string | undefined) {
globals.errlog = errlog
this.emit('errlog', errlog)
}

get debug(): boolean {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const scope = (_scope?: string) => {

async done() {
config.action.stop()
await logger.done()
await logger.flush()
// await flushStdout()
}
}
Expand Down
92 changes: 31 additions & 61 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,70 +28,40 @@ function canWrite(severity: 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fat
}

export default (e: IEventEmitter) => {
let lines: string[] = []
let cur: {close(): Promise<void>} | undefined
let flushing: Promise<void> = Promise.resolve()
let buffer: string[] = []
// it would be preferable to use createWriteStream
// but it gives out EPERM issues on windows for some reason
//
// let stream: Promise<fs.WriteStream> | undefined
// function getStream() {
// return stream = stream || (async () => {
// await fs.mkdirp(path.dirname(file))
// return fs.createWriteStream(file, {flags: 'a+', encoding: 'utf8'})
// })()
// }

function log(file: string) {
let stream: Promise<fs.WriteStream> | undefined
function getStream() {
return stream = stream || (async () => {
await fs.mkdirp(path.dirname(file))
return fs.createWriteStream(file, {flags: 'a+', encoding: 'utf8'})
})()
}

let flushing: Promise<void> = Promise.resolve()
async function flush(waitForMs: number = 0) {
if (lines.length === 0) return flushing
const stream = await getStream()
await wait(waitForMs)
flushing = flushing.then(() => {
if (lines.length === 0) return
const mylines = lines
lines = []
return new Promise<any>((resolve, reject) => {
stream.write(mylines.join('\n') + '\n', (err: any) => err ? reject(err) : resolve())
})
})
}

const handleOutput = (m: Output.Message | Errors.Message) => {
if (!canWrite(m.severity)) return
const msg = m.type === 'error' ? Errors.getErrorMessage(m.error) : Output.render(m)
const output = chomp(_([timestamp(), m.severity.toUpperCase(), m.scope, msg]).compact().join(' '))
lines.push(deps.stripAnsi(output))
flush(50).catch(console.error)
}
e.on('output', handleOutput)

async function close() {
e.removeListener('output', handleOutput)
if (!stream) return
await flush()
const s = await stream
return new Promise<void>((resolve, reject) => {
s.end()
s.on('close', resolve)
s.on('error', reject)
stream = undefined
})
}

return {close}
const handleOutput = (m: Output.Message | Errors.Message) => {
if (!canWrite(m.severity)) return
const msg = m.type === 'error' ? Errors.getErrorMessage(m.error) : Output.render(m)
const output = chomp(_([timestamp(), m.severity.toUpperCase(), m.scope, msg]).compact().join(' '))
buffer.push(deps.stripAnsi(output))
flush(50).catch(console.error)
}
e.on('output', handleOutput)

async function done() {
if (cur) await cur.close()
}

async function startLogging() {
await done()
if (config.errlog) cur = log(config.errlog)
async function flush(waitForMs: number = 0) {
flushing = flushing.then(async () => {
await wait(waitForMs)
if (!config.errlog || buffer.length === 0) return
const file = config.errlog
const mylines = buffer
buffer = []
await fs.mkdirp(path.dirname(file))
await fs.appendFile(file, mylines.join('\n') + '\n')
})
await flushing
}
startLogging().catch(console.error)
config.on('errlog', startLogging)

return {
done,
}
return {flush}
}

0 comments on commit ed1f207

Please sign in to comment.