Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add '--run-in-band' option to run in the current process #71

Merged
merged 1 commit into from
Dec 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bin/jscodeshift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ var opts = require('nomnom')
extensions: {
default: 'js',
help: 'File extensions the transform file should be applied to'
},
runInBand: {
flag: true,
default: false,
full: 'run-in-band',
help: 'Run serially in the current process'
}
})
.parse();
Expand Down
22 changes: 12 additions & 10 deletions src/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,22 +109,24 @@ function run(transformFile, paths, options) {
}

console.log('Processing %d files...', files.length);
console.log(
'Spawning %d workers with %d files each...',
fileChunks.length,
fileChunks[0].length
);
if (!options.runInBand) {
console.log(
'Spawning %d workers with %d files each...',
fileChunks.length,
fileChunks[0].length
);
}
if (options.dry) {
console.log(
clc.green('Running in dry mode, no files will be written!')
);
}

return fileChunks.map(files => {
const child = child_process.fork(
require.resolve('./Worker'),
[transformFile, options.babel ? 'babel' : 'no-babel']
);
const args = [transformFile, options.babel ? 'babel' : 'no-babel'];
const child = options.runInBand ?
require('./Worker')(args) :
child_process.fork(require.resolve('./Worker'), args);
child.send({files, options});
child.on('message', message => {
switch (message.action) {
Expand All @@ -146,7 +148,7 @@ function run(transformFile, paths, options) {
.then(pendingWorkers =>
Promise.all(pendingWorkers).then(() => {
const endTime = process.hrtime(startTime);
console.log('All workers done.');
console.log('All done.');
showFileStats(fileCounters);
showStats(statsCounter);
console.log(
Expand Down
49 changes: 37 additions & 12 deletions src/Worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,51 @@

'use strict';

if (process.argv[3] === 'babel') {
require('babel-core/register')();
}
const EventEmitter = require('events').EventEmitter;

const async = require('async');
const fs = require('fs');

var async = require('async');
var fs = require('fs');
const jscodeshift = require('./core');

var jscodeshift = require('./core');
var transform = require(process.argv[2]);
let emitter;
let finish;
let notify;
let transform;

if (module.parent) {
emitter = new EventEmitter();
emitter.send = (data) => { run(data); };
finish = () => { emitter.emit('disconnect'); };
notify = (data) => { emitter.emit('message', data); };
module.exports = (args) => {
setup(args[0], args[1]);
return emitter;
};
} else {
finish = () => { process.disconnect(); };
notify = (data) => { process.send(data); };
process.on('message', (data) => { run(data); });
setup(process.argv[2], process.argv[3]);
}

function setup(tr, babel) {
if (babel === 'babel') {
require('babel-core/register')();
}
transform = require(tr);
}

function updateStatus(status, file, msg) {
msg = msg ? file + ' ' + msg : file;
process.send({action: 'status', status: status, msg: msg});
notify({action: 'status', status: status, msg: msg});
}

function empty() {}

function stats(name, quantity) {
quantity = typeof quantity !== 'number' ? 1 : quantity;
process.send({action: 'update', name: name, quantity: quantity});
notify({action: 'update', name: name, quantity: quantity});
}

function trimStackTrace(trace) {
Expand All @@ -45,7 +70,7 @@ function trimStackTrace(trace) {
return result.join('\n');
}

process.on('message', function(data) {
function run(data) {
var files = data.files;
var options = data.options;
async.each(
Expand Down Expand Up @@ -105,7 +130,7 @@ process.on('message', function(data) {
if (err) {
updateStatus('error', '', 'This should never be shown!');
}
process.disconnect();
finish();
}
);
});
}