-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
executable file
·168 lines (146 loc) · 4.16 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
164
165
166
167
168
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const URL = require("url").URL;
const { log, name, spawn, onCompile, help } = require("./utils");
let args = process.argv.slice(2);
log(`\n <h> ₪ ${name} </h> extensible static HTML\n`, { prefix: null });
const cmds = ["dev", "build", "help"];
const cmd = args.shift();
if (!cmds.includes(cmd)) {
if (cmd) {
log(
`Invalid command <u>${cmd || ""}</u>. Valid commands are ${cmds
.map((c) => `<u>${c}</u>`)
.join(" ")}.`
);
process.exit(1);
} else {
help();
process.exit(0);
}
}
if (cmd === "help" || args.some((a) => a === "-h" || a === "--help")) {
help();
process.exit(0);
}
const isDev = cmd === "dev";
const portOptionIndex = args.findIndex((a) => a === "-p" || a === "--port");
let port = 5000;
if (~portOptionIndex && args[portOptionIndex + 1]) {
port = args[portOptionIndex + 1];
args.splice(portOptionIndex, 2);
}
const rootDir = args.reduce((rootDir, option, index) => {
if (["-r", "--root"].includes(option) && args[index + 1]) {
rootDir = args[index + 1];
}
return rootDir;
}, ".");
const outputOptionIndex = args.findIndex((a) => a === "-o" || a === "--output");
let devDir = `.${name}`;
let outDir = isDev
? path.join(rootDir, devDir)
: (~outputOptionIndex && args[outputOptionIndex + 1]) || "out";
if (~outputOptionIndex) {
args[outputOptionIndex + 1] = outDir;
} else {
args.push("-o");
args.push(outDir);
}
// Mirror the input folder tree when outputting.
args.push("-a");
const hasPattern = args.findIndex((a) => /^[-!]/.test(a));
if (hasPattern === 0) {
args.unshift("**/*.html");
}
args.splice(1, 0, `!**/${isDev ? devDir : outDir}/**`);
args.push("-u");
args.push(`posthtml-${name}-import`);
args.push(`--posthtml-${name}-import.root`);
args.push(rootDir);
args.push(`--posthtml-${name}-import.strict`);
args.push("");
args.push("-u");
args.push("posthtml-md2html");
args.push("--posthtml-md2html.gfm");
args.push("true");
if (fs.existsSync(outDir)) {
fs.rmdirSync(outDir, { recursive: true });
}
const htmlOnlyIndex = args.findIndex((a) => a === "-x" || a === "--htmlOnly");
const run =
~htmlOnlyIndex || isDev
? (fn) => fn()
: (fn) => {
require("ncp").ncp(
rootDir,
outDir,
{
filter: (filePath) =>
!filePath.includes(outDir) && !filePath.includes(devDir),
},
(err) => {
if (err) {
throw err;
}
fn();
}
);
};
if (~htmlOnlyIndex) {
args.splice(htmlOnlyIndex, 1);
}
run(() => {
if (isDev) {
const http = require("http");
const sirvHandler = require("sirv")(rootDir, { dev: true });
const server = new http.createServer((req, res, next) => {
const pathname = new URL(req.url, "https://localhost/").pathname || "/";
let filename = path.join(rootDir, pathname);
const extension = path.extname(filename).trim();
const shouldRebuildRoute =
(!extension || extension === ".html") &&
["index.html", ".html", ""].some((guess) => {
let guessFilename =
guess == "index.html"
? path.join(filename, guess)
: filename + guess;
if (fs.existsSync(path.resolve(guessFilename))) {
filename =
guess == "index.html"
? path.join(pathname, guess)
: pathname + guess;
return true;
}
return false;
});
if (shouldRebuildRoute) {
// html files will be built and served from the devDir
log(`Serving ${req.url} ...`);
req.url = path.join(devDir, req.url);
const [_, ...newArgs] = args;
newArgs.unshift(filename);
spawn("posthtml", {
args: newArgs,
onMsg: () => {},
});
}
return sirvHandler(req, res, next);
});
server.listen(port, (err) => {
if (err) {
log(`<u>${err}</u>`);
process.exit(1);
}
log(`Listening on <u>https://localhost:${port}</u>\n`);
});
} else {
spawn("posthtml", {
args,
onMsg: () => {
log("Done.");
},
});
}
});