build-public.ts 3.8 KB

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