Skip to content

Commit

Permalink
refactor: split up writers into separate modules
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed May 25, 2018
1 parent 4d8c89e commit e528b35
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 236 deletions.
78 changes: 0 additions & 78 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"klaw": "^2.1.1",
"kubernetes-client": "^5.2.0",
"log-symbols": "^2.2.0",
"log-update": "^2.3.0",
"moment": "^2.22.1",
"node-emoji": "^1.8.1",
"node-pty": "^0.7.4",
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
PluginError,
} from "./exceptions"
import { Garden } from "./garden"
import { FileWriter } from "./logger/writers"
import { FileWriter } from "./logger/writers/file-writer"
import { getLogger, RootLogNode } from "./logger"
import { resolve } from "path"
import { BuildCommand } from "./commands/build"
Expand Down
4 changes: 3 additions & 1 deletion src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import {
LogEntryOpts,
LogSymbolType,
} from "./types"
import { BasicTerminalWriter, FancyTerminalWriter, Writer } from "./writers"
import { BasicTerminalWriter } from "./writers/basic-terminal-writer"
import { FancyTerminalWriter } from "./writers/fancy-terminal-writer"
import { Writer } from "./writers/base"
import { ParameterError } from "../exceptions"

const ROOT_DEPTH = -1
Expand Down
25 changes: 25 additions & 0 deletions src/logger/renderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,28 @@ export function renderDuration(entry: LogEntry): string {
? msgStyle(` (finished in ${duration(entry.timestamp)}s)`)
: ""
}

export function formatForTerminal(entry: LogEntry): string {
let renderers
if (entry.depth > 0) {
// Skip section on child entries.
renderers = [
[leftPad, [entry]],
[renderSymbol, [entry]],
[renderEmoji, [entry]],
[renderMsg, [entry]],
[renderDuration, [entry]],
["\n"],
]
} else {
renderers = [
[renderSymbol, [entry]],
[renderSection, [entry]],
[renderEmoji, [entry]],
[renderMsg, [entry]],
[renderDuration, [entry]],
["\n"],
]
}
return combine(renderers)
}
8 changes: 6 additions & 2 deletions src/logger/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { LogEntryOpts } from "./types"
import { LogEntryOpts, LogLevel } from "./types"
import { LogEntry, LogNode } from "."

export interface Node {
Expand Down Expand Up @@ -107,7 +107,7 @@ export function interceptStream(stream: NodeJS.WriteStream, callback: Function)
return restore
}

export const getTerminalWidth = (stream: NodeJS.WriteStream = process.stdout) => {
export function getTerminalWidth(stream: NodeJS.WriteStream = process.stdout) {
const columns = (stream || {}).columns

if (!columns) {
Expand All @@ -121,3 +121,7 @@ export const getTerminalWidth = (stream: NodeJS.WriteStream = process.stdout) =>

return columns
}

export function validate(level: LogLevel, entry: LogEntry): boolean {
return level >= entry.level && entry.opts.msg !== undefined
}
21 changes: 21 additions & 0 deletions src/logger/writers/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
LogLevel,
} from "../types"

import { LogEntry, RootLogNode } from "../index"

export interface WriterConfig {
level?: LogLevel
}

export abstract class Writer {
public level: LogLevel | undefined

constructor({ level }: WriterConfig = {}) {
this.level = level
}

abstract render(...args): string | string[] | null
abstract onGraphChange(entry: LogEntry, rootLogNode: RootLogNode): void
abstract stop(): void
}
30 changes: 30 additions & 0 deletions src/logger/writers/basic-terminal-writer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
LogLevel,
} from "../types"
import {
formatForTerminal,
} from "../renderers"
import { LogEntry, RootLogNode } from "../index"
import { validate } from "../util"
import { Writer } from "./base"

export class BasicTerminalWriter extends Writer {
public level: LogLevel

render(entry: LogEntry, rootLogNode: RootLogNode): string | null {
const level = this.level || rootLogNode.level
if (validate(level, entry)) {
return formatForTerminal(entry)
}
return null
}

onGraphChange(entry: LogEntry, rootLogNode: RootLogNode) {
const out = this.render(entry, rootLogNode)
if (out) {
console.log(out)
}
}

stop() { }
}
Loading

0 comments on commit e528b35

Please sign in to comment.