build-public.ts 4.2 KB

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