build-public.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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) => (priorityOrder[a.name] || priorityOrder.default) - (priorityOrder[b.name] || priorityOrder.default) || +(a.name > b.name) || -(a.name < b.name);
  45. function generateHtml(tree: TreeTypeArray) {
  46. let html = `<!DOCTYPE html>
  47. <html lang="en">
  48. <head>
  49. <meta charset="utf-8">
  50. <title>Surge Ruleset Server | Sukka (@SukkaW)</title>
  51. <meta name="viewport" content="width=device-width,initial-scale=1,viewport-fit=cover">
  52. <link href="https://cdn.skk.moe/favicon.ico" rel="icon" type="image/ico">
  53. <link href="https://cdn.skk.moe/favicon/apple-touch-icon.png" rel="apple-touch-icon" sizes="180x180">
  54. <link href="https://cdn.skk.moe/favicon/android-chrome-192x192.png" rel="icon" type="image/png" sizes="192x192">
  55. <link href="https://cdn.skk.moe/favicon/favicon-32x32.png" rel="icon" type="image/png" sizes="32x32">
  56. <link href="https://cdn.skk.moe/favicon/favicon-16x16.png" rel="icon" type="image/png" sizes="16x16">
  57. <meta name="description" content="Sukka 自用的 Surge 规则组">
  58. <link rel="stylesheet" href="https://cdn.skk.moe/ruleset/css/21d8777a.css" />
  59. <meta property="og:title" content="Surge Ruleset | Sukka (@SukkaW)">
  60. <meta property="og:type" content="Website">
  61. <meta property="og:url" content="https://ruleset.skk.moe/">
  62. <meta property="og:image" content="https://cdn.skk.moe/favicon/android-chrome-192x192.png">
  63. <meta property="og:description" content="Sukka 自用的 Surge / Clash Premium 规则组">
  64. <meta name="twitter:card" content="summary">
  65. <link rel="canonical" href="https://ruleset.skk.moe/">
  66. </head>`;
  67. html += `<body>
  68. <main class="container">
  69. <h1>Sukka Ruleset Server</h1>
  70. <p>
  71. 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>
  72. </p>
  73. <p>Last Build: 2023-12-03T16:54:15.820Z</p>
  74. <br>`;
  75. html += '<ul class="directory-list">';
  76. const walk = (tree: TreeTypeArray) => {
  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. html += `<li class="folder">${entry.name}`;
  82. html += '<ul>';
  83. walk(entry.children);
  84. html += '</ul>';
  85. } else if (/* entry.type === 'file' && */ entry.name !== 'index.html') {
  86. html += `<li><a class="file directory-list-file" href="${entry.path}">${entry.name}</a></li>`;
  87. }
  88. }
  89. };
  90. walk(tree);
  91. html += '</ul>';
  92. html += `</main>
  93. </body>
  94. </html>`;
  95. return html;
  96. }