-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcli
executable file
·288 lines (267 loc) · 7.88 KB
/
cli
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#!/usr/bin/env node
'use strict';
const GENERATOR = require('./index');
const MEOW = require('meow');
const YAML = require('yamljs');
const options = [
{
name: 'oilOutpost',
alias: 'oil',
description: 'Generate an oil outpost instead of an ore outpost'
},
{
name: 'trainSide',
description: '0-3, side the train station should be on (default 1 right)'
},
{
name: 'trainDirection',
description: '0-3, side train station should enter from (Must be perpendicular to trainSide) (default 2 bottom)'
},
{
name: 'beacons',
description: 'Booleann, whether to use beacons (default false)'
},
{
name: 'minerSpace',
alias: 'miner',
description: '0-2, space between miners (default 1)'
},
{
name: 'includeRadar',
alias: 'radar',
description: 'Boolean, whether or not to include a radar (default true)',
},
{
name: 'turretSpacing',
alias: null,
description: '2-9, spacing between turrets on wall (default 8)'
},
{
name: 'turrets',
alias: null,
description: 'Boolean, whether turrets are enabled or not (default true)'
},
{
name: 'laserTurrets',
alias: 'laser',
description: 'Boolean, use laser turrets instead of gun turrets (default true)'
},
{
name: 'includeTrainStation',
alias: 'withTrain',
description: 'Boolean, whether or not to include a train station (default true)'
},
{
name: 'locomotiveCount',
alias: 'locomotives',
description: '1+, number of locomotives before cargo wagons (default 2)'
},
{
name: 'cargoWagonCount',
alias: 'wagons',
description: '1+, number of cargo wagons on train (default 4)'
},
{
name: 'exitRoute',
alias: 'exit',
description: 'true/false, whether or not there is a route past the train station for single-headed trains (default false)'
},
{
name: 'walls',
alias: null,
description: 'Whether or not to include walls (default true)'
},
{
name: 'wallSpace',
alias: null,
description: 'Space between wall and rest of the outpost (default 5)'
},
{
name: 'wallThickness',
alias: null,
description: 'Number of walls thick the outpost defenses are (default 1)'
},
{
name: 'undergroundBelts',
alias: null,
description: 'Use underground belts in front of miners instead of regular belts'
},
{
name: 'beltName',
alias: 'belt',
description: 'Name of type of belt in the format `type` or `type_transport_belt` (default `` which is yellow belt)'
},
{
name: 'useStackInserters',
alias: 'useStack',
description: 'Boolean, use stack inserters between the buffer chests and cargo wagon instead of fast inserters (default true)'
},
{
name: 'botBased',
alias: 'bots',
description: 'Boolean, use passive provider and requester chests (default false)'
},
{
name: 'requestItem',
alias: null,
description: 'Item for requester chests to request if botBased (default iron_ore)'
},
{
name: 'requestAmount',
alias: null,
description: 'The amount of items each chest requests when bot based (default 4800)'
},
{
name: 'conrete',
alias: null,
description: 'Name of concrete type (in vanilla either `concrete` or `hazard_conrete`) (default none)'
},
{
name: 'borderConcrete',
alias: null,
description: 'Type of concrete put on the wall and just inside the walls (default none)'
},
{
name: 'trackConrete',
alias: null,
description: 'Type of concrete put on the track and just surrounding the track (default none)'
},
{
name: 'balancer',
alias: null,
description: 'Blueprint string for an NxN balancer if the script does not have any available (N being the # of cargo wagons) (no default)'
},
{
name: 'pumpjackName',
alias: null,
description: 'Name of modded pumpjack (not needed if using vanilla pumpjacks)'
},
{
name: 'includeLights',
alias: 'lights',
description: 'Include lights in the outpost'
},
{
name: 'useFile',
alias: 'file',
description: 'Use Yaml File for config instead of cli arguments'
}];
function validateData(name, data) {
switch (name) {
case 'includeLights':
return true;
case 'minedOreDirection':
case 'trainDirection': {
if (parseInt(data) < 0 || parseInt(data) > 3) {
showMessageAndExit(name + ' needs to be between 0 and 3')
}
break;
}
case 'minerSpace': {
if (parseInt(data) < 0 || parseInt(data) > 2) {
showMessageAndExit(name + ' needs to be between 0 and 2')
}
break;
}
case 'turretSpacing': {
if (parseInt(data) < 2 || parseInt(data) > 9) {
showMessageAndExit(name + ' needs to be between 2 and 9')
}
break;
}
case 'requestAmount':
case 'cargoWagonCount':
case 'locomotiveCount': {
if (parseInt(data) <= 0) {
showMessageAndExit(name + ' needs to be higher than 0')
}
break;
}
default: {
return data;
}
}
return data;
}
function showMessageAndExit(message, code = 1) {
if (Array.isArray(message)) {
message.forEach(function (val) {
console.error(val);
})
} else {
console.error(message);
}
process.exit(code);
}
let usageOptions = '';
let aliases = {};
options.forEach(function (val) {
let alias = '';
if (val.alias !== null) {
alias = ' -' + val.alias;
aliases[val.alias] = val.name;
}
usageOptions += `--${val.name},${alias} ${val.description}
`;
});
const cli = MEOW(`
Usage
$ cli blueprintstring
Options
${usageOptions}
Examples
$ cli 0eNqNj8EKwj... --minedOreDirection=1 --radar=true --locomotives=1 --wagons=2
$ cli 0eNqNj8EKwj... --useFile=config.yaml`, {
alias: aliases
});
let blueprint = '';
//if no input (arguments without -- prefixed) show help and exit the program
if (cli.input.length === 0) {
cli.showHelp();
return;
} else {
blueprint = cli.input[0]
}
let opts = {};
//load arguments from cli object
let args = cli.flags;
//check if useFile argument is available
if (args.hasOwnProperty('useFile')) {
try {
//load arguments from yaml file instead of cli arguments
args = YAML.load(args['useFile']);
} catch (e) {
showMessageAndExit(e.message);
}
}
if(args.hasOwnProperty('blueprint')) {
blueprint = args['blueprint'];
delete args['blueprint'];
}
options.forEach(function (val) {
if (args.hasOwnProperty(val.name)) {
opts[val.name] = validateData(val.name, args[val.name]);
//delete processed keys from arguments array
delete args[val.name]
delete args[val.alias]
}
});
//if arguments has still elements (arguments that are not in the options array)
if (Object.keys(args).length >= 1) {
let messages = [];
for( let key in args) {
if(!args.hasOwnProperty(key)) {
continue;
}
//push new message to array
messages.push(`Invalid Option: ${key}`);
}
//show all invalid options and exit the program
showMessageAndExit(messages);
}
try {
global.console = new console.Console({ stdout: process.stderr, stderr: process.stderr });
process.stdout.write(opts.oilOutpost ? GENERATOR.oilOutpost(blueprint, opts) : GENERATOR.outpost(blueprint, opts));
} catch (e) {
console.log(e.message);
}