build-public.ts 3.8 KB

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