-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.js
executable file
·94 lines (84 loc) · 2.34 KB
/
command.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
#!/usr/bin/env node
var program = require('commander');
var path = require('path');
var rotatelib = require('./index.js');
var filters = require('./lib/filters.js');
var criteria = require('./lib/criteria.js');
var inquirer = require('inquirer');
function camelcase(flag) {
return flag.split('-').reduce(function(str, word) {
return str + word[0].toUpperCase() + word.slice(1);
});
}
program.version('0.0.1');
var items = {};
// register criteria options
for (var criteriaItem in criteria) {
if (criteria.hasOwnProperty(criteriaItem) && criteriaItem[0] != '_' && criteriaItem != 'default') {
if (typeof criteria[criteriaItem].option === 'function') {
var option = criteria[criteriaItem].option(program);
items[option] = criteriaItem;
}
}
}
// register filter options
for (var filterItem in filters) {
if (filters.hasOwnProperty(filterItem) && filterItem[0] != '_' && filterItem != 'default') {
if (typeof filters[filterItem].option === 'function') {
var option = filters[filterItem].option(program);
items[option] = filterItem;
}
}
}
// actions
program
.option('--remove', 'Remove matched items, prompts by default')
.option('--no-prompt', 'Do not prompt, assume yes', false);
program.parse(process.argv);
if (program.args.length != 1) {
program.outputHelp();
return;
}
var params = {
directory: program.args[0]
};
for (var item in items) {
if (items.hasOwnProperty(item)) {
if (program.hasOwnProperty(item)) {
params[items[item]] = program[item];
}
}
}
rotatelib
.list(params)
.then(function(items) {
if (program.remove) {
if (items.length === 0 && program.prompt) {
console.log('No items matched, nothing removed');
return;
}
if (!program.prompt) {
rotatelib.removeItems(items, params);
return;
}
console.log('Matched items:');
console.log(items.join('\n'));
inquirer.prompt({
name: 'confirm',
type: 'confirm',
message: 'Do you want to remove these items?'
}, function(input) {
if (input.confirm) {
rotatelib.removeItems(items, params);
}
else {
console.log('No items removed');
}
});
return;
}
// list out the items
items.forEach(function(item) {
console.log(path.join(params.directory, item));
});
});