Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement 'lerna run' to run an npm script in each package.
  • Loading branch information
Rick authored and jamiebuilds committed Feb 16, 2016
1 parent 279d94e commit 9a9d28b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions bin/lerna.js
Expand Up @@ -13,6 +13,7 @@ var cli = meow([
" bootstrap Link together local packages and npm install remaining package dependencies",
" publish Publish updated packages to npm",
" updated Check which packages have changed since the last release",
" run Run npm script in each package",
""
]);

Expand Down
1 change: 1 addition & 0 deletions lib/commands/index.js
@@ -1,3 +1,4 @@
exports.bootstrap = require("./bootstrap");
exports.publish = require("./publish");
exports.updated = require("./updated");
exports.run = require("./run");
95 changes: 95 additions & 0 deletions lib/commands/run/index.js
@@ -0,0 +1,95 @@
var progressBar = require("../progress-bar");
var chalk = require("chalk");
var child = require("child_process");
var async = require("async");
var fs = require("fs");

module.exports = function (config) {
var scriptPackages = [];
var runArguments = (process.argv||[]).slice(3);
var scriptName = runArguments.shift();

if (!scriptName) {
console.error(chalk.red("No npm script specified."));
process.exit(1);
}

try {
findScriptPackages();
runScript();
} catch (err) {
onError(err);
}

function getPackageLocation(name) {
return config.packagesLoc + "/" + name;
}

function getPackageConfig(name) {
return require(getPackageLocation(name) + "/package.json");
}

function findScriptPackages() {
console.log("Checking packages...");

var packageNames = fs.readdirSync(config.packagesLoc).filter(function (name) {
return name[0] !== "." && fs.statSync(config.packagesLoc + "/" + name).isDirectory();
});

var tick = progressBar(packageNames.length);

packageNames.forEach(function (name) {
tick(name);

var config = getPackageConfig(name);

if (config.scripts && config.scripts.hasOwnProperty(scriptName)) {
scriptPackages.push(name);
}
});

if (!scriptPackages.length) {
console.error(chalk.red("No packages found with the npm script '" + scriptName + "'"));
process.exit(1);
}
}

function runScript () {
console.log("Running npm script in packages...");
var tick = progressBar(scriptPackages.length);

async.parallelLimit(scriptPackages.map(function (name) {
return function run(done) {
var loc = getPackageLocation(name);

child.exec("cd " + loc + " && npm run " + scriptName + " " + runArguments.join(" "), function (err, stdout, stderr) {
if (err || stderr) {
err = stderr || err.stack;
console.log(chalk.red(err));
return done(err);
}

tick(name);
done();
});
};
}), 4, function (err) {
onError(err);
console.log();
console.log(chalk.green("Successfully ran npm script '" + scriptName + "' in packages."));
console.log("Ran in packages:");
console.log(scriptPackages);
process.exit();
});
}

function onError(err) {
if (!err) return;

console.log();
console.error(chalk.red("There was a problem running npm script '" + scriptName + "' in packages."));

console.error(err.stack || err);
process.exit(1);
}
};

0 comments on commit 9a9d28b

Please sign in to comment.