-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·103 lines (88 loc) · 2.9 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
#!/usr/bin/env node
const csv = require('csv-parser');
const fs = require('fs');
const util = require('util');
const urlparse = require('url-parse');
const jsstring = require('javascript-stringify');
const commander = require('commander');
let res = [];
commander.version('0.0.1')
.option('-i, --input <path>', 'Input CSV file', null)
.option('-o, --output <path>', 'Output file name (Defaults to outputing to terminal)', null)
.option('-t, --type <type>', 'Output type [nginx, apache, vue]', /^(nginx|apache|vue)$/i ,null)
.option('-s, --source <name>', 'Source cell name', 'source')
.option('-d, --destination <name>', 'Destination cell name', 'destination')
.parse(process.argv);
if (commander.input === null || commander.type === null) {
console.log("You seem lost ... Here's the help");
commander.outputHelp();
return;
}
let converted = [];
function getVue(source, destination) {
return {path: source, redirect: destination};
}
function getNginx(source, destination) {
return util.format("%s\t%s;", source, destination);
}
function getApache(source, destination) {
return util.format("RedirectPermanent %s \t %s", source, destination);
}
fs.createReadStream(commander.input)
.pipe(csv())
.on('data', (data) => res.push(data))
.on('end', () => {
res.forEach((item) => {
if (!(commander.source in item) || !(commander.destination in item)) {
console.log(`panic.jpg! Malformed CSV. CSV needs to contain ${commander.source} and ${commander.destination} columns.`);
process.exit(1);
}
let parsedSource = urlparse(encodeURI(decodeURI(item[commander.source])));
let parsedDestination = urlparse(encodeURI(decodeURI(item[commander.destination])));
let finalSource = parsedSource.pathname + parsedSource.query;
let finalDestination = parsedDestination.pathname + parsedDestination.query;
if (finalDestination === "") {
finalDestination = "/";
}
if (finalSource === ""){
finalSource = "/";
}
switch (commander.type) {
case "vue":
converted.push(getVue(finalSource, finalDestination));
break;
case "nginx":
converted.push(getNginx(finalSource, finalDestination));
break;
case "apache":
converted.push(getApache(finalSource, finalDestination));
break;
}
});
let toWrite = "";
switch (commander.type) {
case "vue":
toWrite = jsstring.stringify(converted, null, " ");
break;
case "nginx":
converted.push(getNginx("default", "0"));
toWrite = "map $request_uri $redirect {\n"
.concat(converted.join("\n"),
"\n}");
break;
case "apache":
toWrite = converted.join("\n");
break;
}
if (commander.output === null) {
console.log(toWrite);
} else {
fs.writeFile(commander.output, toWrite, (err, data) => {
if (err) {
console.log("Error while writing to file : ", err);
} else {
console.log("Sucessfully wrote to file !");
}
});
}
});