build-public.ts 4.8 KB

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