build-public.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import path from 'path';
  2. import { task } from './trace';
  3. import { treeDir } from './lib/tree-dir';
  4. import type { TreeType, TreeTypeArray } from './lib/tree-dir';
  5. import { fdir as Fdir } from 'fdir';
  6. import { sort } from './lib/timsort';
  7. import Trie from 'mnemonist/trie';
  8. const rootPath = path.resolve(import.meta.dir, '../');
  9. const publicPath = path.resolve(import.meta.dir, '../public');
  10. const folderAndFilesToBeDeployed = [
  11. `Mock${path.sep}`,
  12. `List${path.sep}`,
  13. `Clash${path.sep}`,
  14. `Modules${path.sep}`,
  15. `Script${path.sep}`,
  16. `Internal${path.sep}`,
  17. 'LICENSE'
  18. ];
  19. export const buildPublic = task(import.meta.main, import.meta.path)(async (span) => {
  20. await span
  21. .traceChild('copy public files')
  22. .traceAsyncFn(async () => {
  23. const trie = Trie.from(await new Fdir()
  24. .withRelativePaths()
  25. .exclude((dirName) => {
  26. return dirName === 'node_modules' || dirName === 'Build' || dirName === 'public' || dirName[0] === '.';
  27. })
  28. .crawl(rootPath)
  29. .withPromise());
  30. const filesToBeCopied = folderAndFilesToBeDeployed.flatMap(folderOrFile => trie.find(folderOrFile));
  31. return Promise.all(filesToBeCopied.map(file => {
  32. const src = path.resolve(rootPath, file);
  33. const dest = path.resolve(publicPath, file);
  34. return Bun.write(dest, Bun.file(src));
  35. }));
  36. });
  37. const html = await span
  38. .traceChild('generate index.html')
  39. .traceAsyncFn(() => treeDir(publicPath).then(generateHtml));
  40. return Bun.write(path.join(publicPath, 'index.html'), html);
  41. });
  42. const priorityOrder: Record<'default' | string & {}, number> = {
  43. domainset: 1,
  44. non_ip: 2,
  45. ip: 3,
  46. List: 10,
  47. Surge: 11,
  48. Clash: 12,
  49. Modules: 13,
  50. Script: 14,
  51. Mock: 15,
  52. Assets: 16,
  53. Internal: 17,
  54. LICENSE: 20,
  55. default: Number.MAX_VALUE
  56. };
  57. const prioritySorter = (a: TreeType, b: TreeType) => {
  58. return ((priorityOrder[a.name] || priorityOrder.default) - (priorityOrder[b.name] || priorityOrder.default)) || a.name.localeCompare(b.name);
  59. };
  60. const walk = (tree: TreeTypeArray) => {
  61. let result = '';
  62. sort(tree, prioritySorter);
  63. for (let i = 0, len = tree.length; i < len; i++) {
  64. const entry = tree[i];
  65. if (entry.type === 'directory') {
  66. result += `<li class="folder">${entry.name}`;
  67. result += '<ul>';
  68. result += walk(entry.children);
  69. result += '</ul>';
  70. } else if (/* entry.type === 'file' && */ entry.name !== 'index.html') {
  71. result += `<li><a class="file directory-list-file" href="${entry.path}">${entry.name}</a></li>`;
  72. }
  73. }
  74. return result;
  75. };
  76. function generateHtml(tree: TreeTypeArray) {
  77. let html = `<!DOCTYPE html>
  78. <html lang="en">
  79. <head>
  80. <meta charset="utf-8">
  81. <title>Surge Ruleset Server | Sukka (@SukkaW)</title>
  82. <meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
  83. <link href="https://cdn.skk.moe/favicon.ico" rel="icon" type="image/ico">
  84. <link href="https://cdn.skk.moe/favicon/apple-touch-icon.png" rel="apple-touch-icon" sizes="180x180">
  85. <link href="https://cdn.skk.moe/favicon/android-chrome-192x192.png" rel="icon" type="image/png" sizes="192x192">
  86. <link href="https://cdn.skk.moe/favicon/favicon-32x32.png" rel="icon" type="image/png" sizes="32x32">
  87. <link href="https://cdn.skk.moe/favicon/favicon-16x16.png" rel="icon" type="image/png" sizes="16x16">
  88. <meta name="description" content="Sukka 自用的 Surge / Clash Premium 规则组">
  89. <link rel="stylesheet" href="https://cdn.skk.moe/ruleset/css/21d8777a.css" />
  90. <meta property="og:title" content="Surge Ruleset | Sukka (@SukkaW)">
  91. <meta property="og:type" content="Website">
  92. <meta property="og:url" content="https://ruleset.skk.moe/">
  93. <meta property="og:image" content="https://cdn.skk.moe/favicon/android-chrome-192x192.png">
  94. <meta property="og:description" content="Sukka 自用的 Surge / Clash Premium 规则组">
  95. <meta name="twitter:card" content="summary">
  96. <link rel="canonical" href="https://ruleset.skk.moe/">
  97. </head>`;
  98. html += `<body>
  99. <main class="container">
  100. <h1>Sukka Ruleset Server</h1>
  101. <p>
  102. 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>
  103. </p>
  104. <p>Last Build: ${new Date().toISOString()}</p>
  105. <br>`;
  106. html += '<ul class="directory-list">';
  107. html += walk(tree);
  108. html += '</ul>';
  109. html += `</main>
  110. </body>
  111. </html>`;
  112. return html;
  113. }