just-build
Version:
A simple task runner that doesn't bloat your package
68 lines (61 loc) • 2.86 kB
JavaScript
var path = require('path');
var fs = require('fs');
var ref = require('./dirutils');
var getPackageRoot = ref.getPackageRoot;
var getBinLocation = ref.getBinLocation;
var ref$1 = require('./tokenize');
var tokenize = ref$1.tokenize;
var surroundWithQuotes = ref$1.surroundWithQuotes;
var debug = require('./debug');
function whichLocal (cmd, cwd) {
debug.log(("whichLocal(" + (JSON.stringify(cwd)) + ", " + (JSON.stringify(cmd)) + ")"));
var binScript = getBinLocation(cwd, cmd);
debug.log(("binScript = " + (JSON.stringify(binScript))));
if (!binScript) { return null; }
var binDir = path.dirname (binScript);
debug.log(("binDir = " + (JSON.stringify(binDir))));
var node_modules = path.normalize(path.resolve(binDir, ".."));
debug.log(("node_modules = " + (JSON.stringify(node_modules))));
if (!fs.existsSync(binScript)) { return null; }
var scriptLines = fs.readFileSync(binScript, "utf-8").split('\n');
if (scriptLines.length === 0) { return null; }
var shebang = scriptLines[0].trim();
debug.log(("shebang = " + shebang));
if (shebang[0] != "#") { return null; }
if (shebang.indexOf("bin/sh") === -1) {
// Not a shellscript (cygwin starter)
if (scriptLines[0].indexOf(' node') !== -1) {
// A node script by itself
debug.log("A node script by itself");
return path.normalize(binScript);
}
debug.log("whichLocal return null since bin/sh not found in shebang.");
return null;
}
var invokerLines = scriptLines.filter(function (line) { return line.indexOf('node ') !== -1; });
debug.log(("invokerLines = " + (JSON.stringify(invokerLines))));
// Expected to find: ` node "$basedir/../uglify-js/bin/uglifyjs" "$@"`
if (invokerLines.length < 1) { return null; }
var tokenized = tokenize(invokerLines[0]);
// Expected: "node", "$basedir/../uglify-js/bin/uglifyjs", "$@" or
// "exec", "node", "$basedir/../typescript/bin/tsc", "$@" or
// "exec", "$basedir/node", "$basedir/../typescript/bin/tsc". "$@""
if (tokenized.length < 3) { return null; }
var binFilePath = tokenized.find(function (part) { return part !== 'exec' &&
part !== 'node' &&
!part.endsWith('/node'); }
);
var splitted = binFilePath.split('/');
while (splitted.length > 0 && (
splitted[0].indexOf('$') === 0 || // $basedir
//splitted[0] === '..' || // ".."
splitted[0] === '.')) // just for optional future changes
{
splitted = splitted.slice(1);
}
var jsScriptPath = path.normalize(path.resolve(binDir, splitted.join('/')));
debug.log(("jsScriptPath = " + (JSON.stringify(jsScriptPath))));
if (splitted.length === 0) { return null; }
return jsScriptPath;
}
module.exports = { whichLocal: whichLocal };