Skip to content

Commit

Permalink
fix: show some colors in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyben committed Jul 26, 2019
1 parent 016a8da commit 06d8bbd
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 27 deletions.
7 changes: 3 additions & 4 deletions src/copy-addon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { sys, Program } from 'typescript'
import { resolve, relative, normalize } from 'path'
import { relativeToCWD } from './path.utils'
import { cp } from './fs.utils'
import { red } from './log.utils'

/**
* Copy non-typescript files to `outDir`.
Expand All @@ -13,14 +14,12 @@ export default function copyOtherFiles(program: Program, emittedFiles: string[]
const { outDir, listEmittedFiles } = program.getCompilerOptions()

if (outDir == null) {
console.warn('Cannot copy: you must define `outDir` in the compiler options')
console.error(red('Cannot copy: you must define `outDir` in the compiler options'))
return
}

const srcDir = program.getCommonSourceDirectory()
if (!srcDir) {
throw Error('Cannot copy: issue with internal typescript method `getCommonSourceDirectory`')
}
if (!srcDir) throw Error('Cannot copy: issue with internal typescript method `getCommonSourceDirectory`')

const otherFiles = matchAllFilesBut(srcDir, ['**/*.ts'])
const copiedFiles: string[] = [] // Track copied files to list them later if needed
Expand Down
30 changes: 7 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ts from 'typescript'
import { CreateProgramFromConfigOptions, TsConfig, EmitOptions, BuildOptions } from './interfaces'
import { ensureAbsolutePath } from './path.utils'
import { logDiagnostics, red, yellow, green } from './log.utils'
import cleanTargets from './clean-addon'
import copyOtherFiles from './copy-addon'

Expand Down Expand Up @@ -85,30 +86,13 @@ export function emit(program: ts.Program, { basePath, clean, copyOtherToOutDir,
const allDiagnostics = ts.getPreEmitDiagnostics(program).concat(diagnostics)
logDiagnostics(allDiagnostics, betterDiagnostics)

if (!options.noEmit && emitSkipped) throw Error('Compilation failed')

if (copyOtherToOutDir) copyOtherFiles(program, emittedFiles)

if (allDiagnostics.length) console.log(`Compilation done with ${allDiagnostics.length} errors`)
else console.log('Compilation successful')
}

/**
* Log compiler diagnostics to stderr.
* @internal
*/
function logDiagnostics(diagnostics: ts.Diagnostic[], better = false) {
if (!diagnostics.length) return

const formatHost: ts.FormatDiagnosticsHost = {
getCanonicalFileName: (path) => path,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine,
if (!options.noEmit && emitSkipped) {
console.error(red('Compilation failed'))
return
}

const message = better
? ts.formatDiagnosticsWithColorAndContext(diagnostics, formatHost)
: ts.formatDiagnostics(diagnostics, formatHost)
if (copyOtherToOutDir) copyOtherFiles(program, emittedFiles)

console.warn(message)
if (allDiagnostics.length) console.log(yellow(`Compilation done with ${allDiagnostics.length} errors`))
else console.log(green('Compilation successful'))
}
93 changes: 93 additions & 0 deletions src/log.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import ts from 'typescript'

/**
* Log compiler diagnostics to stderr.
* @internal
*/
export function logDiagnostics(diagnostics: ts.Diagnostic[], better = false) {
if (!diagnostics.length) return

const formatHost: ts.FormatDiagnosticsHost = {
getCanonicalFileName: (path) => path,
getCurrentDirectory: ts.sys.getCurrentDirectory,
getNewLine: () => ts.sys.newLine,
}

const message = better
? ts.formatDiagnosticsWithColorAndContext(diagnostics, formatHost)
: ts.formatDiagnostics(diagnostics, formatHost)

console.warn(message)
}

/**
* @internal
*/
export function grey(text: string) {
return formatColor(text, EscapeSequence.Grey)
}

/**
* @internal
*/
export function red(text: string) {
return formatColor(text, EscapeSequence.Red)
}

/**
* @internal
*/
export function green(text: string) {
return formatColor(text, EscapeSequence.Green)
}

/**
* @internal
*/
export function yellow(text: string) {
return formatColor(text, EscapeSequence.Yellow)
}

/**
* @internal
*/
export function blue(text: string) {
return formatColor(text, EscapeSequence.Blue)
}

/**
* @internal
*/
export function magenta(text: string) {
return formatColor(text, EscapeSequence.Magenta)
}

/**
* @internal
*/
export function cyan(text: string) {
return formatColor(text, EscapeSequence.Cyan)
}

/**
* Apply foreground color to text.
* @internal
*/
function formatColor(text: string, color: EscapeSequence) {
return color + text + EscapeSequence.Reset
}

/**
* Foreground color escape sequences.
* Taken from https://github.com/microsoft/TypeScript/blob/v3.5.3/src/compiler/program.ts#L368
*/
enum EscapeSequence {
Grey = '\u001b[90m',
Red = '\u001b[91m',
Green = '\u001b[92m',
Yellow = '\u001b[93m',
Blue = '\u001b[94m',
Magenta = '\u001b[95m',
Cyan = '\u001b[96m',
Reset = '\u001b[0m',
}
18 changes: 18 additions & 0 deletions src/ts-internals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ declare module 'typescript' {
readonly directories: ReadonlyArray<string>
}

/**
* @see https://github.com/microsoft/TypeScript/blob/v3.5.3/src/compiler/utilities.ts#L7087-L7105
*/
function createCompilerDiagnostic(message: DiagnosticMessage): Diagnostic

/**
* @see https://github.com/microsoft/TypeScript/blob/v3.5.3/src/compiler/utilities.ts#L7107-L7117
*/
function createCompilerDiagnosticFromMessageChain(chain: DiagnosticMessageChain): Diagnostic

enum ForegroundColorEscapeSequences {
Grey,
Red,
Yellow,
Blue,
Cyan,
}

interface Program {
/**
* @see https://github.com/microsoft/TypeScript/blob/v3.5.3/src/compiler/program.ts#L982-L1006
Expand Down

0 comments on commit 06d8bbd

Please sign in to comment.