-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-wiki.js
57 lines (53 loc) · 2.83 KB
/
build-wiki.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
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const repoFolder = path.join(path.dirname(__filename), '..');
const folderToServe = path.join(repoFolder, 'public-dist');
// cross-env TIDDLYWIKI_PLUGIN_PATH='node_modules/tiddlywiki/plugins/published' TIDDLYWIKI_THEME_PATH='${wikiFolderName}/themes'
process.env.TIDDLYWIKI_PLUGIN_PATH = `${repoFolder}/plugins`;
process.env.TIDDLYWIKI_THEME_PATH = `${repoFolder}/themes`;
const execAndLog = (command, options) => console.log(String(execSync(command, options)));
module.exports = function build() {
// npm run build:prepare
execAndLog(`rm -rf ${folderToServe}`);
// npm run build:public
execAndLog(`cp -r ${repoFolder}/public/ ${folderToServe}`, { cwd: repoFolder });
try {
execAndLog(`cp ${repoFolder}/vercel.json ${folderToServe}/vercel.json`, { cwd: repoFolder });
} catch (error) {
console.log(error);
}
// try copy some static assets, don't cause error if some of them been removed by the user
try {
// npm run build:public
execAndLog(`cp ${repoFolder}/tiddlers/favicon.ico ${folderToServe}/favicon.ico`, { cwd: repoFolder });
execAndLog(`cp ${repoFolder}/tiddlers/TiddlyWikiIconWhite.png ${folderToServe}/TiddlyWikiIconWhite.png`, {
cwd: repoFolder,
});
execAndLog(`cp ${repoFolder}/tiddlers/TiddlyWikiIconBlack.png ${folderToServe}/TiddlyWikiIconBlack.png`, {
cwd: repoFolder,
});
} catch (error) {
console.log(error);
}
// npm run build:nodejs2html
// exclude edit related plugins, make it readonly, and reduce size
execAndLog(`tiddlywiki ${repoFolder} --build readonlyexternalimages`, { cwd: repoFolder });
execAndLog(`tiddlywiki ${repoFolder} --build externaljs`, { cwd: repoFolder });
// npm run build:sitemap
execAndLog(`tiddlywiki . --rendertiddler sitemap sitemap.xml text/plain && mv ${repoFolder}/output/sitemap.xml ${folderToServe}/sitemap.xml`, {
cwd: repoFolder,
});
// npm run build:minifyHTML
const htmlMinifyPath = `${repoFolder}/output/index-minify.html`;
const htmlOutputPath = `${folderToServe}/index.html`;
execAndLog(`html-minifier-terser -c ./html-minifier-terser.config.json -o ${htmlMinifyPath} ${repoFolder}/output/index.html`, { cwd: repoFolder });
// build dll.js and config tw to load it
// original filename contains invalid char, will cause static server unable to load it
const htmlContent = fs.readFileSync(htmlMinifyPath, 'utf-8');
const htmlContentWithCorrectJsPath = htmlContent.replaceAll('%24%3A%2Fcore%2Ftemplates%2Ftiddlywiki5.js', 'tiddlywiki5.js');
fs.writeFileSync(htmlOutputPath, htmlContentWithCorrectJsPath);
execAndLog(`mv ${repoFolder}/output/tiddlywiki5.js ${folderToServe}/tiddlywiki5.js`, { cwd: repoFolder });
// npm run build:precache
execAndLog(`workbox injectManifest workbox-config.js`, { cwd: repoFolder });
};