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