Debian Codegraph @

flow-remove-types

raw
1 #!/usr/bin/env node
2
3 var fs = require('fs');
4 var path = require('path');
5
6 var flowRemoveTypes = require('./index');
7
8 var usage = 'Usage: flow-remove-types [options] [sources] \n' +
9
10 '\nOptions:\n' +
11 ' -h, --help Show this message\n' +
12 ' -v, --version Prints the current version of flow-remove-types\n' +
13 ' -i, --ignore Paths to ignore, Regular Expression\n' +
14 ' -x, --extensions File extensions to transform\n' +
15 ' -o, --out-file The file path to write transformed file to\n' +
16 ' -d, --out-dir The directory path to write transformed files within\n' +
17 ' -a, --all Transform all files, not just those with a @flow comment\n' +
18 ' -p, --pretty Remove flow types without replacing with spaces, \n' +
19 ' producing prettier output but may require using source maps\n' +
20 ' -m, --sourcemaps Also output source map files. Optionally pass "inline"\n' +
21 ' -q, --quiet Does not produce any output concerning successful progress.\n' +
22
23 '\nExamples:\n' +
24
25 '\nTransform one file:\n' +
26 ' flow-remove-types --out-file output.js input.js\n' +
27
28 '\nTransform many files:\n' +
29 ' flow-remove-types --out-dir out/ input1.js input2.js\n' +
30
31 '\nTransform files in directory:\n' +
32 ' flow-remove-types --out-dir out/ indir/\n' +
33
34 '\nTransform files with source maps:\n' +
35 ' flow-remove-types --out-dir out/ indir/ --sourcemaps\n' +
36
37 '\nTransform files with inline source maps:\n' +
38 ' flow-remove-types --out-dir out/ indir/ --sourcemaps inline\n' +
39
40 '\nTransform stdin:\n' +
41 ' cat input.js | flow-remove-types > output.js\n';
42
43 var _memo = {};
44
45 function mkdirp(dirpath) {
46 if (_memo[dirpath]) {
47 return;
48 }
49 _memo[dirpath] = true;
50 try {
51 fs.mkdirSync(dirpath);
52 } catch (err) {
53 if (err.code === 'ENOENT') {
54 mkdirp(path.dirname(dirpath));
55 fs.mkdirSync(dirpath);
56 } else {
57 try {
58 stat = fs.statSync(dirpath);
59 } catch (ignored) {
60 throw err;
61 }
62 if (!stat.isDirectory()) {
63 throw err;
64 }
65 }
66 }
67 }
68
69 // Collect arguments
70 var ignore = /node_modules/;
71 var extensions = [ '.js', '.mjs', '.jsx', '.flow', '.es6' ];
72 var outDir;
73 var outFile;
74 var all;
75 var pretty;
76 var sourceMaps;
77 var inlineSourceMaps;
78 var quiet;
79 var sources = [];
80 var i = 2;
81 while (i < process.argv.length) {
82 var arg = process.argv[i++];
83 if (arg === '-h' || arg === '--help') {
84 process.stdout.write(usage);
85 process.exit(0);
86 } else if (arg === '-v' || arg === '--version') {
87 process.stdout.write('v' + require('./package').version);
88 process.exit(0);
89 } else if (arg === '-i' || arg === '--ignore') {
90 ignore = new RegExp(process.argv[i++]);
91 } else if (arg === '-x' || arg === '--extensions') {
92 extensions = process.argv[i++].split(',');
93 } else if (arg === '-o' || arg === '--out-file') {
94 outFile = process.argv[i++];
95 } else if (arg === '-d' || arg === '--out-dir') {
96 outDir = process.argv[i++];
97 } else if (arg === '-a' || arg === '--all') {
98 all = true;
99 } else if (arg === '-p' || arg === '--pretty') {
100 pretty = true;
101 } else if (arg === '-m' || arg === '--sourcemaps') {
102 sourceMaps = true;
103 if (process.argv[i] === 'inline') {
104 inlineSourceMaps = true;
105 i++;
106 }
107 } else if (arg === '-q' || arg === '--quiet') {
108 quiet = true;
109 } else {
110 sources.push(arg);
111 }
112 }
113
114 function info(msg) {
115 if (!quiet) {
116 process.stderr.write(msg);
117 }
118 }
119
120 function error(msg) {
121 process.stderr.write('\n\033[31m ' + msg + '\033[0m\n\n');
122 process.exit(1);
123 }
124
125 // Validate arguments
126 if (outDir && outFile) {
127 error('Only specify one of --out-dir or --out-file');
128 }
129
130 if (outDir && sources.length === 0) {
131 error('Must specify files when providing --out-dir');
132 }
133
134 if (!outDir && !outFile && sourceMaps && !inlineSourceMaps) {
135 error('Must specify either an output path or inline source maps');
136 }
137
138 // Ensure all sources exist
139 for (var i = 0; i < sources.length; i++) {
140 try {
141 var stat = fs.lstatSync(sources[i]);
142 if (sources.length > 1 && !stat.isFile()) {
143 error('Source "' + sources[i] + '" is not a file.');
144 }
145 } catch (err) {
146 error('Source "' + sources[i] + '" does not exist.');
147 }
148 }
149
150 // Process stdin if no sources were provided
151 if (sources.length === 0) {
152 var content = '';
153 process.stdin.setEncoding('utf-8');
154 process.stdin.resume();
155 process.stdin.on('data', function (str) { content += str; });
156 process.stdin.on('end', function () {
157 transformAndOutput(content, outFile);
158 });
159 return;
160 }
161
162 var isDirSource = sources.length === 1 && fs.statSync(sources[0]).isDirectory();
163
164 if ((sources.length > 1 || isDirSource) && !outDir) {
165 error('Multiple files require providing --out-dir');
166 }
167
168 // Process multiple files
169 for (var i = 0; i < sources.length; i++) {
170 var source = sources[i];
171 var stat = fs.lstatSync(source);
172 if (stat.isDirectory()) {
173 var files = fs.readdirSync(source);
174 for (var j = 0; j < files.length; j++) {
175 var subSource = path.join(source, files[j]);
176 if (!ignore || !ignore.test(subSource)) {
177 sources.push(subSource);
178 }
179 }
180 } else if (stat.isFile() && extensions.indexOf(path.extname(source)) !== -1) {
181 if (outDir) {
182 outFile = path.join(outDir, isDirSource ? path.relative(sources[0], source) : source);
183 mkdirp(path.dirname(outFile));
184 }
185 var content = fs.readFileSync(source, 'utf8');
186 transformAndOutput(content, outFile, source);
187 }
188 }
189
190 function transformAndOutput(content, outFile, source) {
191 var fileName = source || '<stdin>';
192 var result = transformSource(content, fileName);
193 var code = result.toString();
194
195 if (sourceMaps) {
196 var map = result.generateMap();
197 delete map.file;
198 map.sources[0] = fileName;
199
200 if (source) {
201 delete map.sourcesContent;
202 if (outFile) {
203 map.sources[0] = path.join(path.relative(path.dirname(outFile), path.dirname(source)), path.basename(source));
204 }
205 } else {
206 map.sourcesContent[0] = content;
207 }
208
209 code += '\n//# sourceMappingURL=' + (inlineSourceMaps ?
210 'data:application/json;charset=utf-8;base64,' + btoa(JSON.stringify(map)) :
211 path.basename(outFile) + '.map'
212 ) + '\n';
213 }
214
215 if (outFile) {
216 fs.writeFileSync(outFile, code);
217 info(fileName + '\n \u21B3 \033[32m' + outFile + '\033[0m\n');
218 if (sourceMaps && !inlineSourceMaps) {
219 var mapOutFile = outFile + '.map';
220 fs.writeFileSync(mapOutFile, JSON.stringify(map) + '\n');
221 info('\033[2m \u21B3 \033[32m' + mapOutFile + '\033[0m\n');
222 }
223 } else {
224 process.stdout.write(code);
225 }
226 }
227
228 function btoa(str) {
229 return (Buffer.from ? Buffer.from(str) : new Buffer(str)).toString('base64');
230 }
231
232 function transformSource(content, filepath) {
233 try {
234 return flowRemoveTypes(content, { all: all, pretty: pretty });
235 } catch (error) {
236 if (error.loc) {
237 var line = error.loc.line - 1;
238 var col = error.loc.column;
239 var text = content.split(/\r\n?|\n|\u2028|\u2029/)[line];
240 process.stderr.write(
241 filepath + '\n' +
242 ' \u21B3 \033[31mSyntax Error: ' + error.message + '\033[0m\n' +
243 ' \033[90m' + line + ': \033[0m' +
244 text.slice(0, col) + '\033[7;31m' + text[col] + '\033[0m' + text.slice(col + 1) + '\n'
245 );
246 process.exit(1);
247 }
248 throw error;
249 }
250 }