-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·82 lines (75 loc) · 1.95 KB
/
Copy pathcli.js
File metadata and controls
executable file
·82 lines (75 loc) · 1.95 KB
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
#!/usr/bin/env node
const Node = {
child: require('child_process'),
fs: require('fs'),
path: require('path'),
process: process
};
const Waterfall = require('./index.js');
var source = undefined;
var target = undefined;
const args = Node.process.argv.slice(2);
const options = { barcolor: [], exclude: [], unit: 'ms' };
var valid = true;
for (var index = 0; index < args.length; index++) {
var arg = args[index];
if (/^--/.test(arg)) {
var parts = arg.split('=');
if (parts.length != 2) {
valid = false;
break;
}
var key = parts[0].toLowerCase().replace(/[_-]+/g, '');
var value = parts[1];
if (key === 'barcolor') {
options.barcolor.push(value);
} else if (key === 'exclude') {
options.exclude.push(value);
} else {
valid = false;
break;
}
} else if (source === undefined) {
source = arg;
} else if (target === undefined) {
target = arg;
} else {
valid = false;
break;
}
}
if (!source || !valid) {
return console.error(
'usage: waterfall [--BAR-COLOR=hex] [--BAR-COLOR=label prefix:hex]\n' +
' [--EXCLUDE=label prefix] ' +
' <data-file> [png-file]'
);
}
if (!target) {
target = source;
let parts = target.split('.');
let extension = parts.pop();
if (parts.length > 0 && parts[0].length > 0) {
target = target.split('.').slice(0, -1).join('.');
}
target += '.png';
}
if (!/\.png$/.test(target)) {
return console.error(
'<png-file> does not have a ".png" extension: ' + target
);
}
if (!Node.fs.existsSync(source)) {
return console.error('<data-file> does not exist: ' + source);
}
const buffer = Node.fs.readFileSync(source);
const png = Waterfall.chart(buffer, options);
Node.fs.writeFileSync(target, png);
// Perform platform and path safety checks, and degrade gracefully.
if (
Node.process.platform === 'darwin' &&
!/"/.test(target) &&
!/^-/.test(target)
) {
Node.child.execSync('open "' + target + '"');
}