-
Notifications
You must be signed in to change notification settings - Fork 150
/
bin.js
112 lines (100 loc) · 2.85 KB
/
bin.js
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const help = require('help-me')({
dir: path.join(__dirname, 'help'),
ext: '.txt'
})
const pump = require('pump')
const sjp = require('secure-json-parse')
const JoyCon = require('joycon')
const stripJsonComments = require('strip-json-comments')
const build = require('./')
const CONSTANTS = require('./lib/constants')
const { isObject } = require('./lib/utils')
const minimist = require('minimist')
const parseJSON = input => {
return sjp.parse(stripJsonComments(input), { protoAction: 'remove' })
}
const joycon = new JoyCon({
parseJSON,
files: [
'pino-pretty.config.cjs',
'pino-pretty.config.js',
'.pino-prettyrc',
'.pino-prettyrc.json'
],
stopDir: path.dirname(process.cwd())
})
const cmd = minimist(process.argv.slice(2))
if (cmd.h || cmd.help) {
help.toStdout()
} else {
const DEFAULT_VALUE = '\0default'
let opts = minimist(process.argv, {
alias: {
colorize: 'c',
colorizeObjects: 'C',
crlf: 'f',
errorProps: 'e',
levelFirst: 'l',
minimumLevel: 'L',
customLevels: 'x',
customColors: 'X',
useOnlyCustomProps: 'U',
errorLikeObjectKeys: 'k',
messageKey: 'm',
levelKey: CONSTANTS.LEVEL_KEY,
levelLabel: 'b',
messageFormat: 'o',
timestampKey: 'a',
translateTime: 't',
ignore: 'i',
include: 'I',
hideObject: 'H',
singleLine: 'S'
},
default: {
messageKey: DEFAULT_VALUE,
minimumLevel: DEFAULT_VALUE,
levelKey: DEFAULT_VALUE,
timestampKey: DEFAULT_VALUE
}
})
// Remove default values
opts = filter(opts, value => value !== DEFAULT_VALUE)
const config = loadConfig(opts.config)
// Override config with cli options
opts = Object.assign({}, config, opts)
// set defaults
opts.errorLikeObjectKeys = opts.errorLikeObjectKeys || 'err,error'
opts.errorProps = opts.errorProps || ''
const res = build(opts)
pump(process.stdin, res)
// https://github.com/pinojs/pino/pull/358
/* istanbul ignore next */
if (!process.stdin.isTTY && !fs.fstatSync(process.stdin.fd).isFile()) {
process.once('SIGINT', function noOp () {})
}
function loadConfig (configPath) {
const files = configPath ? [path.resolve(configPath)] : undefined
const result = joycon.loadSync(files)
if (result.path && !isObject(result.data)) {
configPath = configPath || path.basename(result.path)
throw new Error(`Invalid runtime configuration file: ${configPath}`)
}
if (configPath && !result.data) {
throw new Error(`Failed to load runtime configuration file: ${configPath}`)
}
return result.data
}
function filter (obj, cb) {
return Object.keys(obj).reduce((acc, key) => {
const value = obj[key]
if (cb(value, key)) {
acc[key] = value
}
return acc
}, {})
}
}