UNPKG

1.86 kBJavaScriptView Raw
1#!/usr/bin/env node
2
3var co = require('co');
4var chronicle = require('../lib');
5var fs = require('mz/fs');
6var is = require('is_js');
7var pkg = require('../package');
8var program = require('commander');
9var stdin = require('get-stdin');
10var UpdateNotifier = require('update-notifier');
11
12UpdateNotifier({
13 pkg,
14 updateCheckInterval: 1000 * 60 * 60 // once an hour
15}).notify({defer: false});
16
17var bundle = (entry, program) => chronicle.bundle(entry, program.opts());
18/**
19 * @param report {String}
20 * @param program
21 */
22var 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 // output: can be any type => string buffer number date boolean | array object
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
50program.version(pkg.version)
51 .description(pkg.description);
52program.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);
57program.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);
62program.parse(process.argv);