build-public.ts 5.1 KB

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