-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
217 lines (204 loc) · 8.2 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Richard Wen
// rrwen.dev@gmail.com
var fs = require('fs');
/**
* Yargs command for managing config files.
*
* * {@link https://www.npmjs.com/package/yargs Yargs npm Package}
* * {@link https://www.json.org/ JavaScript Object Notation (JSON)}
*
* @module config
* @param {Object} [options={}] options for this function.
* @param {string} options.file default path to JSON config file for yargs.
*
* * The command line argument `[--config]` will take priority over `options.file`
*
* @param {string} [options.command='config'] name of base `<config>` command.
*
* 1. **Original**: `config <task> [key] [value] [--config]`
* 2. **options.command='newconfig':** `newconfig <task> [key] [value] [--config]`
*
* @param {Object} [options.defaults={}] default config object to be used.
*
* * If `options.defaults` is undefined, the object `argv.config` will be used before defaulting to `{}`
*
* @param {string} [options.describe='describe'] description for base `<config>` command.
* @param {Object} [options.task={}] options for `<task>` commands.
* @param {Object} [options.task.command='task'] name of `<task>` command.
*
* 1. **Original**: `config <task> [key] [value] [--config]`
* 2. **options.task.command='newtask':** `config <newtask> [key] [value] [--config]`
*
* @param {string} [options.task.key='key'] name of optional `[key]` argument.
*
* 1. **Original**: `config <task> [key] [value] [--config]`
* 2. **options.task.key='newkey':** `config <task> [newkey] [value] [--config]`
*
* @param {string} [options.task.value='value'] name of optional `[value]` argument.
*
* 1. **Original**: `config <task> [key] [value] [--config]`
* 2. **options.task.value='newvalue':** `config <task> [key] [newvalue] [--config]`
*
* @param {string} [options.task.config='config'] name of optional `[--config]` argument.
*
* 1. **Original**: `config <task> [key] [value] [--config]`
* 2. **options.task.config='newconfig':** `config <task> [key] [value] [--newconfig]`
*
* @param {Object} [options.task.reset='reset'] name of `<task`> command for `reset`.
*
* 1. **Original**: `config reset [--config]`
* 2. **options.task.reset='newreset':** `config newreset [--config]`
*
* @param {Object} [options.task.clear='clear'] name of `<task`> command for `clear`.
*
* 1. **Original**: `config clear [--config]`
* 2. **options.task.reset='newclear':** `config newclear [--config]`
*
* @param {Object} [options.task.view='view'] name of `<task`> command for `view`.
*
* 1. **Original**: `config view [--config]`
* 2. **options.task.view='newview':** `config newview [--config]`
*
* @param {Object} [options.task.delete='delete'] name of `<task`> command for `delete`.
*
* 1. **Original**: `config delete [--config]`
* 2. **options.task.delete='newdelete':** `config newdelete [key] [--config]`
*
* @param {Object} [options.task.set='set'] name of `<task`> command for `set`.
*
* 1. **Original**: `config set [--config]`
* 2. **options.task.set='newset':** `config newset [key] [value] [--config]`
*
* @returns {Object} Yargs {@link https://github.com/yargs/yargs/blob/master/docs/advanced.md#providing-a-command-module Command Module} with the following properties (`out` is the returned Object):
*
* * `out.command`: the command string in the form of `options.command <options.task.command> [options.task.key] [options.task.value] [--options.task.config]`
* * `out.describe`: the description string for `out.command`
* * `out.handler`: the function that manages the config file and returns an `argv` Object containing command line arguments
*
* @example
* // *** DEFAULT ***
*
* var yargs = require('yargs');
*
* // (config) Load command with path to config JSON file
* var config = require('yargs-command-config')({file: './path/to/config.json'});
*
* // (yargs) Add command to manage config file
* var argv = yargs.command(config).argv;
*
* // *** CUSTOM ***
*
* var yargs = require('yargs');
*
* // (options_command) Setup command options
* options = {};
* options.command = 'config2';
* options.defaults = {field: 'value'};
* options.describe = 'Description';
*
* // (options_task) Setup task options
* options.task = {};
* options.task.command = 'task2';
* options.task.key = 'key2';
* options.task.value = 'value2';
* options.task.config = 'config2';
* options.task.reset = 'reset2';
* options.task.clear = 'clear2';
* options.task.view = 'view2';
* options.task.delete = 'delete2';
* options.task.set = 'set2';
*
* // (config) Load command with options
* var config = require('yargs-command-config')(options);
*
* // (yargs) Add command to manage config file
* var argv = yargs.command(config).argv;
*
*/
module.exports = function(options) {
options = options || {};
// (default_task) Default task options
options.task = options.task || {};
options.task.command = options.task.command || 'task' ;
options.task.key = options.task.key || 'key';
options.task.value = options.task.value || 'value';
options.task.config = options.task.config || 'config';
options.task.reset = options.task.reset || 'reset';
options.task.clear = options.task.clear || 'clear';
options.task.view = options.task.view || 'view';
options.task.delete = options.task.delete || 'delete';
options.task.set = options.task.set || 'set';
// (default) Default options
options.command = options.command || 'config';
options.describe = options.describe ||
'manage default config' +
'\n\n<' + options.task.command + '> is one of:' +
'\n\n* ' + options.task.set +
'\n* ' + options.task.delete +
'\n* ' + options.task.view +
'\n* ' + options.task.clear +
'\n* ' + options.task.reset +
'\n\nSet option to value' +
'\n> ' + options.task.set + ' [' + options.task.key + '] [' + options.task.value + ']' +
'\n\nRemove default option' +
'\n> ' + options.task.delete + ' [' + options.task.key + ']' +
'\n\nView default options' +
'\n> ' + options.task.view +
'\n\nClear default options' +
'\n> ' + options.task.clear +
'\n\nReset default options' +
'\n> ' + options.task.reset +
'\n\nManage other config file' +
'\n> ' + options.task.set + ' [' + options.task.key + '] [' + options.task.value + ']' + ' --' + options.task.config + ' other.json' +
'\n> ' + options.task.delete + ' [' + options.task.key + ']' + ' --' + options.task.config + ' other.json' +
'\n> ' + options.task.view + ' --' + options.task.config + ' other.json' +
'\n> ' + options.task.clear + ' --' + options.task.config + ' other.json' +
'\n> ' + options.task.reset + ' --' + options.task.config + ' other.json';
// (command_module) Create yargs command module
var out = {};
out.command = options.command + ' <' + options.task.command + '> [' + options.task.key + '] [' + options.task.value + '] [--' + options.task.config + ']';
out.describe = options.describe;
out.handler = function(argv) {
var task = argv[options.task.command];
var file = argv[options.task.config] || options.file;
var defaults = options.defaults || {};
// (json_read) Read json file or create if not exists
if (!fs.existsSync(file)) {
fs.writeFileSync(file, JSON.stringify(defaults));
}
var json = JSON.parse(fs.readFileSync(file));
// (json_reset) Reset json file to defaults
if (task == options.task.reset) {
json = defaults;
fs.writeFileSync(file, JSON.stringify(json));
console.log('Reset defaults');
}
// (json_clear) Clear json file
if (task == options.task.clear) {
json = {};
fs.writeFileSync(file, JSON.stringify(json));
console.log('Empty defaults');
}
// (json_view) View defaults
if (task == options.task.view) {
console.log(JSON.stringify(json, null, 2));
}
// (json_delete) Delete key from json file
if (task == options.task.delete) {
delete json[argv[options.task.key]];
delete argv[argv[options.task.key]];
fs.writeFileSync(file, JSON.stringify(json));
console.log('Delete default', argv[options.task.key]);
}
// (json_set) Set key to value for json file
if (task == options.task.set) {
json[argv[options.task.key]] = argv[options.task.value];
fs.writeFileSync(file, JSON.stringify(json));
console.log('Set default', argv[options.task.key], 'to', argv[options.task.value]);
}
// (argv_return) Update argv with json
Object.assign(argv, json);
return argv;
};
return(out);
};