just-build
Version:
A simple task runner that doesn't bloat your package
68 lines (57 loc) • 2.34 kB
JavaScript
var ref = require ('./dirutils');
var getPackageRoot = ref.getPackageRoot;
var fs = require('fs');
var path = require('path');
var ref$1 = require ('./extend');
var clone = ref$1.clone;
function extractConfig (args, ref) {
var cwd = ref.cwd;
var env = ref.env;
args = args.slice(2);
var pkg = getPackageOption(args) || cwd;
var pkgIsDir = fs.lstatSync(pkg).isDirectory();
var pkgDir = pkgIsDir ? pkg : path.dirname(pkg);
var packageRoot = getPackageRoot(pkgDir);
if (!packageRoot)
{ throw new Error(("Could not find npm package in " + pkgDir)); }
var configFile = pkgIsDir ?
path.resolve(packageRoot, 'package.json') :
pkg;
if (!fs.existsSync(configFile))
{ throw new Error(("File not found: " + configFile)); }
var cfg = JSON.parse(fs.readFileSync(configFile));
var taskSet = cfg["just-build"];
if (!taskSet)
{ throw new Error (("\"just-build\" attribute missing in " + configFile)); }
if (typeof taskSet !== 'object')
{ throw new Error (("\"just-build\" attribute not an object in " + configFile)); }
var tasksToRun = args.filter(function (arg) { return arg.indexOf('-') !== 0; });
if (tasksToRun.length === 0) {
if (!taskSet["default"])
{ throw new Error (("No default task exists in " + configFile)); }
tasksToRun = ["default"];
}
// Make sure packageRoot/node_modules/.bin is part of PATH.
var PATH = env.PATH || "";
var node_modules_bin = path.resolve(packageRoot, 'node_modules/.bin');
if (PATH.indexOf(("node_modules" + (path.sep) + ".bin")) === -1) {
PATH = "" + PATH + (path.delimiter) + node_modules_bin;
}
return {
dir: packageRoot,
taskSet: taskSet,
tasksToRun: tasksToRun,
watchMode: args.some(function (arg) { return arg === '--watch' || arg === '-w'; }),
env: clone(cfg, env), // Pass package.json as ENV vars unless overrided by os ENV vars.
packageRoot: packageRoot
};
}
function getPackageOption(args) {
var idxPackage = args.indexOf('--package');
if (idxPackage === -1) { idxPackage = args.indexOf('-p'); }
if (idxPackage >= 0 && idxPackage + 1 < args.length) {
return args[idxPackage + 1];
}
return null;
}
module.exports = {extractConfig: extractConfig};