UNPKG

npm-link-up

Version:

Use this package to link your projects together for local development.

112 lines (111 loc) 4.05 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); const chalk_1 = require("chalk"); const dashdash = require('dashdash'); const cmd_line_opts_1 = require("./cmd-line-opts"); const logging_1 = require("../../logging"); const npmLinkUpPkg = require('../../../package.json'); const residence = require("residence"); const cwd = process.cwd(); const path = require("path"); const utils_1 = require("../../utils"); const fs = require("fs"); const assert = require("assert"); process.once('exit', code => { console.log(); logging_1.default.info('Exiting with code:', code, '\n'); }); 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); } let root = residence.findProjectRoot(cwd); opts.config = path.resolve(String(opts.config || '').replace(/\.nlu\.json$/, '')); try { if (opts.config) { assert(fs.statSync(opts.config).isDirectory(), 'config path is not a directory.'); } } catch (err) { logging_1.default.error('You declared a config path but the following path is not a directory:', opts.config); throw chalk_1.default.magenta(err.message); } let { nluFilePath: confPath, nluConfigRoot } = utils_1.handleConfigCLIOpt(cwd, opts); if (!root) { logging_1.default.warn('You want to update the local config, but we could not find a project root - we could not find a local "package.json" file.'); root = cwd; } let conf = null; if (opts.global) { confPath = utils_1.globalConfigFilePath; } try { conf = require(confPath); } catch (err) { logging_1.default.error('Could not load your nlu config file at path:', chalk_1.default.bold(confPath)); throw chalk_1.default.magenta(err.message); } if (Array.isArray(conf)) { throw chalk_1.default.magenta('Conf resolved to an Array instance, it needs to be an non-Array object.'); } if (!(conf && typeof conf === 'object')) { conf = {}; } if (String(opts._args[1] || '').match(/[^a-zA-Z0-9-]+/g)) { logging_1.default.warn('Your key had a bad character, converting to underscore.'); } if (String(opts._args[2] || '').match(/[^a-zA-Z0-9-]+/g)) { logging_1.default.warn('Your value had a bad character, converting to underscore.'); } const firstArg = String(opts._args[0] || '').toLowerCase(); const k = String(opts._args[1] || '').toLowerCase().replace(/[^a-zA-Z0-9-]+/g, '_'); const v = String(opts._args[2] || '').toLowerCase().replace(/[^a-zA-Z0-9-]+/g, '_'); if (opts._args[1]) { logging_1.default.info('Sanitized key:', `'${k}'`); } if (opts._args[2]) { logging_1.default.info('Sanitized value:', `'${v}'`); } const importGlobal = (val) => { Promise.resolve().then(() => require(`./global/${val}`)).then(m => { console.log(); m.default(opts, confPath, conf, k, v); }); }; const globalValues = {}; ['delete', 'clear', 'get', 'set'].forEach(meth => { globalValues[meth] = () => { logging_1.default.info(`Running "${meth}" on your ${chalk_1.default.bold('global')} config.`); importGlobal(meth); }; }); const importLocal = (val) => { Promise.resolve().then(() => require(`./local/${val}`)).then(m => { console.log(); m.default(opts, confPath, conf, k, v); }); }; const localValues = {}; ['delete', 'clear', 'get', 'set'].forEach(meth => { localValues[meth] = () => { logging_1.default.info(`Running "${meth}" on your ${chalk_1.default.bold('local')} config (localSettings in .nlu.json).`); importLocal(meth); }; }); let container = Object.assign({}, localValues); if (opts.global) { container = globalValues; } if (container[firstArg]) { container[firstArg](); } else { logging_1.default.error('No "nlu config" subcommand was recognized. Your subcommand was:', firstArg); process.exit(1); }