forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.ts
More file actions
90 lines (74 loc) · 2.3 KB
/
command.ts
File metadata and controls
90 lines (74 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { Flags, LoggerPriority } from "jacdac-ts"
export const GENDIR = ".devicescript"
export const LIBDIR = `node_modules/@devicescript`
export const BINDIR = `${GENDIR}/bin`
export const FLASHDIR = `${GENDIR}/flash`
export const FLASHFILE = `sim.bin`
export const log = console.log
export function debug(...args: any[]) {
if (!isQuiet) console.debug(...wrapArgs(34, args))
}
export function error(...args: any[]) {
console.error(...wrapArgs(91, args))
}
export function fatal(msg: string): never {
error("fatal error: " + msg)
process.exit(1)
}
export let consoleColors = true
export function setConsoleColors(enabled: boolean) {
consoleColors = !!enabled
}
export function setDeveloperMode(enabled: boolean) {
Flags.developerMode = !!enabled
}
// https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit
export function wrapColor(n: number | string, message: string) {
if (consoleColors) return `\x1B[${n}m${message}\x1B[0m`
else return message
}
function wrapArgs(color: number, args: any[]) {
if (
consoleColors &&
args.every(e => typeof e == "string" || typeof e == "number")
) {
// if it's just strings & numbers use the coloring
const msg = args.join(" ")
return [wrapColor(color, msg)]
} else {
// otherwise use the console.log() etc built-in formatting
return args
}
}
export function logToConsole(priority: LoggerPriority, message: string) {
switch (priority) {
case LoggerPriority.Debug:
if (!isQuiet) console.debug(wrapColor(34, message))
break
case LoggerPriority.Log:
console.log(wrapColor(96, message))
break
case LoggerPriority.Warning:
console.warn(wrapColor(93, message))
break
case LoggerPriority.Error:
console.error(wrapColor(91, message))
break
}
}
export let isVerbose = 0
export let isQuiet = false
export let isInteractive = !Boolean(process.env.CI)
export function incVerbose() {
isVerbose++
verboseLog(`verbose level: ${isVerbose}`)
}
export function setQuiet(v: boolean) {
isQuiet = v
}
export function setInteractive(v: boolean) {
isInteractive = v
}
export function verboseLog(msg: string) {
if (isVerbose) console.debug(wrapColor(90, msg))
}