forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-directory.js
More file actions
103 lines (100 loc) · 3.39 KB
/
example-directory.js
File metadata and controls
103 lines (100 loc) · 3.39 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
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
const fs = require('fs');
let categoriesList = [];
let categoriesString = '';
let categoriesCounter = 0;
let examplesCounter = 0;
fs.readdir(`${__dirname}/src/examples/`, function (err, categories) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
categoriesCounter = categories.length;
categories.forEach(function (category) {
var dir = `dist/${category}`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
fs.readdir(`${__dirname}/src/examples/${category}`, (err, examples) => {
examples = examples.map((example) => example.replace('.tsx', ''));
categoriesList.push({
name: category,
examples
});
if (err) {
return console.log('Unable to scan directory: ' + err);
}
categoriesString += `<h2>${category}</h2>`;
examplesCounter += examples.length;
examples.forEach((example) => {
categoriesString += `<li><a href='/#/DIRECTORY_TYPE/${category}/${example}'>${example}</a></li>`;
examplesCounter--;
if (examplesCounter === 0) {
categoriesCounter--;
if (categoriesCounter === 0) {
createDirectory('iframe');
createDirectory('debug');
createCategoriesListFile();
}
}
const content = `
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="0; url='/#/${category}/${example}'" />
<meta name="twitter:card" content="photo" />
<meta name="twitter:site" content="@playcanvas" />
<meta name="twitter:title" content="${example.split('-').join(' ')}" />
<meta name="twitter:description" content="A PlayCanvas engine example" />
<meta name="twitter:image" content="https://playcanvas.github.io/thumbnails/${category}_${example}.png" />
<meta name="twitter:url" content="https://playcanvas.github.io/${category}/${example}" />
</head>
<body>
<p>Please follow <a href="/#/${category}/${example}">this link</a>.</p>
</body>
</html>
`;
var dir = `dist/${category}/${example}`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
fs.writeFile(`dist/${category}/${example}/index.html`, content, (err) => {
if (err) {
console.error(err);
return null;
}
});
});
});
});
});
function createDirectory(path) {
const directoryHtml = (path) => `
<!DOCTYPE html>
<html>
<head>
</head>
<body>
${categoriesString.split('DIRECTORY_TYPE').join(path)}
</body>
</html>
`;
var dir = `dist/${path}-directory/`;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
fs.writeFile(`dist/${path}-directory/index.html`, directoryHtml(path), (err) => {
if (err) {
console.error(err);
return null;
}
});
}
function createCategoriesListFile() {
const text = `/* eslint-disable no-unused-vars */
var categories = ${JSON.stringify(categoriesList)};`;
fs.writeFile(`dist/examples.js`, text, (err) => {
if (err) {
console.error(err);
return null;
}
});
}