build-public.ts 4.0 KB

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