38f43df14cf35baf1cdf4427d19a9474848aea36.svn-base
8.03 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/env node
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var fs = require('fs');
var path = require('path');
var flowRemoveTypes = require('./index');
var usage = 'Usage: flow-remove-types [options] [sources] \n' +
'\nOptions:\n' +
' -h, --help Show this message\n' +
' -v, --version Prints the current version of flow-remove-types\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' +
' -a, --all Transform all files, not just those with a @flow comment\n' +
' -p, --pretty Remove flow types without replacing with spaces, \n' +
' producing prettier output but may require using source maps\n' +
' --ignore-uninitialized-fields\n' +
' Removes uninitialized class fields (`foo;`, `foo: string;`)\n' +
' completely rather than only removing the type. THIS IS NOT\n' +
' SPEC COMPLIANT! Instead, use `declare foo: string;` for\n' +
' type-only fields.\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' +
' flow-remove-types --out-file output.js input.js\n' +
'\nTransform many files:\n' +
' flow-remove-types --out-dir out/ input1.js input2.js\n' +
'\nTransform files in directory:\n' +
' flow-remove-types --out-dir out/ indir/\n' +
'\nTransform files with source maps:\n' +
' flow-remove-types --out-dir out/ indir/ --sourcemaps\n' +
'\nTransform files with inline source maps:\n' +
' flow-remove-types --out-dir out/ indir/ --sourcemaps inline\n' +
'\nTransform stdin:\n' +
' cat input.js | flow-remove-types > 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', '.mjs', '.cjs', '.jsx', '.flow', '.es6' ];
var outDir;
var outFile;
var all;
var pretty;
var ignoreUninitializedFields;
var sourceMaps;
var inlineSourceMaps;
var quiet;
var sources = [];
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 === '-a' || arg === '--all') {
all = true;
} else if (arg === '-p' || arg === '--pretty') {
pretty = true;
} else if (arg === '--ignore-uninitialized-fields') {
ignoreUninitializedFields = true;
} 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.join(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 = [content];
}
code += '\n//# sourceMappingURL=' + (inlineSourceMaps ?
'data:application/json;charset=utf-8;base64,' + btoa(JSON.stringify(map)) :
path.basename(outFile) + '.map'
) + '\n';
}
if (outFile) {
fs.writeFileSync(outFile, code);
info(fileName + '\n \u21B3 \033[32m' + outFile + '\033[0m\n');
if (sourceMaps && !inlineSourceMaps) {
var mapOutFile = outFile + '.map';
fs.writeFileSync(mapOutFile, JSON.stringify(map) + '\n');
info('\033[2m \u21B3 \033[32m' + mapOutFile + '\033[0m\n');
}
} else {
process.stdout.write(code);
}
}
function btoa(str) {
// There are 5.x versions of Node that have `Buffer.from` but don't have the
// `Buffer.from(string)` overload, so check for other new methods to be sure.
return (Buffer.from && Buffer.alloc && Buffer.allocUnsafe
? Buffer.from(str)
: new Buffer(str)
).toString('base64');
}
function transformSource(content, filepath) {
try {
return flowRemoveTypes(content, {
all: all,
pretty: pretty,
ignoreUninitializedFields: ignoreUninitializedFields,
});
} 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;
}
}