build-public.ts 5.0 KB

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