-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathncu-config.js
executable file
·101 lines (91 loc) · 2.5 KB
/
ncu-config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import {
getConfig, updateConfig, GLOBAL_CONFIG, PROJECT_CONFIG, LOCAL_CONFIG
} from '../lib/config.js';
import { setVerbosityFromEnv } from '../lib/verbosity.js';
setVerbosityFromEnv();
const args = yargs(hideBin(process.argv))
.completion('completion')
.command({
command: 'set <key> <value>',
desc: 'Set a config variable',
builder: (yargs) => {
yargs
.positional('key', {
describe: 'key of the configuration',
type: 'string'
})
.positional('value', {
describe: 'value of the configuration'
});
},
handler: setHandler
})
.command({
command: 'get <key>',
desc: 'Get a config variable',
builder: (yargs) => {
yargs
.positional('key', {
describe: 'key of the configuration',
type: 'string'
});
},
handler: getHandler
})
.command({
command: 'list',
desc: 'List the configurations',
handler: listHandler
})
.demandCommand(1, 'must provide a valid command')
// Can't set default of boolean variables if using conflict
// https://github.com/yargs/yargs/issues/929
// default: false
.option('global', {
alias: 'g',
type: 'boolean',
describe: 'Use global config (~/.ncurc)'
})
.option('project', {
alias: 'p',
type: 'boolean',
describe: 'Use project config (./.ncurc)'
})
.conflicts('global', 'project')
.help();
const argv = args.parse();
function getConfigType(argv) {
if (argv.global) {
return { configName: 'global', configType: GLOBAL_CONFIG };
}
if (argv.project) {
return { configName: 'project', configType: PROJECT_CONFIG };
}
return { configName: 'local', configType: LOCAL_CONFIG };
}
function setHandler(argv) {
const { configName, configType } = getConfigType(argv);
const config = getConfig(configType);
console.log(
`Updating ${configName} configuration ` +
`[${argv.key}]: ${config[argv.key]} -> ${argv.value}`);
updateConfig(configType, { [argv.key]: argv.value });
}
function getHandler(argv) {
const { configType } = getConfigType(argv);
const config = getConfig(configType);
console.log(config[argv.key]);
}
function listHandler(argv) {
const { configType } = getConfigType(argv);
const config = getConfig(configType);
for (const key of Object.keys(config)) {
console.log(`${key}: ${config[key]}`);
}
}
if (!['get', 'set', 'list'].includes(argv._[0])) {
args.showHelp();
}