-
Notifications
You must be signed in to change notification settings - Fork 57
/
deploy.js
110 lines (102 loc) · 3.78 KB
/
deploy.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
const fs = require('fs');
const path = require('path');
function wrapReact(source) {
const relativeSource = source.replaceAll(
/"\.\/([^"]+)"/g,
`"{{ 'demo/$1' | relative_url }}"`);
return "{% if page.is_react %}\n" + relativeSource + "\n{% endif %}\n";
}
function copyIndex(indexSrcPath, destFolderPath) {
let indexSource = fs.readFileSync(indexSrcPath, 'utf8');
let includesPath = path.join(destFolderPath, '../_includes');
let match = indexSource.match(/<script defer="defer".*" rel="stylesheet">/);
let modulesSource = `
{% if page.modules %}
<script>
window.liveCodingExtraModules = "{{ page.modules }}";
</script>
{% endif %}
`;
let filesSource = `
{% if page.files %}
<script>
window.liveCodingExtraFiles = "{{ page.files }}";
</script>
{% endif %}
`;
let rawSource = match[0];
let destFilePath = path.join(includesPath, 'head-scripts.html');
fs.writeFileSync(
destFilePath,
wrapReact(modulesSource + filesSource + rawSource));
match = indexSource.match(/<div id="root"><\/div>(.*)<\/body>/ms);
destFilePath = path.join(includesPath, 'footer-scripts.html');
fs.writeFileSync(destFilePath, wrapReact(match[1]));
}
function copyPyodide(srcPath, destPath) {
const srcFiles = [
'cycler-0.11.0-py3-none-any.whl',
'distutils-1.0.0.zip',
'fonttools-4.38.0-py3-none-any.whl',
'kiwisolver-1.4.4-cp310-cp310-emscripten_3_1_27_wasm32.whl',
'matplotlib-3.5.2-cp310-cp310-emscripten_3_1_27_wasm32.whl',
'matplotlib_pyodide-0.1.1-py3-none-any.whl',
'numpy-1.23.5-cp310-cp310-emscripten_3_1_27_wasm32.whl',
'packaging-21.3-py3-none-any.whl',
'PIL-9.1.1-cp310-cp310-emscripten_3_1_27_wasm32.whl',
'pyodide.js',
'pyodide.js.map',
'pyodide.asm.wasm',
'pyodide.asm.js',
'pyodide.asm.data',
'pyodide_py.tar',
'pyparsing-3.0.9-py3-none-any.whl',
'python_dateutil-2.8.2-py2.py3-none-any.whl',
'pytz-2022.7-py2.py3-none-any.whl',
'package.json',
'repodata.json',
'six-1.16.0-py2.py3-none-any.whl',
'space_tracer-4.10.0-py3-none-any.whl'];
fs.mkdirSync(destPath);
for (const fileName of srcFiles) {
const fileSrcPath = path.join(srcPath, fileName),
fileDestPath = path.join(destPath, fileName);
fs.copyFileSync(fileSrcPath, fileDestPath);
}
}
function main() {
const dest = path.resolve('../docs/demo'),
src = path.resolve('build'),
pyodideDest = path.resolve('../docs/demo/pyodide'),
pyodideSrc = path.resolve('../../pyodide/dist'),
pyodideExists = fs.existsSync(pyodideSrc),
d = fs.opendirSync(dest);
let entry;
while ((entry = d.readSync()) !== null) {
let destFilePath = path.join(dest, entry.name);
if (entry.name === 'pyodide' && ! pyodideExists) {
// No new copy to replace it, so don't delete it.
} else if (entry.isDirectory()) {
if (entry.name === 'static' || entry.name === 'pyodide') {
fs.rmSync(destFilePath, {recursive: true});
}
}
}
const entries = fs.readdirSync(src);
entries.sort(); // Fails if we hit index.html before _includes.
for (entry of entries) {
let srcFilePath = path.join(src, entry),
destFilePath = path.join(dest, entry);
if (entry === 'favicon.ico') {
// Skip it.
} else if (entry !== 'index.html') {
fs.renameSync(srcFilePath, destFilePath);
} else {
copyIndex(srcFilePath, dest);
}
}
if (pyodideExists) {
copyPyodide(pyodideSrc, pyodideDest);
}
}
main();