forked from zumwald/better-vsts-npm-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
107 lines (97 loc) · 2.7 KB
/
cli.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
102
103
104
105
106
107
#!/usr/bin/env node
const config = require("./lib/config.js");
const task = require("./index.js");
const input = require("input");
let runningCmd = false;
// commands
const CONFIG_SETTER = argv => {
config.set(argv.key, argv.value);
return Promise.resolve();
};
const CONFIG_GETTER = argv => {
let configObj = config.get();
if (argv.key) {
configObj = configObj[argv.key];
}
console.log(configObj);
return Promise.resolve();
};
const CONFIG_DELETER = argv => {
let configObj = config.get();
let writeConfig = o => {
console.log("new config:\n", o);
config.write(o);
};
if (configObj[argv.key]) {
delete configObj[argv.key];
writeConfig(configObj);
return Promise.resolve();
} else {
// get user confirmation and then delete the whole config
return input
.confirm("Are you sure you want to delete your config file?")
.then(deleteConfig => {
if (deleteConfig) {
writeConfig({});
}
});
}
};
const commandBuilder = cmd => {
return args => {
runningCmd = true;
cmd(args).then(() => process.exit(0));
};
};
const argv = require("yargs")
.usage("Usage: $0 [command] [options]")
.example("$0", "process the local .npmrc file")
.example(
"$0 -n /foo/bar/.npmrc -c /baz/bang/.bettervstsnpmauthcfg",
"process the .npmrc file located at /foo/bar, use /baz/bang/.bettervstsnpmauthcfg as the config file"
)
.example("$0 config foo bar", 'set a config value "foo" to be "bar"')
.options("n", {
alias: "npmrcPath",
describe: "path to npmrc config",
type: "string"
})
.options("c", {
alias: "configOverride",
describe: "alternate path to this tool's configuration file",
type: "string"
})
.command({
command: "config [command]",
desc: 'modify the config (run "config --help" for more info)',
builder: yargs =>
yargs
.command({
command: "set <key> <value>",
desc: "Set a config variable",
handler: commandBuilder(CONFIG_SETTER)
})
.command({
command: "get [key]",
desc: "Get a config variable",
handler: commandBuilder(CONFIG_GETTER)
})
.command({
command: "delete [key]",
desc:
"Delete a config variable. If the variable is not supplied, deletes the entire config.",
handler: commandBuilder(CONFIG_DELETER)
}),
handler: commandBuilder(CONFIG_GETTER)
})
.help().argv;
// safety first - handle and exit non-zero if we run into issues
let abortProcess = e => {
console.log(e);
process.exit(1);
};
process.on("uncaughtException", abortProcess);
process.on("unhandledRejection", abortProcess);
if (!runningCmd) {
task.run(argv);
}