-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·74 lines (61 loc) · 1.95 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
#!/usr/bin/env node
var chalk = require('chalk');
var packageJson =require(process.cwd() + '/package.json');
var scripts = packageJson.scripts || {};
var scriptsHelpConfig = getScriptsHelpConfig();
var search = specificScript = process.argv[2];
if (scriptsHelpConfig['help-message']){
console.log(chalk.bold.cyan('----------------------------------------------'));
console.log(getDesc(scriptsHelpConfig['help-message']));
console.log(chalk.bold.cyan('----------------------------------------------'));
}
Object.keys(scripts)
.filter(function(scriptName){
if (!search) return true;
return new RegExp(search).test(scriptName)
})
.map(getScriptHelp)
.map(printScriptHelp)
function getScriptHelp (scriptName){
var script = scripts[scriptName] || '';
var currentScriptHelp = scriptsHelpConfig[scriptName];
if (typeof currentScriptHelp === 'string' || Array.isArray(currentScriptHelp)) {
currentScriptHelp = {
"Description":getDesc(currentScriptHelp)
}
}
return Object.assign({},{
name: scriptName,
script:script
},currentScriptHelp);
}
function printScriptHelp(help){
console.log(' ');
console.log(chalk.cyan(help.name + ':'));
Object.keys(help).filter(function(key){return key !== 'name';})
.forEach(function(helpKey){
var desc = getDesc(help[helpKey]);
console.log(chalk.magenta(helpKey + ': ') + desc);
});
}
function getDesc(line){
if (!line) return '';
if (typeof line === 'string') {
return line;
}
if (Array.isArray(line) && line.length > 0 && typeof line[0] === 'string'){
return formatMultiLine(line);
}
return '';
}
function formatMultiLine(multiline){
return '\n' +multiline.join('\n');
}
function getScriptsHelpConfig(){
try{
return require(process.cwd() + '/.scriptshelprc.js');
}
catch (err) {
return packageJson['scriptshelp'] || {};
}
}