Skip to content

Commit a0e6180

Browse files
authored
Merge pull request #1 from StatelessSoftware/v0.0.1
V0.0.1
2 parents acd0e64 + 21a1c93 commit a0e6180

10 files changed

Lines changed: 351 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ typings/
5757
# dotenv environment variables file
5858
.env
5959

60+
# Test folders
61+
/config/
62+
/dist/

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");

config.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"baker": {
3+
"domains": [
4+
"localhost:3000"
5+
],
6+
"input": {
7+
"directory": "./dist",
8+
"runcmd": "npm start"
9+
},
10+
"output": {
11+
"directory": "./docs"
12+
},
13+
"options": [
14+
"recursive",
15+
"page-requisites",
16+
"html-extension",
17+
"convert-links",
18+
"restrict-file-names=windows",
19+
"no-parent"
20+
]
21+
}
22+
}

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: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
const shell = require("child_process");
2+
3+
// Load cli
4+
let cli = require("./lib/cli")();
5+
let config = new (require("./lib/config"))();
6+
7+
// Check if we need to create a config
8+
if (cli.createConfig) {
9+
config.create();
10+
process.exit();
11+
}
12+
13+
// Get the existing config
14+
try {
15+
config = config.get();
16+
}
17+
catch (ex) {
18+
console.log("Could not load config.");
19+
process.exit();
20+
}
21+
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+
28+
// Start the engine
29+
30+
/// Check for input server command
31+
let server = false;
32+
let serverStarted = false;
33+
let downloadStarted = false;
34+
if (config.input && config.input.directory && config.input.runcmd) {
35+
36+
// Run the command
37+
console.log("Starting server...");
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+
})
61+
62+
}
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+
}

lib/cli.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const getCommandLine = require("command-line-args");
2+
3+
module.exports = function() {
4+
return getCommandLine([
5+
{
6+
name: "createConfig",
7+
alias: "c",
8+
type: Boolean,
9+
},
10+
{
11+
name: "force",
12+
alias: "f",
13+
type: Boolean,
14+
}
15+
]);
16+
};

lib/config.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const env = process.env.NODE_ENV || 'default';
2+
const suppress = process.env.SUPPRESS_NO_CONFIG_WARNING = 'y';
3+
4+
const shell = require("child_process");
5+
const config = require("config");
6+
const path = require("path");
7+
const fs = require("fs");
8+
9+
module.exports = function() {
10+
11+
/**
12+
* Create a new config
13+
*/
14+
this.create = function() {
15+
16+
let file = path.normalize("config/" + env + ".json");
17+
let dir = path.dirname(file);
18+
19+
// Check for config dir
20+
if (!fs.existsSync("config")) {
21+
shell.execSync("mkdir " + dir);
22+
}
23+
24+
// Check for environment config file
25+
if (!fs.existsSync(file)) {
26+
let defaultConfig = require("../config.json");
27+
28+
try {
29+
// Create file
30+
fs.writeFileSync(file,
31+
JSON.stringify(
32+
defaultConfig,
33+
null,
34+
'\t'
35+
)
36+
);
37+
}
38+
catch (ex) {
39+
throw "Could not create configuration file.";
40+
}
41+
42+
}
43+
else {
44+
console.log("Configuration file exists. Will not overwrite.");
45+
}
46+
47+
}
48+
49+
/**
50+
* Get the config
51+
*/
52+
this.get = function() {
53+
return config.get("baker");
54+
};
55+
56+
};

package-lock.json

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "html-baker",
3+
"version": "0.0.1",
4+
"description": "Convert websites into flat-file websites",
5+
"main": "index.js",
6+
"bin": {
7+
"html-baker": "bin/htmlbaker.js"
8+
},
9+
"directories": {
10+
"lib": "lib"
11+
},
12+
"scripts": {
13+
"start": "node index.js"
14+
},
15+
"repository": {
16+
"type": "git",
17+
"url": "git+https://github.com/StatelessSoftware/html-baker.git"
18+
},
19+
"keywords": [],
20+
"author": "",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/StatelessSoftware/html-baker/issues"
24+
},
25+
"homepage": "https://github.com/StatelessSoftware/html-baker#readme",
26+
"dependencies": {
27+
"command-line-args": "^5.0.2",
28+
"config": "^1.30.0"
29+
}
30+
}

0 commit comments

Comments
 (0)