UNPKG

nlu

Version:

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

130 lines (129 loc) 4.24 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 root = residence.findProjectRoot(cwd); 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 conf = null, confPath = null; if (opts.global) { confPath = utils_1.globalConfigFilePath; try { conf = require(utils_1.globalConfigFilePath); } catch (err) { conf = {}; } if (!(conf && typeof conf === 'object')) { conf = {}; } if (Array.isArray(conf)) { conf = {}; } } else { if (!root) { logging_1.default.error('You want to update the local config, but we could not find a project root - could not find a local package.json file.'); process.exit(0); } confPath = path.resolve(root + '/.nlu.json'); try { conf = require(confPath); } catch (err) { logging_1.default.error('Could not load your .nlu.json file at path:', confPath); throw chalk_1.default.magenta(err.message); } if (!(conf && typeof conf === 'object')) { conf = {}; } if (Array.isArray(conf)) { conf = {}; } } if (String(opts._args[1] || '').match(/[^a-zA-Z_-]+/g)) { logging_1.default.warn('Your key had a bad character, converting to underscore.'); } if (String(opts._args[2] || '').match(/[^a-zA-Z_-]+/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-Z]+/g, '_'); const v = String(opts._args[2] || '').toLowerCase().replace(/[^a-zA-Z]+/g, '_'); const importGlobal = function (val) { Promise.resolve().then(() => require(`./global/${val}`)).then(m => { console.log(); m.default(opts, confPath, conf, k, v); }); }; const globalValues = { delete() { logging_1.default.info('Running "delete" on your global config for key:', k); importGlobal('delete'); }, clear() { logging_1.default.info('Running "clear" on your global config'); importGlobal('clear'); }, get() { logging_1.default.info('Running "get" on your global config.'); importGlobal('get'); }, set() { logging_1.default.info('Running "set" on your global config.'); importGlobal('set'); } }; const importLocal = function (val) { Promise.resolve().then(() => require(`./local/${val}`)).then(m => { console.log(); m.default(opts, confPath, conf, k, v); }); }; const localValues = { delete() { logging_1.default.info('Running "delete" on your local config (localSettings in .nlu.json).'); importLocal('delete'); }, clear() { logging_1.default.info('Running "clear" on your local config (localSettings in .nlu.json).'); importLocal('clear'); }, get() { logging_1.default.info('Running "get" on your local config (localSettings in .nlu.json).'); importLocal('get'); }, set() { logging_1.default.info('Running "set" on your local config (localSettings in .nlu.json).'); importLocal('set'); } }; let container = 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); }