1 | #!/usr/bin/env node
|
2 |
|
3 | var co = require('co');
|
4 | var chronicle = require('../lib');
|
5 | var fs = require('mz/fs');
|
6 | var is = require('is_js');
|
7 | var pkg = require('../package');
|
8 | var program = require('commander');
|
9 | var stdin = require('get-stdin');
|
10 | var UpdateNotifier = require('update-notifier');
|
11 |
|
12 | UpdateNotifier({
|
13 | pkg,
|
14 | updateCheckInterval: 1000 * 60 * 60
|
15 | }).notify({defer: false});
|
16 |
|
17 | var bundle = (entry, program) => chronicle.bundle(entry, program.opts());
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var run = co.wrap(function * (report, program) {
|
23 | try {
|
24 | var options = program.opts();
|
25 | if (!report) report = yield stdin();
|
26 | var parameters;
|
27 | try {
|
28 | parameters = (options.parameters ? JSON.parse(options.parameters) : {});
|
29 | }
|
30 | catch (error) {
|
31 | throw new TypeError('--parameters must be JSON parseable.');
|
32 | }
|
33 | var press = chronicle.Press.create();
|
34 | var output = yield press.run(report, parameters);
|
35 |
|
36 | if (is.array(output) || is.object(output)) output = JSON.stringify(output);
|
37 | if (options.output) {
|
38 | yield fs.writeFile(options.output, output);
|
39 | }
|
40 | else {
|
41 | console.log(output);
|
42 | }
|
43 | }
|
44 | catch (error) {
|
45 | console.error(error.stack);
|
46 | process.exit(1);
|
47 | }
|
48 | });
|
49 |
|
50 | program.version(pkg.version)
|
51 | .description(pkg.description);
|
52 | program.command('bundle [entry]')
|
53 | .description('Bundle a module and its dependencies into a single file.')
|
54 | .option('-o, --output <output>', 'output file path')
|
55 | .option('-w, --watch', 'press when dependencies change')
|
56 | .action(bundle);
|
57 | program.command('run [definition]')
|
58 | .description('Run a report through the press.')
|
59 | .option('-p, --parameters <parameters>', 'stringified report parameters object')
|
60 | .option('-o, --output <filename>', 'filename of file to write output to')
|
61 | .action(run);
|
62 | program.parse(process.argv);
|