Skip to content

Commit

Permalink
Perf: speed up public folder setup
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Dec 19, 2023
1 parent 74293ac commit d1385b5
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions Build/build-public.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fsp from 'fs/promises';
import { task } from './lib/trace-runner';
import { treeDir } from './lib/tree-dir';
import type { TreeType, TreeTypeArray } from './lib/tree-dir';
import listDir from '@sukka/listdir';

const rootPath = path.resolve(import.meta.dir, '../');
const publicPath = path.resolve(import.meta.dir, '../public');
Expand All @@ -18,14 +19,22 @@ const folderAndFilesToBeDeployed = [

export const buildPublic = task(import.meta.path, async () => {
await fsp.mkdir(publicPath, { recursive: true });
await Promise.all(folderAndFilesToBeDeployed.map(dir => fsp.cp(
path.resolve(rootPath, dir),
path.resolve(publicPath, dir),
{ force: true, recursive: true }
)));

const html = generateHtml(await treeDir(publicPath));
const filesToBeCopied = (await listDir(
rootPath, {
ignoreHidden: true,
ignorePattern: /node_modules|Build|public/
}
)).filter(file => folderAndFilesToBeDeployed.some(folderOrFile => file.startsWith(folderOrFile + path.sep)));

await Promise.all(filesToBeCopied.map(file => {
const src = path.resolve(rootPath, file);
const dest = path.resolve(publicPath, file);

return Bun.write(dest, Bun.file(src));
}));

const html = generateHtml(await treeDir(publicPath));
return Bun.write(path.join(publicPath, 'index.html'), html);
});

Expand Down Expand Up @@ -99,7 +108,7 @@ function generateHtml(tree: TreeTypeArray) {
<p>
Made by <a href="https://skk.moe">Sukka</a> | <a href="https://github.com/SukkaW/Surge/">Source @ GitHub</a> | Licensed under <a href="/LICENSE" target="_blank">AGPL-3.0</a>
</p>
<p>Last Build: 2023-12-03T16:54:15.820Z</p>
<p>Last Build: ${new Date().toISOString()}</p>
<br>`;

html += '<ul class="directory-list">';
Expand Down

0 comments on commit d1385b5

Please sign in to comment.