1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import type { NextApiRequest, NextApiResponse } from "next";
- import { build, EventBus } from "@luckysheet/cli";
- import type { IObj, IEventBus } from "@luckysheet/cli";
- import { Low, JSONFile } from "lowdb";
- type IData = {
- msg: string;
- };
- type IDBData = {
- buildObj: {
- [key: string]: string[];
- };
- };
- const adapter = new JSONFile<IDBData>("./db.json");
- const db = new Low(adapter);
- export default async function handler(
- req: NextApiRequest,
- res: NextApiResponse<IData>
- ) {
- res.writeHead(200, {
- "Content-Type": "text/event-stream;charset=utf-8",
- "Cache-Control": "no-cache,no-transform",
- Connection: "keep-alive",
- "Access-Control-Allow-Origin": "*",
- });
- // res.write("retry: 100000\n");
- const queryObject = req.query;
- console.log(queryObject);
- // Subscribe to event
- const eventBus: IEventBus = new EventBus();
- let id = 0;
- eventBus.subscribe("progress", (obj: IObj, num: number) => {
- res.write("event: progress\n");
- res.write("id: " + ++id + "\n");
- res.write("data: " + obj.msg + "-" + num + "\n\n");
- });
- await db.read();
- db.data = db.data || { buildObj: {} };
- const { buildObj } = db.data;
- const buildList = buildObj[queryObject.buildId as string];
- // Though in some cases TypeScript can infer them
- // const buildList = await db.get(queryObject.buildId);
- build(
- {
- include: buildList,
- },
- {
- eventBus,
- buildId: queryObject.buildId as string,
- }
- )
- .then((e: any) => {
- res.write("event: progress\n");
- res.write("id: " + ++id + "\n");
- res.write("data: " + "Success" + "-" + 1 + "\n\n");
- // res.write("event: message\n");
- // res.write("id: " + ++id + "\n");
- // res.write("data: Success\n\n");
- res.end();
- })
- .catch((e: any) => {
- res.write("event: progress\n");
- res.write("id: " + ++id + "\n");
- res.write("data: " + "Fail" + "-" + 0 + "\n\n");
- // res.write("event: message\n");
- // res.write("id: " + ++id + "\n");
- // res.write("data: Fail\n\n");
- res.end();
- });
- }
|