-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (92 loc) · 2.72 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
const fs = require('fs');
const Fuse = require('@codefeathers/fuse');
const argvParser = require('minimist');
const requireAll = require('require-all');
const tryCatch = (fn, args) => {
try {
return fn(args);
} catch (e) {
return ['Failed', e];
}
}
const Errors = [
'Not a file or directory path',
'Unprocessable entity'
]
const fileOrDir = path => {
let stat = tryCatch(fs.lstatSync, path);
if (stat[0] === 'Failed') return Errors[0];
if (stat.isFile()) return 'file';
if (stat.isDirectory()) return 'dir';
return Errors[0];
}
const findActionType = action =>
(typeof action === 'object' && !Array.isArray(action)) ?
'hash' :
(typeof action === 'string') ?
fileOrDir(action) :
Errors[1];
class CMDjs {
constructor(hash, options) {
this.hash = hash;
}
use(action) {
const type = findActionType(action);
new Fuse(type)
.on(x => Errors.find(y => y === x), _ => { throw new TypeError(`${action} : ${_}`) })
.is('hash', _ => { this.hash = { ...this.hash, ...action } })
.is('file', _ => this.use(require(action)))
.is('dir', _ => {
const all = requireAll({
dirname: action,
recursive: false
});
Object.keys(all).forEach(item => this.use(action + '/' + item + '.js'));
})
.resolve();
return this;
}
start(proc) {
const { log } = console;
const args = proc.argv.slice(2);
const argString = args.join(" ");
// Only for consumer convenience
const argmin = argvParser(args);
const match = (keys, argString) => {
const matched = keys.find(source => {
const split = source.split(",").map(x => x.trim());
return split.find(x => argString.startsWith(x)) !== undefined;
});
if (matched) {
const distinct = matched.split(",").map(x => x.trim()).find(x => argString.startsWith(x));
const rest = argString.slice(distinct.length).trim();
return { key: matched, distinct, rest };
}
};
const recurse = (hash, keys, argString) => {
const matched = match(keys, argString);
const unmatched = { unmatched: true, hash };
if (matched === undefined) return unmatched;
if (matched && matched.key) {
if (matched.rest
&& typeof hash[matched.key] !== 'object') return unmatched;
if (typeof hash[matched.key] === 'function') return hash[matched.key](argmin);
if (typeof hash[matched.key] === 'object') return recurse(
hash[matched.key],
Object.keys(hash[matched.key]),
matched.rest
)
}
return unmatched;
};
const keys = Object.keys(this.hash);
const res = recurse(this.hash, keys, argString);
if (res && res.unmatched && this.hash['@default']) {
if(res.hash) res.hash['@default'](argmin);
else this.hash['@default'](argmin);
return { status: 'default' };
}
return { status: 'ok' };
}
}
module.exports = CMDjs;