forked from davidmarkclements/0x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.js
executable file
Β·115 lines (100 loc) Β· 3.29 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
#!/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) => {
console.error(hasUnicode ? `\nπ« ${err.message}` : `\n${err.message}`)
debug(err)
process.exit(err.code || 1)
})
} else module.exports = cmd
async function cmd (argv, banner = defaultBanner) {
var args = minimist(argv, {
stopEarly: true,
'--': true,
boolean: [
'open', 'version', 'help', 'quiet',
'silent', 'treeDebug', 'kernelTracingDebug',
'kernelTracing', 'collectOnly'
],
alias: {
silent: 's',
quiet: 'q',
open: 'o',
'output-dir': 'outputDir',
D: 'outputDir',
'output-html': 'outputHtml',
F: 'outputHtml',
version: 'v',
help: 'h',
visualizeOnly: 'visualize-only',
collectOnly: 'collect-only',
kernelTracing: 'kernel-tracing',
kernelTracingDebug: 'kernel-tracing-debug',
treeDebug: 'tree-debug',
onPort: 'on-port',
P: 'onPort'
}
})
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'
)
}
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)
args.workingDir = process.cwd()
args.status = status
const { pathToNodeBinary, subprocessArgv } = parseSubprocessCommand(args)
args.argv = subprocessArgv
args.pathToNodeBinary = pathToNodeBinary
if (args.visualizeOnly) status(`Creating flamegraph from ${args.visualizeOnly}`)
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['--']
if (dashDash[0] && dashDash[0][0] === '-') {
throw Error(`The node binary must immediately follow double dash (--)
0x [flags] -- node [nodeFlags] script.js [scriptFlags]
`)
}
var pathToNodeBinary = false
var subprocessArgv = args._
if (dashDash[0]) {
if (dashDash[0][0] !== 'node') pathToNodeBinary = dashDash[0]
dashDash.shift()
subprocessArgv = dashDash
}
return { pathToNodeBinary, subprocessArgv }
}
function createStatus ({silent, quiet}) {
const statusStream = process.stderr
if (quiet || silent) return () => {}
const status = sll(statusStream)
return hasUnicode ? (s) => status(`π₯ ${s}`) : status
}