UNPKG

2.04 kBPlain TextView Raw
1#!/usr/bin/env node
2var Styledown = require('..'),
3 read = require('read-input');
4
5var args = require('minimist')(process.argv.slice(2), {
6 boolean: ['inline', 'css', 'js', 'conf', 'quiet'],
7 alias: { h: 'help', v: 'version', i: 'inline', o: 'output', q: 'quiet' }
8});
9
10if (args.help) {
11 var cmd = require('path').basename(process.argv[1]);
12 console.log([
13 'Usage:',
14 ' '+cmd+' [options] FILE',
15 ' ... | '+cmd+' [options]',
16 '',
17 'Options:',
18 ' -h, --help print usage information',
19 ' -v, --version show version info and exit',
20 ' -i, --inline force extracts from inline CSS comments (for piping)',
21 ' -o, --output FILE write output a file',
22 '',
23 'Support files:',
24 ' '+cmd+' --conf > config.md',
25 ' '+cmd+' --css > styledown.css',
26 ' '+cmd+' --js > styledown.js',
27 ].join('\n'));
28 process.exit();
29}
30
31if (args.version) {
32 console.log(require('../package.json').version);
33 process.exit();
34}
35
36if (args.js) {
37 print(Styledown.defaults.js());
38 process.exit();
39}
40
41if (args.css) {
42 print(Styledown.defaults.css());
43 process.exit();
44}
45
46if (args.conf) {
47 print(Styledown.defaults.conf());
48 process.exit();
49}
50
51read(args._, function (err, res) {
52 var errors = res.files.filter(function (f) { return !! f.error; });
53
54 if (errors.length) {
55 errors.forEach(function (f) {
56 console.error((f.name || 'stdin') + ':', f.error.message);
57 });
58 process.exit(8);
59 }
60
61 var html;
62
63 var ms = measure(function () {
64 html = Styledown.parse(res.files, {
65 inline: args.inline
66 }) + "\n";
67 });
68
69 print(html, ms);
70});
71
72function print (html, ms) {
73 if (args.output) {
74 require('fs').writeFileSync(args.output, html);
75
76 if (!args.quiet) {
77 var tip = '' + args.output;
78 if (ms) tip += " [" + ms + "ms]";
79 process.stderr.write(tip + "\n");
80 }
81 } else {
82 process.stdout.write(html);
83 }
84}
85
86function measure (fn) {
87 var d = new Date();
88 fn();
89 return new Date() - d;
90}