215 lines
5.8 KiB
TypeScript
215 lines
5.8 KiB
TypeScript
import {
|
|
dirAsync,
|
|
exists,
|
|
existsAsync,
|
|
read,
|
|
removeAsync,
|
|
writeAsync,
|
|
} from "fs-jetpack";
|
|
import { decode } from "msgpackr";
|
|
import { createRouter } from "radix3";
|
|
import { startBrCompress } from "./br-load";
|
|
import { dir } from "./dir";
|
|
import { g } from "./global";
|
|
import { gunzipAsync } from "./gzip";
|
|
|
|
const decoder = new TextDecoder();
|
|
export const deploy = {
|
|
async init(load_from?: string) {
|
|
await dirAsync(dir(`app/web/deploy`));
|
|
|
|
if (!(await this.has_gz())) {
|
|
await this.run();
|
|
}
|
|
|
|
await this.load(this.config.deploy.ts);
|
|
},
|
|
async load(ts: string) {
|
|
console.log(`Loading site: ${this.config.site_id} ${ts}`);
|
|
|
|
try {
|
|
if (await Bun.file(`app/web/deploy/${ts}.mpack`).exists()) {
|
|
g.deploy.content = decode(
|
|
await gunzipAsync(
|
|
new Uint8Array(
|
|
await Bun.file(dir(`app/web/deploy/${ts}.gz`)).arrayBuffer()
|
|
)
|
|
)
|
|
);
|
|
} else {
|
|
g.deploy.content = JSON.parse(
|
|
decoder.decode(
|
|
new Uint8Array(
|
|
await gunzipAsync(
|
|
new Uint8Array(
|
|
await Bun.file(dir(`app/web/deploy/${ts}.gz`)).arrayBuffer()
|
|
)
|
|
)
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
if (g.deploy.content) {
|
|
g.cache = {
|
|
br: {},
|
|
gz: {},
|
|
br_progress: {
|
|
pending: {},
|
|
running: false,
|
|
timeout: null,
|
|
},
|
|
};
|
|
startBrCompress();
|
|
|
|
if (exists(dir("public"))) {
|
|
await removeAsync(dir("public"));
|
|
if (g.deploy.content.public) {
|
|
await dirAsync(dir("public"));
|
|
for (const [k, v] of Object.entries(g.deploy.content.public)) {
|
|
await writeAsync(dir(`public/${k}`), v);
|
|
}
|
|
}
|
|
}
|
|
for (const page of g.deploy.content.layouts) {
|
|
if (page.is_default_layout) {
|
|
g.deploy.layout = page.content_tree;
|
|
break;
|
|
}
|
|
}
|
|
if (!g.deploy.layout && g.deploy.content.layouts.length > 0) {
|
|
g.deploy.layout = g.deploy.content.layouts[0].content_tree;
|
|
}
|
|
|
|
g.deploy.router = createRouter();
|
|
g.deploy.pages = {};
|
|
for (const page of g.deploy.content.pages) {
|
|
g.deploy.pages[page.id] = page;
|
|
g.deploy.router.insert(page.url, page);
|
|
}
|
|
|
|
g.deploy.comps = {};
|
|
for (const comp of g.deploy.content.comps) {
|
|
g.deploy.comps[comp.id] = comp.content_tree;
|
|
}
|
|
|
|
if (g.deploy.content.code.server) {
|
|
setTimeout(async () => {
|
|
if (g.deploy.content) {
|
|
delete require.cache[dir(`app/web/server/index.js`)];
|
|
await removeAsync(dir(`app/web/server`));
|
|
await dirAsync(dir(`app/web/server`));
|
|
for (const [k, v] of Object.entries(
|
|
g.deploy.content.code.server
|
|
)) {
|
|
await writeAsync(dir(`app/web/server/${k}`), v);
|
|
}
|
|
|
|
if (await existsAsync(dir(`app/web/server/index.js`))) {
|
|
const res = require(dir(`app/web/server/index.js`));
|
|
if (res && typeof res.server === "object") {
|
|
g.deploy.server = res.server;
|
|
}
|
|
}
|
|
|
|
if (g.server) {
|
|
await g.deploy.server?.init?.({ port: g.server.port });
|
|
} else {
|
|
const inv = setInterval(async () => {
|
|
if (g.server) {
|
|
clearInterval(inv);
|
|
await g.deploy.server?.init?.({ port: g.server.port });
|
|
}
|
|
}, 1000);
|
|
}
|
|
}
|
|
}, 300);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
console.log("Failed to load site", this.config.site_id);
|
|
if (e instanceof Error)
|
|
console.error(e.message, `[app/web/deploy/${ts}.gz]`);
|
|
}
|
|
},
|
|
async run(load_from?: string) {
|
|
if (!this.config.site_id) {
|
|
console.log("site_id is not found on app/web/config.json");
|
|
return;
|
|
}
|
|
let buf: ArrayBuffer | null = null;
|
|
if (!load_from) {
|
|
let base_url = "https://prasi.avolut.com";
|
|
if (g.mode === "dev") {
|
|
base_url = "http://localhost:4550";
|
|
}
|
|
|
|
console.log(
|
|
`Downloading site deploy: ${this.config.site_id} [ts: ${this.config.deploy.ts}] ${base_url}`
|
|
);
|
|
const res = await fetch(
|
|
`${base_url}/prod-zip/${this.config.site_id}?ts=${Date.now()}&msgpack=1`
|
|
);
|
|
buf = await res.arrayBuffer();
|
|
} else {
|
|
const res = await fetch(load_from);
|
|
buf = await res.arrayBuffer();
|
|
}
|
|
|
|
if (!buf) {
|
|
console.log("Failed to download site deploy");
|
|
return;
|
|
}
|
|
const ts = Date.now();
|
|
const file = Bun.file(dir(`app/web/deploy/${ts}.gz`));
|
|
await Bun.write(file, buf);
|
|
await Bun.write(dir(`app/web/deploy/${ts}.mpack`), "ok");
|
|
this.config.deploy.ts = ts + "";
|
|
|
|
await this.saveConfig();
|
|
},
|
|
get config() {
|
|
if (!g.deploy) {
|
|
g.deploy = {
|
|
comps: {},
|
|
layout: null,
|
|
pages: {},
|
|
router: createRouter(),
|
|
config: { deploy: { ts: "" }, site_id: "" },
|
|
init: false,
|
|
raw: null,
|
|
content: null,
|
|
server: null,
|
|
};
|
|
}
|
|
|
|
if (!g.deploy.init) {
|
|
g.deploy.init = true;
|
|
g.deploy.raw = read(dir(`app/web/config.json`), "json");
|
|
|
|
if (g.deploy.raw) {
|
|
for (const [k, v] of Object.entries(g.deploy.raw)) {
|
|
(g.deploy.config as any)[k] = v;
|
|
}
|
|
}
|
|
}
|
|
|
|
return g.deploy.config;
|
|
},
|
|
saveConfig() {
|
|
return Bun.write(
|
|
Bun.file(dir(`app/web/config.json`)),
|
|
JSON.stringify(this.config, null, 2)
|
|
);
|
|
},
|
|
has_gz() {
|
|
if (this.config.deploy.ts) {
|
|
return Bun.file(
|
|
dir(`app/web/deploy/${this.config.deploy.ts}.gz`)
|
|
).exists();
|
|
}
|
|
|
|
return false;
|
|
},
|
|
};
|