build-public.ts 4.3 KB

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