UNPKG

5.04 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const errors_1 = require("@anycli/errors");
4const os = require("os");
5const path = require("path");
6const debug_1 = require("./debug");
7const Plugin = require("./plugin");
8const util_1 = require("./util");
9const debug = debug_1.default();
10class Config extends Plugin.Plugin {
11 constructor(opts) {
12 super(opts);
13 this.debug = 0;
14 if (this.alreadyLoaded)
15 return;
16 this.arch = (os.arch() === 'ia32' ? 'x86' : os.arch());
17 this.platform = os.platform();
18 this.windows = this.platform === 'win32';
19 this.bin = this.pjson.anycli.bin || this.name;
20 this.dirname = this.pjson.anycli.dirname || this.name;
21 this.userAgent = `${this.name}/${this.version} (${this.platform}-${this.arch}) node-${process.version}`;
22 this.shell = this._shell();
23 this.debug = this._debug();
24 this.home = process.env.HOME || (this.windows && this.windowsHome()) || os.homedir() || os.tmpdir();
25 this.cacheDir = this.scopedEnvVar('CACHE_DIR') || this.macosCacheDir() || this.dir('cache');
26 this.configDir = this.scopedEnvVar('CONFIG_DIR') || this.dir('config');
27 this.dataDir = this.scopedEnvVar('DATA_DIR') || this.dir('data');
28 this.errlog = path.join(this.cacheDir, 'error.log');
29 this.npmRegistry = this.scopedEnvVar('NPM_REGISTRY') || this.pjson.anycli.npmRegistry || 'https://registry.yarnpkg.com';
30 if (opts.devPlugins !== false) {
31 try {
32 const devPlugins = this.pjson.anycli.devPlugins;
33 if (devPlugins)
34 this.loadPlugins(this.root, 'dev', devPlugins);
35 }
36 catch (err) {
37 process.emitWarning(err);
38 }
39 }
40 if (opts.userPlugins !== false) {
41 try {
42 const userPJSONPath = path.join(this.dataDir, 'package.json');
43 const pjson = this.userPJSON = util_1.loadJSONSync(userPJSONPath);
44 if (!pjson.anycli)
45 pjson.anycli = { schema: 1 };
46 this.loadPlugins(userPJSONPath, 'user', pjson.anycli.plugins);
47 }
48 catch (err) {
49 if (err.code !== 'ENOENT')
50 process.emitWarning(err);
51 }
52 }
53 debug('config done');
54 }
55 async runHook(event, opts) {
56 debug('start %s hook', event);
57 await super.runHook(event, Object.assign({}, opts || {}, { config: this }));
58 debug('done %s hook', event);
59 }
60 async runCommand(id, argv = []) {
61 debug('runCommand %s %o', id, argv);
62 const c = this.findCommand(id);
63 if (!c)
64 throw new errors_1.CLIError(`command ${id} not found`);
65 const command = c.load();
66 await this.runHook('prerun', { Command: command, argv });
67 await command.run(argv, this);
68 }
69 scopedEnvVar(k) {
70 return process.env[this.scopedEnvVarKey(k)];
71 }
72 scopedEnvVarTrue(k) {
73 let v = process.env[this.scopedEnvVarKey(k)];
74 return v === '1' || v === 'true';
75 }
76 scopedEnvVarKey(k) {
77 return [this.bin, k]
78 .map(p => p.replace(/-/g, '_'))
79 .join('_')
80 .toUpperCase();
81 }
82 dir(category) {
83 const base = process.env[`XDG_${category.toUpperCase()}_HOME`]
84 || (this.windows && process.env.LOCALAPPDATA)
85 || path.join(this.home, category === 'data' ? '.local/share' : '.' + category);
86 return path.join(base, this.dirname);
87 }
88 windowsHome() { return this.windowsHomedriveHome() || this.windowsUserprofileHome(); }
89 windowsHomedriveHome() { return (process.env.HOMEDRIVE && process.env.HOMEPATH && path.join(process.env.HOMEDRIVE, process.env.HOMEPATH)); }
90 windowsUserprofileHome() { return process.env.USERPROFILE; }
91 macosCacheDir() { return this.platform === 'darwin' && path.join(this.home, 'Library', 'Caches', this.dirname) || undefined; }
92 _shell() {
93 let shellPath;
94 const { SHELL, COMSPEC } = process.env;
95 if (SHELL) {
96 shellPath = SHELL.split('/');
97 }
98 else if (this.windows && COMSPEC) {
99 shellPath = COMSPEC.split(/\\|\//);
100 }
101 else {
102 shellPath = ['unknown'];
103 }
104 return shellPath[shellPath.length - 1];
105 }
106 _debug() {
107 if (this.scopedEnvVarTrue('DEBUG'))
108 return 1;
109 try {
110 const { enabled } = require('debug')(this.bin);
111 if (enabled)
112 return 1;
113 }
114 catch (_a) { }
115 return 0;
116 }
117}
118exports.Config = Config;
119function load(opts = (module.parent && module.parent && module.parent.parent && module.parent.parent.filename) || __dirname) {
120 if (typeof opts === 'string')
121 opts = { root: opts };
122 if (isConfig(opts))
123 return opts;
124 return new Config(opts);
125}
126exports.load = load;
127function isConfig(o) {
128 return o && !!o._base;
129}