-
Notifications
You must be signed in to change notification settings - Fork 0
/
index
executable file
·98 lines (83 loc) · 2.28 KB
/
index
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
#! /usr/bin/env node
/*
* Core deps
*/
var spawnSync = require('child_process').spawnSync;
var repl = require('repl');
var fs = require('fs');
var path= require('path');
/*
* Mod deps
*/
var argv = require('minimist')(process.argv.slice(2));
var filters = ['h', 'help', 'install'];
var mod = isTest() ? module.exports : {};
function isTest() {
return process.env.NODE_ENV === 'test';
}
var getopts = mod.getopts = function(argv, filter) {
var opts = Object.keys(argv).filter(function(arg) {
return !(filter.some(function(val) {
return arg == val;
}));
});
return opts.reduce(function(acc, opt) {
acc[opt] = argv[opt];
return acc;
}, {});
};
var assign = mod.assign = function(opts) {
return opts.reduce(function(acc, opt) {
acc[opt.replace('-', '_')] = opt;
return acc;
}, {});
};
var merge = mod.merge = function() {
var args = Array.prototype.slice.call(arguments);
return args.reduce(function(acc, arg) {
Object.keys(arg).forEach(function(ar) {
acc[ar] = arg[ar];
return acc;
});
return acc;
}, {});
};
function installAssign(varname, optsObj) {
try {
var install = spawnSync('npm', ['install', optsObj[varname]]);
console.log(install.output.toString()); // eslint-disable-line
} catch (e) {
console.log('Failed to install', e); // eslint-disable-line
}
global[varname] = require(path.resolve(process.cwd(), 'node_modules', optsObj[varname]));
}
function gAssign(varname, optsObj) {
global[varname] = require(path.resolve(process.cwd(), 'node_modules', optsObj[varname]));
}
function init(argv) {
// Grunt works
var hasInstall = Boolean(argv.install);
var optsObj = getopts(argv || [], filters);
var namedOptsObj = getopts(optsObj, ['_']);
var selfOptsObj = assign(optsObj._ || []);
optsObj = merge(selfOptsObj, namedOptsObj);
var ops = hasInstall ? installAssign : gAssign;
Object.keys(optsObj).forEach(function(key) {
ops(key, optsObj);
});
return true;
}
function choices(choice) {
/* eslint-disable */
switch (choice) {
case true:
fs.createReadStream(path.resolve(__dirname,'./README.md')).pipe(process.stdout);
break;
default:
isTest() || (init(argv) && repl.start('>'));
}
/* eslint-enable */
}
(function run() {
choices(argv.h || argv.help);
}());