-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata
More file actions
executable file
·78 lines (69 loc) · 2.46 KB
/
Copy pathdata
File metadata and controls
executable file
·78 lines (69 loc) · 2.46 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
#!/usr/bin/env node
const path = require('path');
const fs = require('fs');
const clap = require('clap');
const commonOptions = require('../lib/cli');
const getData = require('../lib/data');
// define command
const command = clap.command('data', '[config] [model]')
.option(...commonOptions.config)
.option(...commonOptions.model)
.option(...commonOptions.cache)
.option('--rewrite-cache', 'Ignore existing cache')
.option('--pretty [indent]', 'Pretty print with optionally specified indentation (4 spaces by default)', (value = 4) => Number(value) || false, false)
.option('--background', 'Update/warmup data cache in background, no output')
.option('-o, --output <filename>', 'Output file',
(value = '') => path.resolve(process.cwd(), value)
)
.version(require('../package.json').version)
.action(function(args) {
const configFile = this.values.config || args[0];
const model = this.values.model || args[1];
const outputFile = this.values.output || null;
const cache = this.values.cache || false;
const rewriteCache = this.values.rewriteCache;
const background = this.values.background;
const pretty = this.values.pretty;
const options = {
configFile,
model,
cache,
rewriteCache,
warmup: background,
pretty
};
getData(options).then(({ stream, size, brotli } = {}) => {
if (outputFile) {
stream.pipe(fs.createWriteStream(outputFile));
return;
}
// send data to parent process if run as a child process
if (typeof process.send === 'function') {
// FIXME: temporary solution
if (background) {
process.send('');
return;
}
process.send({ payload: 'stream', size, brotli });
stream.pipe(fs.createWriteStream(null, { fd: 4 }));
return;
}
// otherwise write to stdout
stream.pipe(process.stdout);
}).catch(error => {
console.error(error);
process.exit(2);
});
});
// run command
try {
command.run();
} catch (e) {
// output user frendly message if cli error
if (e instanceof clap.Error) {
console.error(e.message || e);
process.exit(2);
}
// otherwise re-throw exception
throw e;
}