-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin
executable file
·181 lines (181 loc) · 5.66 KB
/
bin
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _interopRequireDefault = require("@swc/helpers/lib/_interop_require_default.js").default;
var _slicedToArray = require("@swc/helpers/lib/_sliced_to_array.js").default;
var _regeneratorRuntime = /*#__PURE__*/ _interopRequireDefault(require("regenerator-runtime"));
Object.defineProperty(exports, "__esModule", {
value: true
});
var tslib_1 = require("tslib");
var fs_1 = require("fs");
var debug_1 = require("./lib/utils/debug");
var mergeDeep_1 = require("./lib/utils/mergeDeep");
var tryIt_1 = require("./lib/utils/tryIt");
function sendError(error) {
console.error("\x1b[31mError\x1b[0m", error.message);
process.exit(1);
}
function getArg(usage, name, alias) {
{
var value = process.argv.slice(2).find(function(arg) {
return arg.startsWith("--".concat(name));
});
if (value) {
var ref = _slicedToArray(value.split("="), 2), value_ = ref[1];
return {
has: true,
name: name,
value: value_
};
}
}
alias: {
if (!alias) break alias;
var value1 = process.argv.slice(2).find(function(arg) {
return arg.startsWith("-".concat(alias));
});
if (value1) {
var ref1 = _slicedToArray(value1.split("="), 2), value_1 = ref1[1];
return {
has: true,
name: name,
value: value_1
};
}
}
return {
has: false,
name: name
};
}
function getStringArg(usage, name, alias) {
var arg = getArg(usage, name, alias);
if (arg.has) {
if (!arg.value) sendError(new Error("".concat(name, " has no value")));
return arg;
}
return {
has: false,
name: name
};
}
function getBooleanArg(usage, name, alias) {
var arg = getArg(usage, name, alias);
if (arg.has) {
if (!arg.value) return {
has: true,
name: name,
value: true
};
if (arg.value === "true") return {
has: true,
name: name,
value: true
};
if (arg.value === "false") return {
has: true,
name: name,
value: false
};
sendError(new Error("".concat(name, ' has invalid value (only "true", "false", or nothing)')));
}
return {
has: false,
name: name
};
}
var customConfig = {};
var configStrictLevels = [
"literal",
"strict",
"stored"
];
//#region Config
{
var setConfigArg = function setConfigArg(name, path, type) {
var arg = type === "string" ? getStringArg("", name) : getBooleanArg("", name);
if (arg.has) {
if (type === "string" && !configStrictLevels.includes(arg.value)) {
sendError(new Error("".concat(name, " has invalid value (only ").concat(configStrictLevels.join(", "), ")")));
}
deepSetter(customConfig, path, arg.value);
}
};
var setLevelArg = function setLevelArg(name) {
setConfigArg(name + ".comment", [
"levels",
name,
"comment"
], "boolean");
setConfigArg(name + ".level", [
"levels",
name,
"level"
], "string");
};
setConfigArg("minify", [
"minify"
], "boolean");
setConfigArg("ofuscate", [
"ofuscate"
], "boolean");
setConfigArg("autoRun", [
"autoRun"
], "boolean");
setLevelArg("number");
}//#endregion
var configArg = getStringArg("Set config file", "config", "c");
function main() {
return tslib_1.__awaiter(this, void 0, void 0, _regeneratorRuntime.default.mark(function _callee() {
var config;
return _regeneratorRuntime.default.wrap(function _callee$(_ctx) {
while(1)switch(_ctx.prev = _ctx.next){
case 0:
config = function() {
var config = {};
if (configArg.has) {
(0, debug_1.debugLog)("CONFIG", "Loading config:", configArg.value);
var file_ = (0, tryIt_1.tryIt)(true, function() {
return (0, fs_1.readFileSync)(configArg.value, "utf8");
});
if (file_.error) {
sendError(new Error("Cannot read config file: ".concat(configArg.value)));
}
var file = file_.value;
var config_ = (0, tryIt_1.tryIt)(true, function() {
return JSON.parse(file);
});
if (config_.error) {
sendError(new Error("Cannot parse config file: ".concat(configArg.value)));
}
config = config_.value;
(0, debug_1.debugLog)("CONFIG", "Loaded");
}
return (0, mergeDeep_1.mergeDeep)(config, customConfig);
}();
console.log(config);
case 2:
case "end":
return _ctx.stop();
}
}, _callee);
}));
}
function deepSetter(obj, path, value) {
if (path.length === 1) {
// @ts-ignore
obj[path[0]] = value;
return;
}
// @ts-ignore
if (!obj[path[0]]) {
// @ts-ignore
obj[path[0]] = {};
}
// @ts-ignore
deepSetter(obj[path[0]], path.slice(1), value);
}
main();