This repository has been archived by the owner on May 20, 2022. It is now read-only.
/
nativejsx
executable file
·100 lines (87 loc) · 2.84 KB
/
nativejsx
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var glob = require('glob');
var program = require('commander');
var nativejsx = require('../source/nativejsx.js');
function compile(files, outputDirectory) {
files.forEach(function(file) {
outputDirectory = outputDirectory || path.dirname(file);
if (glob.hasMagic(file)) {
compile(glob.sync(file), outputDirectory);
} else {
var jsx = nativejsx.parseSync(file);
var extension = path.extname(file);
var filename = path.basename(file, extension);
var outputFilePath = path.join(outputDirectory, filename + '.js');
fs.writeFileSync(outputFilePath, jsx, 'utf8');
}
});
}
program.
version('0.1.0').
description('A simple command line utility for nativejsx.').
option('-o, --output <directory>', 'The directory to place compiled JSX files').
on('--help', function() {
console.log(' Examples:');
console.log();
console.log(' $ nativejsx path/awesome.jsx --output compiled/');
console.log(' $ nativejsx path-to-jsx/**/*.jsx --output compiled/');
console.log();
console.log(' # Output goes to the location of the current file being processed.');
console.log(' $ nativejsx path-to-jsx/**/*.jsx');
console.log();
console.log(' # All jsx is processed at once and compiled into a single file.');
console.log(' $ cat path-to-jsx/**/*.jsx | nativejsx > large-compiled-file.js');
console.log();
}).
parse(process.argv);
var hasPaths = Array.isArray(program.args) && program.args.length > 0;
var hasOutputDirectory = typeof program.output === 'string' && program.output.length > 0;
if (hasPaths && hasOutputDirectory) {
var outputDirectory = path.join(process.cwd(), program.output);
var exists = fs.existsSync(outputDirectory);
if (exists) {
var stats = fs.statSync(outputDirectory);
var isNotDirectory = !stats.isDirectory();
if (isNotDirectory) {
console.error('Output path already exists as a non-directory file.');
program.help();
process.exit(1);
} else {
compile(program.args, outputDirectory);
}
} else {
try {
fs.mkdir(outputDirectory);
compile(program.args, outputDirectory);
} catch (error) {
console.error(error);
console.error('Could not create directory, %s.', program.output);
process.exit(1);
}
}
} else if (hasPaths) {
compile(program.args);
} else {
var stdin = '';
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk != null) {
stdin += chunk.toString();
}
process.nextTick(function() {
process.stdin.end();
});
});
process.stdin.on('finish', function() {
if (stdin) {
process.stdout.write(
nativejsx.transpile(stdin)
);
} else {
program.help();
}
});
}