UNPKG

1.97 kBJavaScriptView Raw
1"use strict";
2const fs = require("fs");
3const JSON5 = require("json5");
4const cfg_1 = require("../../commons/cfg");
5const applyGlobalGroup_1 = require("../../fns/add-cmd/applyGlobalGroup");
6const cmdName_1 = require("../../fns/cmdName");
7const ConfigWriter_1 = require("../../lib/ConfigWriter");
8const Crypt_1 = require("../../lib/Crypt");
9const PromptableConfig_1 = require("../../lib/PromptableConfig");
10function tryJson5Parse(v) {
11 try {
12 return JSON5.parse(v);
13 }
14 catch (_a) {
15 return v;
16 }
17}
18const 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};
56module.exports = cmd;