Skip to content

Commit 21a1c93

Browse files
committed
First working build
1 parent 4f76cca commit 21a1c93

5 files changed

Lines changed: 116 additions & 2 deletions

File tree

bin/htmlbaker.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/user/bin/env node
2+
3+
var htmlbaker = require("../index.js");

docs/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express</p></body></html>

docs/stylesheets/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
body {
2+
padding: 50px;
3+
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
4+
}
5+
6+
a {
7+
color: #00B7FF;
8+
}

index.js

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,110 @@ catch (ex) {
1919
process.exit();
2020
}
2121

22+
// Remove the directory first if -f is set
23+
if (cli.force && config.output && config.output.directory) {
24+
console.log("Cleaning...");
25+
shell.execSync("rm -rf " + config.output.directory)
26+
}
27+
2228
// Start the engine
2329

2430
/// Check for input server command
31+
let server = false;
32+
let serverStarted = false;
33+
let downloadStarted = false;
2534
if (config.input && config.input.directory && config.input.runcmd) {
2635

2736
// Run the command
2837
console.log("Starting server...");
29-
let cmd = "cd " + config.input.directory + " && " + config.input.runcmd;
30-
shell.execSync(cmd);
38+
let cmd = config.input.runcmd;
39+
server = shell.exec(cmd, {
40+
"cwd": config.input.directory
41+
});
42+
43+
server.stdout.on("data", (data) => {
44+
serverStarted = true;
45+
download();
46+
});
47+
server.stderr.on("data", (data) => {
48+
console.log(`Server Error: ${data}`);
49+
process.kill();
50+
});
51+
server.on("close", (code) => {
52+
console.log(`Server exited with code ${code}`);
53+
serverStarted = false;
54+
process.kill();
55+
})
56+
server.on("exit", (code) => {
57+
console.log(`Server exited with code ${code}`);
58+
serverStarted = false;
59+
process.kill();
60+
})
3161

3262
}
63+
64+
/**
65+
* Run the download
66+
*/
67+
function download() {
68+
69+
if (downloadStarted) {
70+
return;
71+
}
72+
else {
73+
downloadStarted = true;
74+
}
75+
76+
/// Create wget options
77+
let wgetOptions = "";
78+
if (config.options && config.options.length) {
79+
wgetOptions = config.options.join(" ") + " ";
80+
}
81+
82+
if (config.output && config.output.directory) {
83+
wgetOptions += "--directory-prefix=" + config.output.directory + " ";
84+
wgetOptions += "--no-host-directories ";
85+
}
86+
87+
let wgetDomains = "";
88+
if (config.domains && config.domains.length) {
89+
wgetDomains = config.domains.join(" ");
90+
91+
wgetOptions += "-d " + wgetDomains + " ";
92+
}
93+
94+
let wgetcmd = "";
95+
if (wgetOptions.length && wgetDomains.length) {
96+
wgetcmd = "wget " + wgetOptions + wgetDomains
97+
}
98+
else if (!wgetOptions.length) {
99+
throw "Could not read options";
100+
}
101+
else if (!wgetDomains.length) {
102+
throw "Could not read domains.";
103+
}
104+
105+
/// Run wget
106+
if (wgetcmd.length && serverStarted) {
107+
console.log("Running " + wgetcmd);
108+
109+
try {
110+
shell.execSync(wgetcmd);
111+
console.log("Done.");
112+
}
113+
catch (ex) {
114+
console.log("Error: ", ex);
115+
}
116+
117+
}
118+
else {
119+
// Server not started
120+
console.log("Server wasn't started.");
121+
}
122+
123+
if (server) {
124+
server.kill();
125+
}
126+
process.exit();
127+
128+
}

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
"version": "0.0.1",
44
"description": "Convert websites into flat-file websites",
55
"main": "index.js",
6+
"bin": {
7+
"html-baker": "bin/htmlbaker.js"
8+
},
9+
"directories": {
10+
"lib": "lib"
11+
},
612
"scripts": {
713
"start": "node index.js"
814
},

0 commit comments

Comments
 (0)