-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
470 lines (389 loc) · 15.4 KB
/
Copy pathapp.js
File metadata and controls
470 lines (389 loc) · 15.4 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env node
const program = require("commander");
const chalk = require('chalk');
const figlet = require('figlet');
const inquirer = require('inquirer');
const fs = require('fs');
const Path = require("path");
var beautify = require('js-beautify').js;
const home = require('os').homedir();
const homeConfig = Path.join(home, ".config");
const actual = Path.join(process.cwd(), "config");
const actualEusda = Path.join(actual, "eusda");
const homeEusda = Path.join(homeConfig, "eusda");
function start() {
const msn = msn => {
console.log(chalk.bold.cyan(figlet.textSync(msn, {
font: 'ANSI Shadow',
horizontalLayout: 'default',
verticalLayout: 'default'
})));
}
const question1 = () => {
const quest1 = {
name: 'configFile',
type: 'list',
message: 'Do you want to use a default configuration file? ',
choices: [
'Yes.',
'No.'
]
};
return inquirer.prompt(quest1);
}
const question2 = () => {
const quest2 = {
name: 'directory',
type: 'list',
message: 'Where will your config file be? ',
choices: [
'I want to use this directory.',
'I want to use my home directory.'
]
};
return inquirer.prompt(quest2);
}
const queryParams = (first, second) => {
if (second.directory === 'I want to use this directory.') {
process.env["NODE_CONFIG_DIR"] = actual;
const actualEusda = Path.join(actual, "eusda");
} else if (second.directory === 'I want to use my home directory.') {
process.env["NODE_CONFIG_DIR"] = homeConfig;
const homeEusda = Path.join(homeConfig, "eusda");
}
if (first.configFile === 'No.') {
console.log("Please, introduce the answers")
const data = [
{
name: 'dbHost',
type: 'input',
message: 'MongoDB address:',
default: '127.0.0.1',
validate: validateString
}, {
name: 'dbPort',
type: 'number',
message: 'MongoDB port: ',
default: 27017,
validate: validateNumber
},
{
name: 'dbName',
type: 'input',
message: 'Database name: ',
default: 'eusda',
validate: validateString
}, {
name: 'dbCollection',
type: 'input',
message: 'Database collection: ',
default: 'food',
validate: validateString
},
{
name: 'usdaUrlsDefault',
type: 'input',
message: 'Download url: ',
default: 'https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/SR/sr28/dnload/sr28abxl.zip',
validate: validateString
}, {
name: 'filesZip',
type: 'input',
message: 'Zip file name: ',
default: 'data.zip',
validate: validateString
},
{
name: 'filesJson',
type: 'input',
message: 'JSON file name:',
default: 'usda.json',
validate: validateString
}, {
name: 'dirZip',
type: 'input',
message: 'Zip directory name:',
default: 'zip',
validate: validateString
},
{
name: 'dirUnzip',
type: 'input',
message: 'Unzip directory name:',
default: 'unzip',
validate: validateString
},
{
name: 'dirJson',
type: 'input',
message: 'Json directory name:',
default: 'json',
validate: validateString
}
];
return inquirer.prompt(data);
}
if (first.configFile === 'Yes.') {
const cont = {
db:
{
host: "127.0.0.1",
port: 27017,
name: "eusda",
collectionUSDA: "food"
},
usda: {
urls: {
default: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/SR/sr28/dnload/sr28abxl.zip",
sr28: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/SR/sr28/dnload/sr28abxl.zip",
sr27: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/sr27/dnload/sr27abxl.zip",
sr26: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/sr26/dnload/sr26abxl.zip",
sr25: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/sr25/dnload/sr25abxl.zip",
sr24: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/sr24/dnload/sr24abxl.zip",
sr23: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/sr23/dnload/sr23abxl.zip",
sr22: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/sr22/dnload/sr22abxl.zip",
sr21: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/sr21/dnload/sr21abxl.zip",
sr20: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/sr20/sr20abxl.zip",
sr19: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/sr19/sr19abxl.zip",
sr18: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/sr18/sr18abxl.zip",
sr17: "https://www.ars.usda.gov/ARSUserFiles/80400535/DATA/sr17/sr17abxl.zip"
}
},
files: {
zip: "data.zip",
json: "usda.json",
dish: "dishes.json"
},
directories_path: {
zip: "./files/zip/",
unzip: "./files/unzip/",
json: "./files/json/"
}
};
if (!fs.existsSync(process.env["NODE_CONFIG_DIR"])) {
fs.mkdirSync(process.env["NODE_CONFIG_DIR"], 0777);
}
const newPath = Path.join(process.env["NODE_CONFIG_DIR"] + "/eusda");
if (!fs.existsSync(newPath)) {
fs.mkdirSync(newPath, 0777);
}
const file = Path.join(process.env["NODE_CONFIG_DIR"] + "/eusda/default.json");
fs.writeFileSync(file, beautify(JSON.stringify(cont), { indent_size: 2, space_in_empty_paren: true }));
}
createDirs();
};
(async () => {
msn('EUSDA');
const first = await question1();
const second = await question2();
const result = queryParams(first, second)
if (typeof result !== 'undefined') {
createConfig(await result);
}
// process.stdout.write("\u001b[2J\u001b[0;0H");
})();
};
const validateNumber = async (input) => {
if (typeof input !== 'number') {
console.log('\nIncorrect asnwer, it was expected a number.');
process.exit(1);
}
return true;
};
const validateString = async (input) => {
if (typeof input !== 'string') {
console.log('\nIncorrect asnwer, it was expected a number.');
process.exit(1);
}
return true;
};
const createDirs = () => {
const pathFiles = `${process.cwd()}/files`;
if (!fs.existsSync(pathFiles)) {
fs.mkdirSync(pathFiles, 0777);
const pathZip = `${process.cwd()}/files/zip`;
if (!fs.existsSync(pathZip)) {
fs.mkdirSync(pathZip, 0777);
}
const pathUnzip = `${process.cwd()}/files/unzip`;
if (!fs.existsSync(pathUnzip)) {
fs.mkdirSync(pathUnzip, 0777);
}
const pathJson = `${process.cwd()}/files/json`;
if (!fs.existsSync(pathJson)) {
fs.mkdirSync(pathJson, 0777);
}
} else if (fs.existsSync(pathFiles)) {
const pathZip = `${process.cwd()}/files/zip`;
if (!fs.existsSync(pathZip)) {
fs.mkdirSync(pathZip, 0777);
}
const pathUnzip = `${process.cwd()}/files/unzip`;
if (!fs.existsSync(pathUnzip)) {
fs.mkdirSync(pathUnzip, 0777);
}
const pathJson = `${process.cwd()}/files/json`;
if (!fs.existsSync(pathJson)) {
fs.mkdirSync(pathJson, 0777);
}
}
}
const createConfig = (data) => {
const myConfig = {
db:
{
host: data.dbHost,
port: data.dbPort,
name: data.dbName,
collectionUSDA: data.dbCollection
},
usda: {
urls: {
default: data.usdaUrlsDefault
}
},
files: {
zip: data.filesZip,
json: data.filesJson
},
directories_path: {
zip: `./files/${data.dirZip}/`,
unzip: `./files/${data.dirUnzip}/`,
json: `./files/${data.dirJson}/`
}
};
if (!fs.existsSync(process.env["NODE_CONFIG_DIR"])) {
fs.mkdirSync(process.env["NODE_CONFIG_DIR"], 0777);
}
const newPath = Path.join(process.env["NODE_CONFIG_DIR"] + "/eusda");
if (!fs.existsSync(newPath)) {
fs.mkdirSync(newPath, 0777);
}
const file = Path.join(process.env["NODE_CONFIG_DIR"] + "/eusda/default.json");
fs.writeFileSync(file, beautify(JSON.stringify(myConfig), { indent_size: 2, space_in_empty_paren: true }));
};
function init() {
if ((fs.existsSync(actual)) && (fs.existsSync(actualEusda))) {
process.env["NODE_CONFIG_DIR"] = actualEusda;
} else if ((fs.existsSync(homeConfig)) && (fs.existsSync(homeEusda))) {
process.env["NODE_CONFIG_DIR"] = homeEusda;
}
const config = require("config");
const load = require("./lib/load-data");
const urlData = require("./lib/urls-data");
program
.name("app")
.usage("command [option]");
program
.option("-v --version", "Print eusda version")
.description("Show the version.")
if (program.version !== undefined) {
const package = require("./package.json");
console.log(`eusda ${package.version}`);
}
program
.command("config")
.description("Show the config options.")
.action(function () {
console.log("\nThe configuration used is:\n");
console.log(config);
console.log("\n You can change the settings from default.json.\n");
});
const urls = config.get("usda.urls");
const version = urls.default;
const regexp = /sr+[1-9][1-9]/;
const resultVersion = version.match(regexp);
program
.command("download")
.description("Download data from USDA")
.option("-u, --usda", "USDA data version")
.option("-s, --set <url>", "Download data from another USDA url")
.action(function (opt) {
if (opt.usda === true) {
console.log("We are using " + resultVersion + " version");
}
if (opt.set !== undefined) {
load.download(opt.set);
}
if ((opt.usda === undefined) && (opt.set === undefined)) {
load.download();
}
});
program
.command("usda")
.description("Config the URLs")
.option("-a, --add <path> <version> <url>", "Add a new url to config options")
.option("-s, --show", "Show the available urls")
.option("-d, --default <path> <version>", "Set a default url")
.action(function (opt, arg1, otro) {
if (opt.show === true) {
const show = urlData.showUrls();
console.log(show);
process.exit();
}
if (opt.add !== undefined) {
const args = 7;
if ((opt.parent.rawArgs.length < args)) {
console.log("error: option '-a, --add <path> <version> <url>' argument missing");
process.exit();
} else if ((opt.parent.rawArgs.length > args)) {
console.log("error: option '-a, --add <path> <version> <url>' there are more arguments than necessary");
process.exit();
}
const index4 = 4;
const index5 = 5;
const index6 = 6;
const pathFile = process.argv[index4];
const versionName = process.argv[index5];
const newUrl = process.argv[index6];
urlData.addUrl(pathFile, versionName, newUrl);
}
if (opt.default !== undefined) {
const args = 6;
if ((opt.parent.rawArgs.length < args)) {
console.log("error: option '-d, --default <path> <version>' argument missing");
process.exit();
} else if ((opt.parent.rawArgs.length > args)) {
console.log("error: option '-d, --default <path> <version>' there are more arguments than necessary");
process.exit();
}
const index4 = 4;
const index5 = 5;
const pathFile = process.argv[index4];
const versionName = process.argv[index5];
urlData.setDefault(pathFile, versionName);
}
if ((opt.add === undefined) && (opt.default === undefined)) {
program.help();
}
});
const directorio = config.get("directories_path.json");
const archivo = config.get("files.json");
program
.command("insert")
.description(`Insert data from ${directorio}${archivo} to database.`)
.action(function () {
load.insertDatabase();
});
program.on("command:*", function () {
console.error("Invalid command: %s\nSee --help for a list of available commands.", program.args.join(" "));
process.exit();
});
program.parse(process.argv);
const comandos = 3;
if (process.argv.length < comandos) program.help();
}
if ((!fs.existsSync(Path.join(home, ".config" + "/eusda/default.json"))) && (!fs.existsSync(Path.join(process.cwd(), "config" + "/eusda/default.json")))) {
if (process.argv[2] === "--help"){
program.help();
}
if (!fs.existsSync(Path.join(process.cwd(), "files"))) {
createDirs();
}
start();
} else {
if (!fs.existsSync(Path.join(process.cwd(), "files"))) {
createDirs();
}
init();
}