UNPKG

@anycli/config

Version:

base config object and standard interfaces for anycli components

126 lines (125 loc) 4.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const os = require("os"); const path = require("path"); const debug_1 = require("./debug"); const errors_1 = require("./errors"); const Plugin = require("./plugin"); const util_1 = require("./util"); const debug = debug_1.default(); class Config extends Plugin.Plugin { constructor(opts) { super(opts); this.debug = 0; if (this.alreadyLoaded) return; this.arch = (os.arch() === 'ia32' ? 'x86' : os.arch()); this.platform = os.platform(); this.windows = this.platform === 'win32'; this.bin = this.pjson.anycli.bin || this.name; this.dirname = this.pjson.anycli.dirname || this.name; this.userAgent = `${this.name}/${this.version} (${this.platform}-${this.arch}) node-${process.version}`; this.shell = this._shell(); this.debug = this._debug(); this.home = process.env.HOME || (this.windows && this.windowsHome()) || os.homedir() || os.tmpdir(); this.cacheDir = this.scopedEnvVar('CACHE_DIR') || this.macosCacheDir() || this.dir('cache'); this.configDir = this.scopedEnvVar('CONFIG_DIR') || this.dir('config'); this.dataDir = this.scopedEnvVar('DATA_DIR') || this.dir('data'); this.errlog = path.join(this.cacheDir, 'error.log'); this.npmRegistry = this.scopedEnvVar('NPM_REGISTRY') || this.pjson.anycli.npmRegistry || 'https://registry.yarnpkg.com'; try { const devPlugins = this.pjson.anycli.devPlugins; if (devPlugins) this.loadPlugins(this.root, devPlugins); } catch (err) { process.emitWarning(err); } try { const userPJSONPath = path.join(this.dataDir, 'package.json'); const pjson = this.userPJSON = util_1.loadJSONSync(userPJSONPath); if (!pjson.anycli) pjson.anycli = { schema: 1 }; this.loadPlugins(userPJSONPath, pjson.anycli.plugins); } catch (err) { if (err.code !== 'ENOENT') process.emitWarning(err); } debug('config done'); } async runHook(event, opts) { debug('start %s hook', event); await super.runHook(event, Object.assign({}, opts || {}, { config: this })); debug('done %s hook', event); } async runCommand(id, argv = []) { debug('runCommand %s %o', id, argv); const c = this.findCommand(id); if (!c) throw new errors_1.CLIError(`command ${id} not found`); const command = c.load(); await this.runHook('prerun', { Command: command, argv }); await command.run(argv, this); } scopedEnvVar(k) { return process.env[this.scopedEnvVarKey(k)]; } scopedEnvVarTrue(k) { let v = process.env[this.scopedEnvVarKey(k)]; return v === '1' || v === 'true'; } scopedEnvVarKey(k) { return [this.bin, k] .map(p => p.replace(/-/g, '_')) .join('_') .toUpperCase(); } dir(category) { const base = process.env[`XDG_${category.toUpperCase()}_HOME`] || (this.windows && process.env.LOCALAPPDATA) || path.join(this.home, category === 'data' ? '.local/share' : '.' + category); return path.join(base, this.dirname); } windowsHome() { return this.windowsHomedriveHome() || this.windowsUserprofileHome(); } windowsHomedriveHome() { return (process.env.HOMEDRIVE && process.env.HOMEPATH && path.join(process.env.HOMEDRIVE, process.env.HOMEPATH)); } windowsUserprofileHome() { return process.env.USERPROFILE; } macosCacheDir() { return this.platform === 'darwin' && path.join(this.home, 'Library', 'Caches', this.dirname) || undefined; } _shell() { let shellPath; const { SHELL, COMSPEC } = process.env; if (SHELL) { shellPath = SHELL.split('/'); } else if (this.windows && COMSPEC) { shellPath = COMSPEC.split(/\\|\//); } else { shellPath = ['unknown']; } return shellPath[shellPath.length - 1]; } _debug() { if (this.scopedEnvVarTrue('DEBUG')) return 1; try { const { enabled } = require('debug')(this.bin); if (enabled) return 1; } catch (_a) { } return 0; } } exports.Config = Config; function load(opts = (module.parent && module.parent && module.parent.parent && module.parent.parent.filename) || __dirname) { if (typeof opts === 'string') opts = { root: opts }; if (isConfig(opts)) return opts; return new Config(opts); } exports.load = load; function isConfig(o) { return o && !!o._base; }