-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildDocsExamples.js
90 lines (71 loc) · 2.45 KB
/
buildDocsExamples.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
const { execSync } = require("child_process");
const { existsSync, readdirSync, lstatSync, mkdirSync, writeFileSync } = require("fs");
const { resolve } = require("path");
console.log(`Building doc pages for examples...`);
const examplesDir = resolve(__dirname, "./examples");
if (!existsSync(examplesDir)) {
process.exit(1);
}
let examples = readdirSync(examplesDir);
// get built examples
examples = examples.filter((example) => {
const p = resolve(examplesDir, example);
if (
!existsSync(p) ||
!lstatSync(p).isDirectory() ||
!existsSync(resolve(examplesDir, example, "build")) ||
example.startsWith("_")
)
return false;
return true;
});
const buildFolders = examples.map((example) => {
return resolve(examplesDir, example, "build");
});
// copy builds to docs static pages
for (let i = 0; i < examples.length; i++) {
const example = examples[i];
const folder = buildFolders[i];
const destination = resolve(__dirname, "docs/static/examples", example);
mkdirSync(destination, { recursive: true });
execSync(`yarn shx cp -R ${folder}/* ${destination}`);
}
// build doc pages
const urls = examples.map((example) => {
return resolve("/examples", example);
});
const examplesPath = resolve(__dirname, "docs/docs/examples");
mkdirSync(examplesPath, { recursive: true });
writeFileSync(resolve(examplesPath, "_category_.yml"), `position: 0\nlabel: "Examples"`);
writeFileSync(
resolve(examplesPath, "index.md"),
`---
id: "index"
title: "Examples"
slug: "/examples/"
sidebar_label: "Readme"
sidebar_position: 0
---
In this section you will find a collection of small games and demos built with Blaze. Feel free to check them out and use any of their code in one of your own projects.
`,
);
for (let i = 0; i < examples.length; i++) {
const example = examples[i];
const url = urls[i];
const path = resolve(examplesPath, `${example}.md`);
const header = `---
id: "${example}"
title: "${example}"
slug: "${url}"
sidebar_label: "${example}"
sidebar_position: ${i + 1}
custom_edit_url: null
---`;
const content = `
<iframe tabindex="0" title="${example}" width="100%" height="800" src="${url}"></iframe>
<a href="pathname://${url}" style={{marginLeft: "1.5rem", float: "right"}}>View Fullscreen</a>
<a href="https://github.com/freddie-nelson/blaze-2d/tree/master/examples/${example}/src/main.ts" style={{float: "right"}}>View Code</a>
`;
writeFileSync(path, header + content);
}
console.log(`Finished building doc pages for examples.`);