-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
163 lines (134 loc) · 4.04 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
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
const shell = require("child_process");
const fs = require("fs");
// Load cli
let cli = require("./lib/cli")();
let config = new (require("./lib/config"))();
// Check if we need to create a config
if (cli.createConfig) {
config.create();
process.exit();
}
// Get the existing config
try {
config = config.get();
}
catch (ex) {
console.log("Could not load config.");
process.exit();
}
// Remove the directory first if -f is set
if (cli.force && config.output && config.output.directory) {
console.log("Cleaning...");
shell.execSync("rm -rf " + config.output.directory)
}
// Start the engine
// Run the precmd
if (config.precmd && config.precmd.length) {
console.log("Pre-cmd:", config.precmd);
shell.execSync(config.precmd);
}
/// Check for input server command
let server = false;
let serverStarted = false;
let downloadStarted = false;
if (config.input && config.input.directory && config.input.runcmd) {
// Run the command
console.log("Starting server...");
let cmd = config.input.runcmd;
server = shell.exec(cmd, {
"cwd": config.input.directory
});
server.stdout.on("data", (data) => {
serverStarted = true;
download();
server.kill();
});
server.stderr.on("data", (data) => {
console.log(`Server Error: ${data}`);
server.kill();
});
server.on("close", (code) => {
console.log(`Server exited with code ${code}`);
process.exit();
})
server.on("exit", (code) => {
console.log(`Server exited with code ${code}`);
process.exit();
})
}
/**
* Run the download
*/
function download() {
if (downloadStarted) {
return;
}
else {
downloadStarted = true;
}
/// Create wget options
let wgetOptions = "";
if (config.options && config.options.length) {
wgetOptions = config.options.join(" ") + " ";
}
if (config.output && config.output.directory) {
wgetOptions += "--directory-prefix=" + config.output.directory + " ";
wgetOptions += "--no-host-directories ";
}
let wgetDomains = "";
if (config.domains && config.domains.length) {
wgetDomains = config.domains.join(" ");
wgetOptions += "-d " + wgetDomains + " ";
}
let wgetcmd = "";
if (wgetOptions.length && wgetDomains.length) {
wgetcmd = "wget " + wgetOptions + wgetDomains
}
else if (!wgetOptions.length) {
throw "Could not read options";
}
else if (!wgetDomains.length) {
throw "Could not read domains.";
}
/// Run wget
if (wgetcmd.length && serverStarted) {
console.log("Running " + wgetcmd);
try {
try {
shell.execSync(wgetcmd);
}
catch (ex) {
console.log("Wget exited unexpectedly. Please check output before committing.");
}
// Run the post command
if (config.postcmd && config.postcmd.length) {
console.log("Post-cmd:", config.postcmd);
shell.execSync(config.postcmd);
}
// Fix dot ones
if (config.output && config.output.fixDotOnes) {
// Scan the directory
fs.readdirSync(config.output.directory).forEach(file => {
if (file.includes(".1.html")) {
file = config.output.directory + '/' + file;
let newfile = file.replace(".1.html", ".html");
try {
fs.renameSync(file, newfile);
}
catch (ex) {
throw "Could not move file " + file;
}
}
});
}
console.log("Done.");
}
catch (ex) {
console.log("Error: ", ex);
}
}
else {
// Server not started
console.log("Server wasn't started.");
}
}