-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
96 lines (80 loc) · 2.07 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
#!/usr/bin/env node
const args = process.argv.slice(2);
if (!args.length) {
console.log("bhic <file> [--debug or -d] [--verbose or -vb] [--set variableName variableValue]*");
process.exit();
}
const moment = require("moment");
const Environment = require("./src/environment");
const Pipe = require("./src/commands/pipe/index");
const Log = require("./src/log");
let file;
let cwd;
let command = null;
const settings = {
verbose: false,
debug: false,
};
const initialVariables = {
timestamp: () => moment().format("YYYYMMDDHHmmss"),
date: format => moment().format(format || "YYYY-MM-DD"),
time: format => moment().format(format || "HHmmss"),
};
for (let i = 0; i < args.length; i++) {
let a = args[i];
if (a[0] === "-") {
a = a.toLowerCase();
if (a === "--set") {
const variable = args[++i];
const variableName = variable.substr(0, variable.indexOf("=")).trim();
const variableValue = variable.substr(variable.indexOf("=") + 1).trim();
initialVariables[variableName] = variableValue;
}
else if (a === "--debug" || a === "-d") {
settings.debug = true;
Log.debug("Debug mode on");
}
else if (a === "--verbose" || a === "-vb") {
settings.verbose = true;
Log.verbose("Verbose mode on");
}
else if (a === "--command" || a === "-c") {
command = "";
for (let j = i + 1; j < args.length; j++) {
command += ` ${args[j]}`;
}
command = command.trim();
if (settings.verbose || settings.debug) {
Log.debug(`Command: ${command}`);
}
break;
}
}
else if (!file) {
file = a;
}
else if (!cwd) {
cwd = a;
}
else {
Log.error("Wrong number of parameters.");
break;
}
}
cwd = cwd || process.cwd();
const env = new Environment(cwd, settings);
env.setFromProcess(process);
env.setVariables(initialVariables, false);
const pipe = new Pipe(env);
let thread;
if (command) {
thread = pipe.load(command);
}
else {
thread = pipe.loadFromFile(file);
}
thread.then(() => {
Log.success("Program finished");
}, err => {
Log.error(err);
});