forked from davidmarkclements/0x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.js
executable file
Β·164 lines (142 loc) Β· 4.5 KB
/
cmd.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env node
const fs = require('fs')
const minimist = require('minimist')
const { join, isAbsolute, relative } = require('path')
const zeroEks = require('./')
const ajv = require('ajv')()
const { pathTo } = require('./lib/util')
const { version } = require('./package.json')
const schema = require('./cli-schema.json')
const validate = ajv.compile(schema)
const defaultBanner = `
0x ${version}
0x [flags] -- node [nodeFlags] script.js [scriptFlags]
`
if (module.parent === null) cmd(process.argv.slice(2))
else module.exports = cmd
function cmd (argv, banner = defaultBanner) {
var args = minimist(argv, {
stopEarly: true,
'--': true,
number: ['delay', 'phase'],
boolean: [
'open', 'version', 'help', 'quiet',
'silent', 'jsonStacks', 'svg', 'traceInfo',
'collectOnly', 'timestampProfiles', 'profViz'
],
alias: {
silent: 's',
quiet: 'q',
open: 'o',
delay: 'd',
'output-dir': 'outputDir',
D: 'outputDir',
'output-html': 'outputHtml',
F: 'outputHtml',
version: 'v',
help: 'h',
gen: 'g',
langs: 'l',
tiers: 't',
timestampProfiles: 'timestamp-profiles',
traceInfo: 'trace-info',
jsonStacks: 'json-stacks',
logOutput: 'log-output',
visualizeOnly: 'visualize-only',
collectOnly: 'collect-only',
profViz: 'prof-viz'
},
default: {
delay: 0,
phase: 2
}
})
args.name = args.name || (args.gen ? '-' : 'flamegraph')
if (ajv.validate(schema, args) === false) {
const [{keyword, dataPath, params, message}] = ajv.errors
if (keyword === 'type') {
const flag = dataPath.substr(
1,
dataPath[dataPath.length -1] === ']' ?
dataPath.length - 2 :
dataPath.length -1
)
const dashPrefix = flag.length === 1 ? '-' : '--'
console.error(`\n 0x: the ${dashPrefix}${flag} option ${message}\n`)
}
if (keyword === 'additionalProperties') {
const flag = params.additionalProperty
const dashPrefix = flag.length === 1 ? '-' : '--'
console.error(`\n 0x: ${dashPrefix}${flag} is not a recognized flag\n`)
}
process.exit(1)
}
if (args.collectOnly && args.visualizeOnly) {
console.error('\n 0x: --collect-only and --visualize-only cannot be used together')
process.exit(1)
}
if (args.gen && args.visualizeOnly) {
console.error('\n 0x: --gen and --visualize-only cannot be used together')
process.exit(1)
}
args.workingDir = process.cwd()
if (args.version) return console.log('0x ' + version)
if (args.help || argv.length === 0) {
process.stdout.write(banner)
return fs.createReadStream(join(__dirname, 'usage.txt')).pipe(process.stdout)
}
if (args.logOutput && args.logOutput.toLowerCase() === 'stdout') {
args.io = { logStream: process.stdout }
}
if (args.visualizeOnly) {
try {
const { visualizeOnly } = args
const dir = isAbsolute(visualizeOnly) ?
relative(args.workingDir, visualizeOnly) :
visualizeOnly
const ls = fs.readdirSync(dir)
const rx = /^stacks\.(.*)\.out/
const stacks = ls.find((f) => rx.test(f))
if (!stacks) {
console.error('\n 0x: Invalid data path provided to --visualize-only (no stacks file)')
process.exit(1)
}
args.pid = rx.exec(stacks)[1]
args.gen = join(dir, stacks)
} catch (e) {
if (e.code === 'ENOENT') {
console.error('\n 0x: Invalid data path provided to --visualize-only (unable to access/does not exist)')
process.exit(1)
} else if (e.code === 'ENOTIDR') {
console.error('\n 0x: Invalid data path provided to --visualize-only (not a directory)')
process.exit(1)
} else throw e
}
}
if (args.gen) return zeroEks.stacksToFlamegraph(args, (err) => {
if (err) throw err
process.exit()
})
const dashDash = args['--']
if (dashDash[0] && dashDash[0][0] === '-') {
console.error('0x: The node binary must immediately follow double dash (--)')
console.error(' 0x [flags] -- node [nodeFlags] script.js [scriptFlags]')
process.exit(1)
}
var binary = false
if (dashDash[0]) {
if (dashDash[0][0] !== 'node') binary = dashDash[0]
dashDash.shift()
args.argv = dashDash
} else {
args.argv = args._
}
args.title = args.title || 'node ' + args.argv.join(' ')
zeroEks(args, binary, (err) => {
if (err) {
console.error('0x: FATAL', err.stack)
process.exit(err.code || 1)
}
process.exit()
})
}