build-public.ts 4.7 KB

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