build-public.ts 4.8 KB

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