forked from davidmarkclements/0x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd.js
executable file
Β·138 lines (119 loc) Β· 3.61 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
#!/usr/bin/env node
const fs = require('fs')
const { join } = require('path')
const minimist = require('minimist')
const semver = require('semver')
const debug = require('debug')('0x')
const sll = require('single-line-log')
const launch = require('opn')
const hasUnicode = require('has-unicode')()
const zeroEks = semver.lt(process.version, '8.5.0') === true ? () => {} : require('./')
const { version } = require('./package.json')
const defaultBanner = `
0x ${version}
0x [flags] -- node [nodeFlags] script.js [scriptFlags]
`
if (module.parent === null) {
cmd(process.argv.slice(2)).catch((err) => {
err.message = hasUnicode ? `\nπ« ${err.message}` : `\n${err.message}`
console.error(err)
debug(err)
process.exit(err.code || 1)
})
} else {
module.exports = cmd
}
async function cmd (argv, banner = defaultBanner) {
if (semver.lt(process.version, '8.5.0') === true) {
throw Error(
`Node version unsupported. Current Node version is ${process.version}\n` +
'Support extends from Node 8.5.0 and above.\n\n' +
' npm i -g 0x@3 for Node 6.x.x β 8.4.0\n' +
' npm i -g 0x@2 for Node 4\n'
)
}
const args = minimist(argv, {
stopEarly: true,
'--': true,
boolean: [
'open', 'version', 'help', 'quiet',
'silent', 'treeDebug', 'kernelTracingDebug',
'kernelTracing', 'collectOnly', 'writeTicks'
],
alias: {
silent: 's',
quiet: 'q',
open: 'o',
'output-dir': 'outputDir',
D: 'outputDir',
'output-html': 'outputHtml',
F: 'outputHtml',
version: 'v',
help: 'h',
visualizeOnly: 'visualize-only',
visualizeCpuProfile: 'visualize-cpu-profile',
collectOnly: 'collect-only',
collectDelay: 'collect-delay',
kernelTracing: 'kernel-tracing',
kernelTracingDebug: 'kernel-tracing-debug',
treeDebug: 'tree-debug',
writeTicks: 'write-ticks',
onPort: 'on-port',
P: 'onPort'
}
})
if (args.help || argv.length === 0) {
process.stdout.write(banner)
return fs.createReadStream(join(__dirname, 'usage.txt')).pipe(process.stdout)
}
if (args.version) {
return console.log(`0x ${version}`)
}
const status = createStatus(args)
const { pathToNodeBinary, subprocessArgv } = parseSubprocessCommand(args)
args.workingDir = process.cwd()
args.status = status
args.argv = subprocessArgv
args.pathToNodeBinary = pathToNodeBinary
if (args.visualizeOnly) {
status(`Creating flamegraph from ${args.visualizeOnly}`)
}
if (args.visualizeCpuProfile) {
status(`Creating flamegraph from v8 profile ${args.visualizeCpuProfile}`)
}
const assetPath = await zeroEks(args)
if (args.collectOnly) {
status(`Stats collected in folder file://${assetPath}\n`)
} else {
status(`Flamegraph generated in\n${assetPath}\n`)
if (args.open) {
launch(assetPath, { wait: false })
}
}
return assetPath
}
function parseSubprocessCommand (args) {
const dashDash = args['--']
let pathToNodeBinary = process.argv[0]
let subprocessArgv = args._
if (dashDash.length !== 0) {
const dashEntry = dashDash[0][0]
if (dashEntry === '-') {
throw Error(
'The node binary must immediately follow double dash (--)\n' +
'0x [flags] -- node [nodeFlags] script.js [scriptFlags]'
)
}
pathToNodeBinary = dashDash[0]
dashDash.shift()
subprocessArgv = dashDash
}
return { pathToNodeBinary, subprocessArgv }
}
function createStatus ({ silent, quiet }) {
if (quiet || silent) {
return () => {}
}
const status = sll(process.stderr)
return hasUnicode ? (s) => status(`π₯ ${s}`) : status
}