import path from 'path';
import fsp from 'fs/promises';
import { task } from './lib/trace-runner';
import { listDir } from './lib/list-dir';
import type { TreeType, TreeTypeArray } from './lib/list-dir';
const rootPath = path.resolve(import.meta.dir, '../');
const publicPath = path.resolve(import.meta.dir, '../public');
const folderAndFilesToBeDeployed = [
'Mock',
'List',
'Clash',
'Modules',
'Script',
'LICENSE'
];
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 tree = await listDir(publicPath);
const html = generateHtml(tree);
return Bun.write(path.join(publicPath, 'index.html'), html);
});
if (import.meta.main) {
buildPublic();
}
const priorityOrder: Record<'default' | string & {}, number> = {
domainset: 1,
non_ip: 2,
ip: 3,
List: 10,
Surge: 11,
Clash: 12,
Modules: 13,
Script: 14,
Mock: 15,
Assets: 16,
LICENSE: 20,
default: Number.MAX_VALUE
};
const prioritySorter = (a: TreeType, b: TreeType) => (priorityOrder[a.name] || priorityOrder.default) - (priorityOrder[b.name] || priorityOrder.default) || +(a.name > b.name) || -(a.name < b.name);
function generateHtml(tree: TreeTypeArray) {
let html = `
Surge Ruleset Server | Sukka (@SukkaW)
`;
html += `
Sukka Ruleset Server
Made by Sukka | Source @ GitHub | Licensed under AGPL-3.0
Last Build: 2023-12-03T16:54:15.820Z
`;
html += '';
const walk = (tree: TreeTypeArray) => {
tree.sort(prioritySorter);
for (let i = 0, len = tree.length; i < len; i++) {
const entry = tree[i];
if (entry.type === 'directory') {
html += `${entry.name}`;
html += '';
walk(entry.children);
html += ' ';
} else if (/* entry.type === 'file' && */ entry.name !== 'index.html') {
html += ` ${entry.name} `;
}
}
};
walk(tree);
html += ' ';
html += `
`;
return html;
}