jscodeshift
Advanced tools
Comparing version 0.3.13 to 0.3.14
@@ -290,3 +290,3 @@ /* | ||
return n instanceof NodePath; | ||
}), 'Every element in the array is a NodePath'); | ||
}), 'Every element in the array should be a NodePath'); | ||
@@ -310,3 +310,3 @@ return new Collection(paths, parent, type); | ||
return Node.check(n); | ||
}), 'Every element in the array is a Node'); | ||
}), 'Every element in the array should be a Node'); | ||
return fromPaths(nodes.map(function (n) { | ||
@@ -313,0 +313,0 @@ return new NodePath(n); |
@@ -23,3 +23,2 @@ /* | ||
var clc = require('cli-color'); | ||
var dir = require('node-dir'); | ||
var fs = require('fs'); | ||
@@ -29,2 +28,3 @@ var path = require('path'); | ||
var availableCpus = require('os').cpus().length - 1; | ||
var CHUNK_SIZE = 50; | ||
@@ -60,2 +60,38 @@ var log = { | ||
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, function (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(function (file) { | ||
var name = dir + file; | ||
fs.stat(name, function (err, stats) { | ||
if (err) { | ||
// probably a symlink issue | ||
console.log('Skipping path "%s" which does not exist.', name); | ||
done(); | ||
} else if (stats.isDirectory()) { | ||
dirFiles(name + "/", callback, acc); | ||
} else { | ||
acc.files.push(name); | ||
done(); | ||
} | ||
}); | ||
}); | ||
done(); | ||
}); | ||
} | ||
function getAllFiles(paths, filter) { | ||
@@ -72,4 +108,4 @@ return _Promise.all(paths.map(function (file) { | ||
if (stat.isDirectory()) { | ||
dir.files(file, function (err, list) { | ||
return resolve(list ? list.filter(filter) : []); | ||
dirFiles(file, function (list) { | ||
return resolve(list.filter(filter)); | ||
}); | ||
@@ -93,3 +129,2 @@ } else { | ||
}); | ||
var fileChunks = []; | ||
var fileCounters = { error: 0, ok: 0, nochange: 0, skip: 0 }; | ||
@@ -107,3 +142,5 @@ var statsCounter = {}; | ||
}).then(function (files) { | ||
if (files.length === 0) { | ||
var numFiles = files.length; | ||
if (numFiles === 0) { | ||
console.log('No files selected, nothing to do.'); | ||
@@ -113,12 +150,18 @@ return; | ||
var processes = Math.min(files.length, cpus); | ||
var chunkSize = Math.ceil(files.length / processes); | ||
for (var i = 0, l = files.length; i < l; i += chunkSize) { | ||
fileChunks.push(files.slice(i, i + chunkSize)); | ||
var processes = Math.min(numFiles, cpus); | ||
var chunkSize = Math.min(Math.ceil(numFiles / processes), CHUNK_SIZE); | ||
var index = 0; | ||
// return the next chunk of work for a free worker | ||
function next() { | ||
if (!options.silent && !options.runInBand && index < numFiles) { | ||
console.log('Sending %d files to free worker...', Math.min(chunkSize, numFiles - index)); | ||
} | ||
return files.slice(index, index += chunkSize); | ||
} | ||
if (!options.silent) { | ||
console.log('Processing %d files...', files.length); | ||
console.log('Processing %d files...', numFiles); | ||
if (!options.runInBand) { | ||
console.log('Spawning %d workers with %d files each...', fileChunks.length, fileChunks[0].length); | ||
console.log('Spawning %d workers...', processes); | ||
} | ||
@@ -130,6 +173,11 @@ if (options.dry) { | ||
return fileChunks.map(function (files) { | ||
var args = [transformFile, options.babel ? 'babel' : 'no-babel']; | ||
var child = options.runInBand ? require('./Worker')(args) : child_process.fork(require.resolve('./Worker'), args); | ||
child.send({ files: files, options: options }); | ||
var args = [transformFile, options.babel ? 'babel' : 'no-babel']; | ||
var workers = []; | ||
for (var i = 0; i < processes; i++) { | ||
workers.push(options.runInBand ? require('./Worker')(args) : child_process.fork(require.resolve('./Worker'), args)); | ||
} | ||
return workers.map(function (child) { | ||
child.send({ files: next(), options: options }); | ||
child.on('message', function (message) { | ||
@@ -147,2 +195,5 @@ switch (message.action) { | ||
break; | ||
case 'free': | ||
child.send({ files: next(), options: options }); | ||
break; | ||
} | ||
@@ -161,3 +212,3 @@ }); | ||
showStats(statsCounter); | ||
console.log('Time elapsed: %d.%d seconds', endTime[0], (endTime[1] / 1000000).toFixed(0)); | ||
console.log('Time elapsed: %s seconds', (endTime[0] + endTime[1] / 1e9).toFixed(3)); | ||
} | ||
@@ -164,0 +215,0 @@ return fileCounters; |
@@ -61,2 +61,6 @@ /* | ||
function free() { | ||
notify({ action: 'free' }); | ||
} | ||
function updateStatus(status, file, msg) { | ||
@@ -90,2 +94,6 @@ msg = msg ? file + ' ' + msg : file; | ||
var options = data.options; | ||
if (!files.length) { | ||
finish(); | ||
return; | ||
} | ||
async.each(files, function (file, callback) { | ||
@@ -137,4 +145,4 @@ fs.readFile(file, function (err, source) { | ||
} | ||
finish(); | ||
free(); | ||
}); | ||
} |
{ | ||
"name": "jscodeshift", | ||
"version": "0.3.13", | ||
"version": "0.3.14", | ||
"description": "A toolkit for JavaScript codemods", | ||
@@ -5,0 +5,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60606
1309