Skip to content

Commit 36dd026

Browse files
committed
Packaging
1 parent 76d6671 commit 36dd026

File tree

6 files changed

+216
-2
lines changed

6 files changed

+216
-2
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
build/embedded.ts
2+
dist/
13
public/js/*

build/cli.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/// <reference lib="deno.ns" />
2+
3+
import { join, dirname } from "https://deno.land/std/path/mod.ts";
4+
import { ensureDir } from "https://deno.land/std/fs/mod.ts";
5+
import { files } from "./embedded.ts";
6+
7+
// Get the directory of the current script
8+
const __dirname = new URL(".", import.meta.url).pathname;
9+
10+
async function setupTempDir(): Promise<string> {
11+
const tempDir = join(Deno.makeTempDirSync(), "k8s-dashboard");
12+
await ensureDir(tempDir);
13+
14+
// Write embedded files to temp directory
15+
for (const [relativePath, content] of Object.entries(files)) {
16+
const filePath = join(tempDir, relativePath);
17+
await ensureDir(dirname(filePath));
18+
const binaryContent = Uint8Array.from(atob(content as string), c => c.charCodeAt(0));
19+
await Deno.writeFile(filePath, binaryContent);
20+
}
21+
22+
return tempDir;
23+
}
24+
25+
async function startKubectlProxy(wwwDir: string) {
26+
const process = new Deno.Command("kubectl", {
27+
args: [
28+
"proxy",
29+
"--www=" + wwwDir,
30+
"--www-prefix=/",
31+
"--api-prefix=/k8s"
32+
],
33+
stdout: "inherit",
34+
stderr: "inherit"
35+
});
36+
37+
const child = process.spawn();
38+
39+
// Handle process termination
40+
Deno.addSignalListener("SIGINT", () => {
41+
console.log("\nShutting down...");
42+
child.kill("SIGTERM");
43+
Deno.exit(0);
44+
});
45+
46+
// Wait for the process to complete
47+
await child.status;
48+
}
49+
50+
async function main() {
51+
try {
52+
console.log("Setting up temporary directory...");
53+
const tempDir = await setupTempDir();
54+
55+
console.log("Starting kubectl proxy...");
56+
console.log(`Static files served from: ${tempDir}`);
57+
await startKubectlProxy(tempDir);
58+
} catch (error: unknown) {
59+
console.error("Error:", error instanceof Error ? error.message : String(error));
60+
Deno.exit(1);
61+
}
62+
}
63+
64+
// Run the application
65+
if (import.meta.main) {
66+
main();
67+
}

deno.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"tasks": {
3-
"build": "deno run --watch=src/ --watch-exclude=public --no-clear-screen --allow-read --allow-write --allow-run --allow-net --allow-env bundle.ts"
3+
"build": "deno run --watch=src/ --watch-exclude=public --no-clear-screen --allow-read --allow-write --allow-run --allow-net --allow-env bundle.ts",
4+
"embed": "deno run --allow-read --allow-write embed.ts",
5+
"package": "deno compile --allow-read --allow-write --allow-run --allow-net --output ./dist/k8s-dashboard ./build/cli.ts"
46
},
57
"imports": {
68
"@std/assert": "jsr:@std/assert@1",

deno.lock

+101
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

embed.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// <reference lib="deno.ns" />
2+
3+
import { join, dirname } from "https://deno.land/std/path/mod.ts";
4+
import { ensureDir } from "https://deno.land/std/fs/mod.ts";
5+
6+
const __dirname = new URL(".", import.meta.url).pathname;
7+
8+
// Create a temp build directory
9+
const buildDir = join(__dirname, "build");
10+
await ensureDir(buildDir);
11+
12+
// Create a new file that embeds our public directory as a base64 string
13+
const publicDir = join(__dirname, "public");
14+
const files: Record<string, string> = {};
15+
16+
async function embedFiles(dir: string, basePath: string = "") {
17+
for await (const entry of Deno.readDir(dir)) {
18+
const fullPath = join(dir, entry.name);
19+
const relativePath = join(basePath, entry.name);
20+
21+
if (entry.isFile) {
22+
console.log("Embedding " + relativePath);
23+
const content = await Deno.readFile(fullPath);
24+
const base64 = btoa(String.fromCharCode(...content));
25+
files[relativePath] = base64;
26+
} else if (entry.isDirectory) {
27+
await embedFiles(fullPath, relativePath);
28+
}
29+
}
30+
}
31+
32+
// Recursively read all files in the public directory
33+
await embedFiles(publicDir);
34+
35+
// Create the embedded.ts file
36+
const embeddedContent = `
37+
// This file is auto-generated. Do not edit.
38+
export const files = ${JSON.stringify(files, null, 2)};
39+
`;
40+
41+
await Deno.writeTextFile(join(buildDir, "embedded.ts"), embeddedContent);
42+
43+
console.log("Embedding completed!");

public/styles/.gitignore

-1
This file was deleted.

0 commit comments

Comments
 (0)