-
Notifications
You must be signed in to change notification settings - Fork 35
/
async-to-gen
executable file
·235 lines (205 loc) · 6.48 KB
/
async-to-gen
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var asyncToGen = require('./index');
var usage = 'Usage: async-to-gen [options] [sources] \n' +
'\nOptions:\n' +
' -h, --help Show this message\n' +
' -v, --version Print the current version of async-to-gen\n' +
' -i, --ignore Paths to ignore, Regular Expression\n' +
' -x, --extensions File extensions to transform\n' +
' -o, --out-file The file path to write transformed file to\n' +
' -d, --out-dir The directory path to write transformed files within\n' +
' -m, --sourcemaps Also output source map files. Optionally pass "inline"\n' +
' -q, --quiet Does not produce any output concerning successful progress.\n' +
'\nExamples:\n' +
'\nTransform one file:\n' +
' async-to-gen --out-file output.js input.js\n' +
'\nTransform many files:\n' +
' async-to-gen --out-dir out/ input1.js input2.js\n' +
'\nTransform files in directory:\n' +
' async-to-gen --out-dir out/ indir/\n' +
'\nTransform files with source maps:\n' +
' async-to-gen --out-dir out/ indir/ --sourcemaps\n' +
'\nTransform files with inline source maps:\n' +
' async-to-gen --out-dir out/ indir/ --sourcemaps inline\n' +
'\nTransform stdin:\n' +
' cat input.js | async-to-gen > output.js\n';
var _memo = {};
function mkdirp(dirpath) {
if (_memo[dirpath]) {
return;
}
_memo[dirpath] = true;
try {
fs.mkdirSync(dirpath);
} catch (err) {
if (err.code === 'ENOENT') {
mkdirp(path.dirname(dirpath));
fs.mkdirSync(dirpath);
} else {
try {
stat = fs.statSync(dirpath);
} catch (ignored) {
throw err;
}
if (!stat.isDirectory()) {
throw err;
}
}
}
}
// Collect arguments
var ignore = /node_modules/;
var extensions = [ '.js', '.jsx', '.flow', '.es6' ];
var outDir;
var outFile;
var sourceMaps;
var inlineSourceMaps;
var sources = [];
var quiet;
var i = 2;
while (i < process.argv.length) {
var arg = process.argv[i++];
if (arg === '-h' || arg === '--help') {
process.stdout.write(usage);
process.exit(0);
} else if (arg === '-v' || arg === '--version') {
process.stdout.write('v' + require('./package').version);
process.exit(0);
} else if (arg === '-i' || arg === '--ignore') {
ignore = new RegExp(process.argv[i++]);
} else if (arg === '-x' || arg === '--extensions') {
extensions = process.argv[i++].split(',');
} else if (arg === '-o' || arg === '--out-file') {
outFile = process.argv[i++];
} else if (arg === '-d' || arg === '--out-dir') {
outDir = process.argv[i++];
} else if (arg === '-m' || arg === '--sourcemaps') {
sourceMaps = true;
if (process.argv[i] === 'inline') {
inlineSourceMaps = true;
i++;
}
} else if (arg === '-q' || arg === '--quiet') {
quiet = true;
} else {
sources.push(arg);
}
}
function info(msg) {
if (!quiet) {
process.stderr.write(msg);
}
}
function error(msg) {
process.stderr.write('\n\033[31m ' + msg + '\033[0m\n\n');
process.exit(1);
}
// Validate arguments
if (outDir && outFile) {
error('Only specify one of --out-dir or --out-file');
}
if (outDir && sources.length === 0) {
error('Must specify files when providing --out-dir');
}
if (!outDir && !outFile && sourceMaps && !inlineSourceMaps) {
error('Must specify either an output path or inline source maps');
}
// Ensure all sources exist
for (var i = 0; i < sources.length; i++) {
try {
var stat = fs.lstatSync(sources[i]);
if (sources.length > 1 && !stat.isFile()) {
error('Source "' + sources[i] + '" is not a file.');
}
} catch (err) {
error('Source "' + sources[i] + '" does not exist.');
}
}
// Process stdin if no sources were provided
if (sources.length === 0) {
var content = '';
process.stdin.setEncoding('utf-8');
process.stdin.resume();
process.stdin.on('data', function (str) { content += str; });
process.stdin.on('end', function () {
transformAndOutput(content, outFile);
});
return;
}
var isDirSource = sources.length === 1 && fs.statSync(sources[0]).isDirectory();
if ((sources.length > 1 || isDirSource) && !outDir) {
error('Multiple files require providing --out-dir');
}
// Process multiple files
for (var i = 0; i < sources.length; i++) {
var source = sources[i];
var stat = fs.lstatSync(source);
if (stat.isDirectory()) {
var files = fs.readdirSync(source);
for (var j = 0; j < files.length; j++) {
var subSource = path.resolve(source, files[j]);
if (!ignore || !ignore.test(subSource)) {
sources.push(subSource);
}
}
} else if (stat.isFile() && extensions.indexOf(path.extname(source)) !== -1) {
if (outDir) {
outFile = path.join(outDir, isDirSource ? path.relative(sources[0], source) : source);
mkdirp(path.dirname(outFile));
}
var content = fs.readFileSync(source, 'utf8');
transformAndOutput(content, outFile, source);
}
}
function transformAndOutput(content, outFile, source) {
var fileName = source || '<stdin>';
var result = transformSource(content, fileName);
var code = result.toString();
if (sourceMaps) {
var map = result.generateMap();
delete map.file;
map.sources[0] = fileName;
if (source) {
delete map.sourcesContent;
if (outFile) {
map.sources[0] = path.join(path.relative(path.dirname(outFile), path.dirname(source)), path.basename(source));
}
} else {
map.sourcesContent[0] = content;
}
code += '\n//# sourceMappingURL=' +
(inlineSourceMaps ? map.toUrl() : path.basename(outFile) + '.map') + '\n';
}
if (outFile) {
fs.writeFileSync(outFile, code, 'utf8');
info(fileName + '\n \u21B3 \033[32m' + outFile + '\033[0m\n');
if (sourceMaps && !inlineSourceMaps) {
var mapOutFile = outFile + '.map';
fs.writeFileSync(mapOutFile, JSON.stringify(map) + '\n', 'utf8');
info('\033[2m \u21B3 \033[32m' + mapOutFile + '\033[0m\n');
}
} else {
process.stdout.write(code);
}
}
function transformSource(content, filepath) {
try {
return asyncToGen(content);
} catch (error) {
if (error.loc) {
var line = error.loc.line - 1;
var col = error.loc.column;
var text = content.split(/\r\n?|\n|\u2028|\u2029/)[line];
process.stderr.write(
filepath + '\n' +
' \u21B3 \033[31mSyntax Error: ' + error.message + '\033[0m\n' +
' \033[90m' + line + ': \033[0m' +
text.slice(0, col) + '\033[7;31m' + text[col] + '\033[0m' + text.slice(col + 1) + '\n'
);
process.exit(1);
}
throw error;
}
}