1 | "use strict";
|
2 | const fs = require("fs");
|
3 | const JSON5 = require("json5");
|
4 | const cfg_1 = require("../../commons/cfg");
|
5 | const applyGlobalGroup_1 = require("../../fns/add-cmd/applyGlobalGroup");
|
6 | const cmdName_1 = require("../../fns/cmdName");
|
7 | const ConfigWriter_1 = require("../../lib/ConfigWriter");
|
8 | const Crypt_1 = require("../../lib/Crypt");
|
9 | const PromptableConfig_1 = require("../../lib/PromptableConfig");
|
10 | function tryJson5Parse(v) {
|
11 | try {
|
12 | return JSON5.parse(v);
|
13 | }
|
14 | catch (_a) {
|
15 | return v;
|
16 | }
|
17 | }
|
18 | const cmd = {
|
19 | builder(argv) {
|
20 | cfg_1.addCfgKey(argv);
|
21 | applyGlobalGroup_1.applyGlobalGroup(argv);
|
22 | cfg_1.addEncrypt(argv);
|
23 | argv.option('from-file', {
|
24 | default: false,
|
25 | describe: 'Get the value from a file instead of the positional argument. '
|
26 | + 'The positional value argument then acts as a filepath.',
|
27 | type: 'boolean'
|
28 | });
|
29 | cfg_1.addPwd(argv);
|
30 | argv.positional('value', {
|
31 | coerce: tryJson5Parse,
|
32 | describe: 'Config value. Can be a JSON5-parseable item. Optional only if the --stdin option is present.'
|
33 | });
|
34 | return cfg_1.addCfgScope(argv);
|
35 | },
|
36 | command: `${cmdName_1.cmdName(__filename)} <key> [value] [scope]`,
|
37 | describe: 'Set a config option shared by all projects',
|
38 | handler(c$) {
|
39 | if (c$.fromFile) {
|
40 | c$.value = tryJson5Parse(fs.readFileSync(c$.value, 'utf8'));
|
41 | }
|
42 | let val;
|
43 | if (c$.encrypt) {
|
44 | if (typeof c$.value !== 'string') {
|
45 | throw new Error('Only strings can be encrypted');
|
46 | }
|
47 | const c = new PromptableConfig_1.PromptableConfig(c$);
|
48 | val = Crypt_1.Crypt.encryptVar(c$.value, c.promptedEncryptionPassword());
|
49 | }
|
50 | else {
|
51 | val = c$.value;
|
52 | }
|
53 | new ConfigWriter_1.ConfigWriter().set(c$.key, val, c$.scope).save();
|
54 | }
|
55 | };
|
56 | module.exports = cmd;
|