-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
54 lines (44 loc) · 1.69 KB
/
index.ts
File metadata and controls
54 lines (44 loc) · 1.69 KB
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
import * as allSamples from "./data/code.examples.json";
import * as settings from "./data/settings.json";
import * as pug from "pug";
import * as beauty from "js-beautify";
import * as fs from "fs-extra";
import * as www from "serve-static";
import * as http from "http";
import * as done from "finalhandler";
const outputDir = "./output";
(async () => {
// Clear / Create output folder
await fs.ensureDir(outputDir);
await fs.remove(outputDir);
await fs.ensureDir(outputDir);
// Copy standard CSS/images
await fs.copy("assets", `${outputDir}/assets`);
console.log("Copied assets folder");
// Process each sample and save it to the output folder
await Promise.all(allSamples.map( (sampleGroup) => {
Promise.all(sampleGroup.samples.map(async (sample) => {
console.log(`Processing ${sample.title}`);
const compilation = pug.compileFile(sample.template);
const html = beauty.html(compilation({...sample, settings}));
try {
let result = await fs.outputFile(`${outputDir}/${sample.output}`,html);
console.log(`File ${sample.output} created`);
return result;
} catch( err ) {
console.error(err);
throw err;
}
}))
}));
const indexFile = pug.compileFile('layouts/index-page.pug');
try {
await fs.outputFile(`${outputDir}/index.html`, beauty.html(indexFile({groups:allSamples})));
console.log(`Index file created`);
} catch ( err ) {
console.error(err);
}
http
.createServer((req: any,res: any)=>www(outputDir)(req,res,done(req,res)))
.listen(3456)
})();