-
Notifications
You must be signed in to change notification settings - Fork 482
/
Copy pathRunner.js
318 lines (287 loc) · 9.11 KB
/
Runner.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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/**
* 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.
*/
'use strict';
const child_process = require('child_process');
const chalk = require('chalk');
const fs = require('graceful-fs');
const path = require('path');
const http = require('http');
const https = require('https');
const temp = require('temp');
const ignores = require('./ignoreFiles');
const availableCpus = Math.max(require('os').cpus().length - 1, 1);
const CHUNK_SIZE = 50;
function lineBreak(str) {
return /\n$/.test(str) ? str : str + '\n';
}
const bufferedWrite = (function() {
const buffer = [];
let buffering = false;
process.stdout.on('drain', () => {
if (!buffering) return;
while (buffer.length > 0 && process.stdout.write(buffer.shift()) !== false);
if (buffer.length === 0) {
buffering = false;
}
});
return function write(msg) {
if (buffering) {
buffer.push(msg);
}
if (process.stdout.write(msg) === false) {
buffering = true;
}
};
}());
const log = {
ok(msg, verbose) {
verbose >= 2 && bufferedWrite(chalk.white.bgGreen(' OKK ') + msg);
},
nochange(msg, verbose) {
verbose >= 1 && bufferedWrite(chalk.white.bgYellow(' NOC ') + msg);
},
skip(msg, verbose) {
verbose >= 1 && bufferedWrite(chalk.white.bgYellow(' SKIP ') + msg);
},
error(msg, verbose) {
verbose >= 0 && bufferedWrite(chalk.white.bgRed(' ERR ') + msg);
},
};
function report({file, msg}) {
bufferedWrite(lineBreak(`${chalk.white.bgBlue(' REP ')}${file} ${msg}`));
}
function concatAll(arrays) {
const result = [];
for (const array of arrays) {
for (const element of array) {
result.push(element);
}
}
return result;
}
function showFileStats(fileStats) {
process.stdout.write(
'Results: \n'+
chalk.red(fileStats.error + ' errors\n')+
chalk.yellow(fileStats.nochange + ' unmodified\n')+
chalk.yellow(fileStats.skip + ' skipped\n')+
chalk.green(fileStats.ok + ' ok\n')
);
}
function showStats(stats) {
const names = Object.keys(stats).sort();
if (names.length) {
process.stdout.write(chalk.blue('Stats: \n'));
}
names.forEach(name => process.stdout.write(name + ': ' + stats[name] + '\n'));
}
function dirFiles (dir, callback, acc) {
// acc stores files found so far and counts remaining paths to be processed
acc = acc || { files: [], remaining: 1 };
function done() {
// decrement count and return if there are no more paths left to process
if (!--acc.remaining) {
callback(acc.files);
}
}
fs.readdir(dir, (err, files) => {
// if dir does not exist or is not a directory, bail
// (this should not happen as long as calls do the necessary checks)
if (err) throw err;
acc.remaining += files.length;
files.forEach(file => {
let name = path.join(dir, file);
fs.stat(name, (err, stats) => {
if (err) {
// probably a symlink issue
process.stdout.write(
'Skipping path "' + name + '" which does not exist.\n'
);
done();
} else if (ignores.shouldIgnore(name)) {
// ignore the path
done();
} else if (stats.isDirectory()) {
dirFiles(name + '/', callback, acc);
} else {
acc.files.push(name);
done();
}
});
});
done();
});
}
function getAllFiles(paths, filter) {
return Promise.all(
paths.map(file => new Promise(resolve => {
fs.lstat(file, (err, stat) => {
if (err) {
process.stderr.write('Skipping path ' + file + ' which does not exist. \n');
resolve([]);
return;
}
if (stat.isDirectory()) {
dirFiles(
file,
list => resolve(list.filter(filter))
);
} else if (ignores.shouldIgnore(file)) {
// ignoring the file
resolve([]);
} else {
resolve([file]);
}
})
}))
).then(concatAll);
}
function run(transformFile, paths, options) {
let usedRemoteScript = false;
const cpus = options.cpus ? Math.min(availableCpus, options.cpus) : availableCpus;
const extensions =
options.extensions && options.extensions.split(',').map(ext => '.' + ext);
const fileCounters = {error: 0, ok: 0, nochange: 0, skip: 0};
const statsCounter = {};
const startTime = process.hrtime();
ignores.add(options.ignorePattern);
ignores.addFromFile(options.ignoreConfig);
if (/^http/.test(transformFile)) {
usedRemoteScript = true;
return new Promise((resolve, reject) => {
// call the correct `http` or `https` implementation
(transformFile.indexOf('https') !== 0 ? http : https).get(transformFile, (res) => {
let contents = '';
res
.on('data', (d) => {
contents += d.toString();
})
.on('end', () => {
const ext = path.extname(transformFile);
temp.open({ prefix: 'jscodeshift', suffix: ext }, (err, info) => {
if (err) return reject(err);
fs.write(info.fd, contents, function (err) {
if (err) return reject(err);
fs.close(info.fd, function(err) {
if (err) return reject(err);
transform(info.path).then(resolve, reject);
});
});
});
})
})
.on('error', (e) => {
reject(e);
});
});
} else if (!fs.existsSync(transformFile)) {
process.stderr.write(
chalk.white.bgRed('ERROR') + ' Transform file ' + transformFile + ' does not exist \n'
);
return;
} else {
return transform(transformFile);
}
function transform(transformFile) {
return getAllFiles(
paths,
name => !extensions || extensions.indexOf(path.extname(name)) != -1
).then(files => {
const numFiles = files.length;
if (numFiles === 0) {
process.stdout.write('No files selected, nothing to do. \n');
return [];
}
const processes = options.runInBand ? 1 : Math.min(numFiles, cpus);
const chunkSize = processes > 1 ?
Math.min(Math.ceil(numFiles / processes), CHUNK_SIZE) :
numFiles;
let index = 0;
// return the next chunk of work for a free worker
function next() {
if (!options.silent && !options.runInBand && index < numFiles) {
process.stdout.write(
'Sending ' +
Math.min(chunkSize, numFiles-index) +
' files to free worker...\n'
);
}
return files.slice(index, index += chunkSize);
}
if (!options.silent) {
process.stdout.write('Processing ' + files.length + ' files... \n');
if (!options.runInBand) {
process.stdout.write(
'Spawning ' + processes +' workers...\n'
);
}
if (options.dry) {
process.stdout.write(
chalk.green('Running in dry mode, no files will be written! \n')
);
}
}
const args = [transformFile, options.babel ? 'babel' : 'no-babel'];
const workers = [];
for (let i = 0; i < processes; i++) {
workers.push(options.runInBand ?
require('./Worker')(args) :
child_process.fork(require.resolve('./Worker'), args)
);
}
return workers.map(child => {
child.send({files: next(), options});
child.on('message', message => {
switch (message.action) {
case 'status':
fileCounters[message.status] += 1;
log[message.status](lineBreak(message.msg), options.verbose);
break;
case 'update':
if (!statsCounter[message.name]) {
statsCounter[message.name] = 0;
}
statsCounter[message.name] += message.quantity;
break;
case 'free':
child.send({files: next(), options});
break;
case 'report':
report(message);
break;
}
});
return new Promise(resolve => child.on('disconnect', resolve));
});
})
.then(pendingWorkers =>
Promise.all(pendingWorkers).then(() => {
const endTime = process.hrtime(startTime);
const timeElapsed = (endTime[0] + endTime[1]/1e9).toFixed(3);
if (!options.silent) {
process.stdout.write('All done. \n');
showFileStats(fileCounters);
showStats(statsCounter);
process.stdout.write(
'Time elapsed: ' + timeElapsed + 'seconds \n'
);
if (options.failOnError && fileCounters.error > 0) {
process.exit(1);
}
}
if (usedRemoteScript) {
temp.cleanupSync();
}
return Object.assign({
stats: statsCounter,
timeElapsed: timeElapsed
}, fileCounters);
})
);
}
}
exports.run = run;