build-public.ts 4.3 KB

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