-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
38 lines (33 loc) · 1.22 KB
/
cli.js
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
#!/usr/bin/env node
var fs = require('fs'),
convert = require('./conv'),
app = require('commander'),
react = require('react-tools');
app
.version('0.3.0')
.usage('-j <file> [options]')
.option('-j, --js <path>', 'Input js file path')
.option('-x, --html [path]', 'Input html file path, default is js file path but with .html extension')
.option('-o, --out [path]', 'Output file path, default is js file path but with .jsx extension')
.option('-r, --target [file|stdout]', 'Type of output: file (default) or stdout')
.option('-t, --type [jsx|js]', 'Type of output file, default is jsx. If you will specify js then jsx code will be converted to js with react-tools')
.parse(process.argv);
var fileJs = app.js,
fileHtml = app.html || fileJs.replace(/\.js$/, '.html'),
fileJsx = app.out || fileJs.replace(/\.js$/, '.jsx'),
js = fs.readFileSync(fileJs).toString(),
html = fs.readFileSync(fileHtml).toString();
convert(js, html, function (err, jsx) {
if (err) {
throw err;
}
if (app.type === 'js') {
jsx = react.transform(jsx);
}
if (app.target === 'stdout') {
console.log(jsx);
}
else {
fs.writeFileSync(fileJsx, jsx);
}
});