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