UNPKG

4.49 kBJavaScriptView Raw
1var PATH = require('path'),
2 FS = require('fs'),
3 U = require('./util');
4
5module.exports = require('coa').Cmd()
6 .name(process.argv[1])
7 .title('Borschik. Extendable builder for text-based file formats.')
8 .helpful()
9 .opt()
10 .name('version') .title('Version')
11 .short('v').long('version')
12 .flag()
13 .only()
14 .act(function() { return JSON.parse(FS.readFileSync(PATH.resolve(__dirname, '..', 'package.json'))).version })
15 .end()
16 .opt()
17 .name('tech') .title('Technology')
18 .short('t').long('tech')
19 .end()
20 .opt()
21 // Give ability for external technologies to has its own options without conflicts with borschik
22 .name('techOptions') .title('Additional options for tech in JSON format')
23 .short('to').long('tech-options')
24 .def({})
25 .val(function(v) {
26 if (typeof v === 'string') {
27 try {
28 v = JSON.parse(v);
29 } catch(e) {
30 return this.reject('"techOptions" must be valid JSON');
31 }
32 }
33 // Make techOptions to be {} in case of undefined or other falsy value
34 return v || {};
35 })
36 .end()
37 .opt()
38 .name('input').title('Input path')
39 .short('i').long('input')
40 .act(function(opts) {
41 if (opts.inputString) {
42 return this.reject("Can't process 'input' and 'inputString' options at the same time");
43 }
44 })
45 .end()
46 .opt()
47 .name('inputString').title('Input string')
48 .act(function(opts) {
49 if (opts.input) {
50 return this.reject("Can't process 'input' and 'inputString' options at the same time");
51 }
52 if (!opts.basePath) {
53 return this.reject("Can't process 'inputString' without 'basePath' option");
54 }
55 })
56 .end()
57 .opt()
58 .name('basePath').title('Base path for text input')
59 .end()
60 .opt()
61 .name('output').title('Output path')
62 .short('o').long('output')
63 .output()
64 .req()
65 .end()
66 .opt()
67 .name('freeze').title('Freeze links to static files (default: yes)')
68 .short('f').long('freeze')
69 .def(true)
70 .val(function(v) {
71 return U.stringToBoolean(v, true);
72 })
73 .end()
74 .opt()
75 .name('minimize').title('Minimize resulting content (default: yes)')
76 .short('m').long('minimize')
77 .def(true)
78 .val(function(v) {
79 return U.stringToBoolean(v, true);
80 })
81 .end()
82 .opt()
83 .name('comments').title('Wrap included files with comments (default: yes)')
84 .short('c').long('comments')
85 .def(true)
86 .val(function(v) {
87 return U.stringToBoolean(v, true);
88 })
89 .end()
90 .opt()
91 .name('warnings').title('Print warning about duplicates files (default: no)')
92 .long('warnings')
93 .def(false)
94 .val(function(v) {
95 return U.stringToBoolean(v, false);
96 })
97 .end()
98 // borschik freeze
99 .cmd()
100 .name('freeze')
101 .title('Freeze all files in dirs according to .borschik config')
102 .helpful()
103 .opt()
104 .name('input').title('Input path (default: .).')
105 .short('i').long('input')
106 .def('.')
107 .end()
108 .opt()
109 .name('output').title('Output path for resulting JSON')
110 .short('o').long('output')
111 .output()
112 .end()
113 .opt()
114 .name('minimize').title('Minimize resulting JSON (default: yes)')
115 .short('m').long('minimize')
116 .def(true)
117 .val(function(v) {
118 return U.stringToBoolean(v, true);
119 })
120 .end()
121 .act(function(opts) {
122 var result = require('./freeze').freezeAll(opts.input);
123 return U.writeFile(opts.output, opts.minimize? JSON.stringify(result) : JSON.stringify(result, null, 4));
124 })
125 .end()
126 .act(function(opts) {
127 var tech = opts.tech,
128 input = opts.input;
129
130 if (!tech && typeof input === 'string') {
131 tech = PATH.extname(input).substr(1);
132 }
133
134 if (!tech || !tech.Tech) {
135 tech = U.getTech(tech, true);
136 }
137
138 return new (tech.Tech)(opts).process();
139
140 });