forked from ruyadorno/ntl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (106 loc) · 2.39 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
'use strict';
var inquirer = require('inquirer');
var pkg = require('./package');
module.exports = function (p, exec, log, cwd, tasks, options) {
function printHelp() {
log.info(
'\nUsage:\n nti [<path>]\n' +
'\nOptions:\n -v --version Displays app version number\n' +
' -h --help Shows this help message\n' +
' -a --all Includes pre and post scripts on the list\n' +
' -m --multiple Allows a selection of multiple tasks to run at once\n' +
' -i --info Displays the contents of each script'
);
}
function printVersion() {
log.info(pkg.version);
}
function filterPrefixes(taskName) {
if (options.all) {
return true;
}
var prefixes = [
'pre',
'post'
];
return !prefixes.some(function (prefix) {
return taskName.slice(0, prefix.length) === prefix;
});
}
function onPrompt(answer) {
var values = answer.task;
values = Array.isArray(values) ? values : [values];
values.forEach(function (answer) {
exec([
'npm',
'run',
answer
].join(' '), {
cwd: cwd,
stdio: [
p.stdin,
p.stdout,
p.stderr
]
});
});
}
function showList() {
if (!tasks || !Object.keys(tasks).length) {
log.info('There are no npm scripts available here');
return;
}
log.info('Npm Task Info - v' + pkg.version);
var prompt = inquirer.createPromptModule({
input: p.stdin,
output: p.stdout
});
var promptChoices = Object.keys(tasks)
.filter(filterPrefixes)
.map(function (key) {
var name = key;
var regExc = /#(.+)/.exec(tasks[key]);
var comment = regExc && regExc.pop();
if (options.info) {
name += (options.info ? ': ' + tasks[key] : '');
} else if (comment) {
name += ' - ' + comment.trim();
}
return {
name: name,
value: key
};
});
var promptTypes = {
base: {
type: 'list',
name: 'task',
message: 'Select a task to run:',
choices: promptChoices
},
multiple: {
type: 'checkbox',
name: 'task',
message: 'Select tasks to run:',
choices: promptChoices
}
};
p.stdin.setEncoding('utf8');
p.stdin.on('data', function (chunk) {
if (chunk === '\u001b') { // ESC
p.exit(0);
}
});
return prompt(
options.multiple ? promptTypes.multiple : promptTypes.base,
onPrompt
);
}
if (options.help) {
printHelp();
} else if (options.version) {
printVersion();
} else {
return showList();
}
};