UNPKG

cli-engine-config

Version:

base cli-engine config objects and interfaces

289 lines (263 loc) 7.87 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.defaultConfig = undefined; exports.buildConfig = buildConfig; var _path = require('path'); var _path2 = _interopRequireDefault(_path); var _os = require('os'); var _os2 = _interopRequireDefault(_os); var _fsExtra = require('fs-extra'); var _fsExtra2 = _interopRequireDefault(_fsExtra); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function dir(config, category, d) { let cacheKey = `dir:${category}`; let cache = config.__cache[cacheKey]; if (cache) return cache; d = d || _path2.default.join(config.home, category === 'data' ? '.local/share' : '.' + category); if (config.windows) d = process.env.LOCALAPPDATA || d; d = process.env.XDG_DATA_HOME || d; d = _path2.default.join(d, config.dirname); _fsExtra2.default.mkdirpSync(d); config.__cache[cacheKey] = d; return d; } function debug(bin) { const debug = require('debug')(bin).enabled || envVarTrue(envVarKey(bin, 'DEBUG')); return debug ? 1 : 0; } function envVarKey(...parts) { return parts.map(p => p.replace(/-/g, '_')).join('_').toUpperCase(); } function envVarTrue(k) { let v = process.env[k]; return v === '1' || v === 'true'; } function loadUserConfig(config) { const cache = config.__cache['userConfig']; if (cache) return cache; const configPath = _path2.default.join(config.configDir, 'config.json'); let userConfig; try { userConfig = _fsExtra2.default.readJSONSync(configPath); } catch (e) { if (e.code === 'ENOENT') { userConfig = { skipAnalytics: false, install: null }; } else { throw e; } } config.__cache['userConfig'] = userConfig; if (config.skipAnalytics) userConfig.install = null;else if (!userConfig.install) { const uuid = require('uuid/v4'); userConfig.install = uuid(); try { _fsExtra2.default.writeJSONSync(configPath, userConfig); } catch (e) { userConfig.install = null; } } return userConfig; } function shell(onWindows = false) { let shellPath; if (process.env['SHELL']) { shellPath = process.env['SHELL'].split(`/`); } else if (onWindows && process.env['COMSPEC']) { shellPath = process.env['COMSPEC'].split(/\\|\//); } else { shellPath = ['unknown']; } return shellPath[shellPath.length - 1]; } function userAgent(config) { const channel = config.channel === 'stable' ? '' : ` ${config.channel}`; return `${config.name}/${config.version}${channel} (${config.platform}-${config.arch}) node-${process.version}`; } function registry(config) { const env = process.env[envVarKey(config.bin, 'NPM_REGISTRY')]; return env || config.pjson['cli-engine'].npmRegistry || 'https://registry.yarnpkg.com'; } function s3(config) { const env = process.env[envVarKey(config.bin, 'S3_HOST')]; const host = env || config.pjson['cli-engine'].s3 && config.pjson['cli-engine'].s3.host; return host ? { host } : {}; } function commandsDir(config) { let commandsDir = config.pjson['cli-engine'].commands; if (!commandsDir) return; return _path2.default.join(config.root, commandsDir); } function hooks(config) { let hooks = {}; for (let [k, v] of Object.entries(config.pjson['cli-engine'].hooks || {})) { hooks[k] = Array.isArray(v) ? v : [v]; } return hooks; } function envSkipAnalytics(config) { if (config.userConfig.skipAnalytics) { return true; } else if (envVarTrue('TESTING') || envVarTrue(envVarKey(config.bin, 'SKIP_ANALYTICS'))) { return true; } return false; } function topics(config) { if (!config.__cache['topics']) { config.__cache['topics'] = config.pjson['cli-engine'].topics || {}; for (let [k, v] of Object.entries(config.__cache['topics'])) { if (!v.name) v.name = k; } } return config.__cache['topics']; } function validatePJSON(pjson) { // const exampleCLI = { // bin: 'heroku', // dirname: 'heroku', // node: '8.0.0', // defaultCommand: 'dashboard', // commands: './lib/commands', // hooks: { // init: './lib/hooks/init.js', // update: './lib/hooks/update.js', // prerun: './lib/hooks/prerun.js', // 'plugins:preinstall': './lib/hooks/plugins/preinstall.js' // }, // s3: {host: 'host'}, // plugins: ['heroku-pg', 'heroku-redis'] // } // TODO: validate // const cli = pjson['cli-engine'] || {} // const comment = 'cli-engine-config' // const title = { // warning: 'invalid CLI package.json', // error: 'invalid CLI package.json' } // validate(cli, {comment, title, exampleConfig: exampleCLI}) // validate(cli.hooks, { // comment, // condition: (option, validOption) => { // console.dir({option, validOption}) // }, // title, // exampleConfig: exampleCLI.hooks // }) } function buildConfig(existing = {}) { if (!existing) existing = {}; if (existing._version) return existing; if (existing.root && !existing.pjson) { let pjsonPath = _path2.default.join(existing.root, 'package.json'); if (_fsExtra2.default.existsSync(pjsonPath)) { // parse the package.json at the root let pjson = _fsExtra2.default.readJSONSync(_path2.default.join(existing.root, 'package.json')); existing.pjson = { ...defaultConfig.pjson, 'cli-engine': { ...defaultConfig.pjson['cli-engine'], ...(pjson['cli-engine'] || {}) }, ...pjson }; validatePJSON(existing.pjson); } } return { _version: '1', pjson: { name: 'cli-engine', version: '0.0.0', dependencies: {}, 'cli-engine': { hooks: {}, defaultCommand: 'help', userPlugins: false, s3: { host: null } } }, channel: 'stable', home: _os2.default.homedir() || _os2.default.tmpdir(), root: _path2.default.join(__dirname, '..'), arch: _os2.default.arch() === 'ia32' ? 'x86' : _os2.default.arch(), platform: _os2.default.platform() === 'win32' ? 'windows' : _os2.default.platform(), mock: false, argv: process.argv.slice(1), get defaultCommand() { return this.pjson['cli-engine'].defaultCommand; }, get name() { return this.pjson.name; }, get version() { return this.pjson.version; }, get hooks() { return hooks(this); }, get windows() { return this.platform === 'windows'; }, get userAgent() { return userAgent(this); }, get dirname() { return this.pjson['cli-engine'].dirname || this.bin; }, get shell() { return shell(this.windows); }, get bin() { return this.pjson['cli-engine'].bin || this.name; }, get debug() { return debug(this.bin || 'cli-engine') || 0; }, get dataDir() { return dir(this, 'data'); }, get configDir() { return dir(this, 'config'); }, get cacheDir() { return dir(this, 'cache', this.platform === 'darwin' ? _path2.default.join(this.home, 'Library', 'Caches') : null); }, get userConfig() { return loadUserConfig(this); }, get skipAnalytics() { return envSkipAnalytics(this); }, get install() { return this.userConfig.install; }, get s3() { return s3(this); }, get commandsDir() { return commandsDir(this); }, get legacyConverter() { return this.pjson['cli-engine'].legacyConverter; }, get userPlugins() { return this.pjson['cli-engine'].userPlugins; }, get topics() { return topics(this); }, get errlog() { return _path2.default.join(this.cacheDir, 'error.log'); }, get npmRegistry() { return registry(this); }, ...existing, __cache: {} }; } const defaultConfig = exports.defaultConfig = buildConfig();