nlu
Version:
Use this package to link your projects together for local development.
174 lines (173 loc) • 7.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const fs = require("fs");
const readline = require("readline");
const chalk_1 = require("chalk");
const dashdash = require('dashdash');
const async = require("async");
const residence = require("residence");
const cmd_line_opts_1 = require("./cmd-line-opts");
const logging_1 = require("../../logging");
const npmLinkUpPkg = require('../../../package.json');
const cwd = process.cwd();
const root = residence.findProjectRoot(cwd);
const defaultNluJSON = require('../../../assets/default.nlu.json');
const find_matching_projects_1 = require("./find-matching-projects");
const init_ignore_1 = require("./init-ignore");
const always_ignore_1 = require("../../always-ignore");
const map_paths_with_env_vars_1 = require("../../map-paths-with-env-vars");
process.once('exit', code => {
logging_1.default.info('Exiting with code:', code, '\n');
});
if (!root) {
logging_1.default.error('Cannot find a project root given your current working directory:', chalk_1.default.magenta(cwd));
logging_1.default.error(' => NLU could not find a package.json file within your cwd.');
process.exit(1);
}
const allowUnknown = process.argv.indexOf('--allow-unknown') > 0;
let opts, parser = dashdash.createParser({ options: cmd_line_opts_1.default, allowUnknown });
try {
opts = parser.parse(process.argv);
}
catch (e) {
logging_1.default.error(chalk_1.default.magenta(' => CLI parsing error:'), chalk_1.default.magentaBright.bold(e.message));
process.exit(1);
}
if (opts.help) {
let help = parser.help({ includeEnv: true }).trimRight();
console.log('usage: nlu init [OPTIONS]\n' + 'options:\n' + help);
process.exit(0);
}
let pkgJSON;
try {
pkgJSON = require(path.resolve(root + '/package.json'));
}
catch (err) {
logging_1.default.error(err);
logging_1.default.error('Could not load your projects package.json file.');
logging_1.default.error('No package.json file could be found in path:', root);
process.exit(1);
}
const mainProjectName = pkgJSON.name;
if (!mainProjectName) {
logging_1.default.error('Your current project does not have a name (no name property in package.json.');
logging_1.default.error('That is weird.');
}
else {
logging_1.default.info('Your project name is:', chalk_1.default.bold.blueBright(mainProjectName));
}
let nluJSON, nluJSONPath = path.resolve(root + '/.nlu.json');
try {
nluJSON = require(nluJSONPath);
logging_1.default.warn(chalk_1.default.magenta('Looks like your project already has an .nlu.json file.'));
if (opts.verbosity > 3) {
logging_1.default.warn(chalk_1.default.gray('Here is the existing file on your file system:'));
console.log(nluJSON);
}
process.exit(1);
}
catch (err) {
}
const searchRoots = [];
const ignore = init_ignore_1.default.concat(always_ignore_1.default)
.filter((item, index, arr) => arr.indexOf(item) === index)
.map(item => new RegExp(item));
const theirDeps = Object.assign({}, pkgJSON.dependencies, pkgJSON.devDependencies, pkgJSON.optionalDependencies);
if (opts.verbosity > 3) {
logging_1.default.info('Here are the deps in your package.json:', Object.keys(theirDeps));
}
async.autoInject({
checkForNluJSONFile(cb) {
fs.stat(nluJSONPath, (err, stats) => cb(null, stats));
},
askUserAboutSearchRoots(cb) {
if (opts.force) {
const dirname = path.dirname(cwd);
const defaultSearchRoot = String(dirname).replace(String(process.env.HOME), '$HOME');
searchRoots.push(defaultSearchRoot);
return process.nextTick(cb);
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
process.nextTick(function () {
console.log();
console.log(chalk_1.default.gray(` => To skip this, hit return with no input.`));
console.log(chalk_1.default.gray(` => Separate multiple paths with ":"`));
console.log(chalk_1.default.gray(` => Use env variables like $HOME instead of hardcoding dirs, where possible.`));
console.log(chalk_1.default.bold(` => Valid input might be:`), `$HOME/WebstormProjects:$HOME/vscode_projects.`);
});
console.log();
rl.question(chalk_1.default.bold(`What folder(s) do local dependencies of this project '${chalk_1.default.blueBright(mainProjectName)}' reside in?`), a => {
String(a || '').trim()
.split(':')
.map(v => String(v || '').trim())
.filter(Boolean)
.forEach(v => {
const s = !searchRoots.some(p => {
return p.startsWith(v + '/');
});
if (s) {
searchRoots.push(v);
}
});
if (searchRoots.length < 1) {
searchRoots.push('$HOME');
}
cb(null);
rl.close();
});
},
mapSearchRoots(askUserAboutSearchRoots, cb) {
map_paths_with_env_vars_1.mapPaths(searchRoots, cb);
},
getMatchingProjects(askUserAboutSearchRoots, mapSearchRoots, checkForNluJSONFile, cb) {
if (checkForNluJSONFile) {
return process.nextTick(cb, new Error('Looks like your project already has an .nlu.json file, although it may be malformed.'));
}
const map = {}, status = { searching: true };
const findProjects = find_matching_projects_1.makeFindProjects(mainProjectName, ignore, opts, map, theirDeps, status);
const q = async.queue((task, cb) => task(cb));
logging_1.default.info('Your search roots are:', mapSearchRoots);
mapSearchRoots.forEach((v) => {
q.push(function (cb) {
findProjects(v, cb);
});
});
if (q.idle()) {
return process.nextTick(cb, new Error('For some reason no items ended up on the search queue.'));
}
let first = true;
q.drain = q.error = function (err) {
if (err) {
status.searching = false;
logging_1.default.error(chalk_1.default.magenta('There was a search queue processing error.'));
}
if (first) {
q.kill();
cb(err, map);
}
first = false;
};
},
writeNLUJSON(askUserAboutSearchRoots, getMatchingProjects, cb) {
const list = Object.keys(getMatchingProjects);
const newNluJSON = Object.assign({}, defaultNluJSON);
newNluJSON.searchRoots = searchRoots;
newNluJSON.list = list;
const newNluJSONstr = JSON.stringify(newNluJSON, null, 2);
fs.writeFile(nluJSONPath, newNluJSONstr, 'utf8', cb);
}
}, function (err, results) {
if (err) {
logging_1.default.error('There was an error when running "nlu init".');
opts.verbosity > 3 && logging_1.default.error('Here were your arguments:\n', process.argv);
logging_1.default.error(err.message || err);
return process.exit(1);
}
logging_1.default.veryGood(chalk_1.default.green('Looks like the nlu init routine succeeded. ') +
'Check your new .nlu.json file in the root of your project.');
process.exit(0);
});