Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmarkclements committed Jan 18, 2018
1 parent ff34314 commit b34016a
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 27 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
* added `--silent` flag
* added `--name` flag
* added `--log-output` flag
* `--node` flag renamed to `--node-path`
* added double dash syntax for nesting flags, e.g.: `0x -- node --zero-fill-buffers script.js`
55 changes: 37 additions & 18 deletions cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

const fs = require('fs')
const minimist = require('minimist')
const zeroEks = require('./')
const { join } = require('path')
const zeroEks = require('./')
const { pathTo } = require('./lib/util')
const { version } = require('./package.json')

cmd(process.argv.slice(2))

function cmd (argv) {
var args = minimist(argv, {
stopEarly: true,
'--': true,
number: ['delay'],
boolean: ['open', 'version', 'help', 'quiet', 'silent'],
alias: {
Expand All @@ -20,13 +23,14 @@ function cmd (argv) {
'output-dir': 'D',
version: 'v',
nodeOptions: 'node-options',
nodePath: 'node-path',
help: 'h',
gen: 'g',
jsonStacks: 'json-stacks',
logOutput: 'log-output'
},
default: {
node: false,
nodePath: false,
name: 'flamegraph',
nodeOptions: [],
delay: 300
Expand All @@ -39,36 +43,51 @@ function cmd (argv) {
args.nodeOptions = args.nodeOptions.split(' ')
}

const nodeFlagsPresent = (args._[0][0] === '-')
if (nodeFlagsPresent) {
const scriptIx = args._.findIndex(([c0]) => c0 !== '-')

args.nodeOptions = [
...args.nodeOptions,
...args._.splice(0, scriptIx)
]
}


if (args.help) {
console.log('\n')
console.log('0x ' + version)
return fs.createReadStream(join(__dirname, 'usage.txt')).pipe(process.stdout)
}

if (args.logOutput && args.logOutput.toLowerCase() === 'stdout') {
args.io = { logStream: process.stdout }
}

if (args.gen) return zeroEks.stacksToFlamegraph(args, (err) => {
if (err) throw err
process.exit()
})

args.script = args._
const dashDash = args['--']
if (dashDash[0] && dashDash[0][0] === '-') {
console.error('0x: The node binary must immediately follow double dash (--)')
console.error(' e.g. 0x -- node script.js')
process.exit(1)
}

if (args.logOutput && args.logOutput.toLowerCase() === 'stdout') {
args.io = { logStream: process.stdout }
if (dashDash[0] && dashDash[0][0] !== 'node' && args.nodePath === false) {
args.nodePath = dashDash[0]
}
dashDash.shift() // rm node path now

const nodeFlagsPresent = dashDash.length > 0

if (nodeFlagsPresent) {
const scriptIx = dashDash.findIndex(([c0]) => c0 !== '-')
args.nodeOptions = [
...args.nodeOptions,
...dashDash.splice(0, scriptIx)
]
args.script = dashDash
} else {
args.script = args._
}

zeroEks(args, args.node, (err) => {
if (err) throw err
zeroEks(args, args.nodePath, (err) => {
if (err) {
console.error('0x: FATAL', err.stack)
process.exit(err.code || 1)
}
process.exit()
})
}
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

const { sun, linux, windows } = require('./platform')
const { execSync } = require('child_process')
const { EventEmitter } = require('events')
const once = require('once')

Expand Down
4 changes: 3 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,12 @@ function tidy (args) {
}

function pathTo ({ee}, bin) {
if (!ee) bin = arguments[0]
if (fs.existsSync(bin)) { return bin }
var path
try { path = which.sync(bin) } catch (e) {
ee.emit('error', Error('0x: Cannot find ' + bin + ' on your system!'))
if (ee) ee.emit('error', Error('0x: Cannot find ' + bin + ' on your system!'))
else throw Error('0x: Cannot find ' + bin + ' on your system!')
}
return path
}
Expand Down
2 changes: 1 addition & 1 deletion platform/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function linux (args, sudo, binary) {
}).on('exit', function (code) {
if (code !== null && code !== 0 && code !== 143 && code !== 130) {
tidy(args)
const err = Error('tracing subprocess error, code: ' + code)
const err = Error('0x: Tracing subprocess error, code: ' + code)
err.code = code
ee.emit('error', err, code)
return
Expand Down
8 changes: 4 additions & 4 deletions platform/sun.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function sun (args, sudo, binary) {
}).on('exit', function (code) {
if (code !== 0) {
tidy(args)
const err = Error('target subprocess error, code: ' + code)
const err = Error('0x Target subprocess error, code: ' + code)
err.code = code
ee.emit('error', err, code)
return
Expand Down Expand Up @@ -104,7 +104,7 @@ function sun (args, sudo, binary) {
debug('Profiling not begun')
log('No stacks, profiling had not begun\n')
tidy(args)
ee.emit('error', Error('Profiling not begun'))
ee.emit('error', Error('0x: Profiling not begun'))
return
}

Expand All @@ -129,7 +129,7 @@ function sun (args, sudo, binary) {
status('Unable to find map file!\n')
debug('Unable to find map file after multiple attempts')
tidy(args)
ee.emit('error', Error('Unable to find map file'))
ee.emit('error', Error('0x: Unable to find map file'))
return
}
return
Expand All @@ -148,7 +148,7 @@ function sun (args, sudo, binary) {
status('Unable to find map file!\n')
debug('Unable to find map file after multiple attempts')
tidy(args)
ee.emit('error', Error('Unable to find map file'))
ee.emit('error', Error('0x: Unable to find map file'))
return
}
pump(
Expand Down
6 changes: 3 additions & 3 deletions usage.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

0x [flags] -- [nodeFlags] file.js -- [scriptFlags]
0x [flags] -- (node) [nodeFlags] file.js -- [scriptFlags]

--name The name of the HTML file, without the .html extension
Can be set to - to write HTML to STDOUT
Expand All @@ -8,10 +8,10 @@
-o Automatically open after finishing
Default: false

--node=PATH Path to the node executable to use
--node-path Path to the node executable to use
Default: false

--node-options=ARGS List of flags to pass to Node, can be used
--node-options List of flags to pass to Node, can be used
instead of the double dash suffix (--)

--delay | -d Delay before tracing begins, allows us to ignore
Expand Down

0 comments on commit b34016a

Please sign in to comment.