|
| 1 | +import { Context } from "../context.ts"; |
| 2 | +import { ApiContext } from "../api.ts"; |
| 3 | +import * as p from 'npm:@clack/prompts'; |
| 4 | +import color from 'npm:picocolors'; |
| 5 | +import { apiConfig } from "../si_client.ts"; |
| 6 | +// import { ChangeSetsApi, ChangeSetViewV1, MvApi } from "https://jsr.io/@systeminit/api-client/1.9.0/api.ts"; |
| 7 | +import { ChangeSetsApi, ChangeSetViewV1, MvApi } from "../../../../generated-sdks/typescript/api.ts"; |
| 8 | + |
| 9 | +const EXIT = "-1"; |
| 10 | + |
| 11 | +export async function callTui(ctx: Context, _apiCtx: ApiContext) { |
| 12 | + const changeSetsApi = new ChangeSetsApi(apiConfig); |
| 13 | + const mvSetsApi = new MvApi(apiConfig); |
| 14 | + const workspaceId = ctx.workspaceId; |
| 15 | + if (!workspaceId) throw new Error("No Workspace"); |
| 16 | + |
| 17 | + p.updateSettings({ |
| 18 | + aliases: { |
| 19 | + w: 'up', |
| 20 | + s: 'down', |
| 21 | + a: 'left', |
| 22 | + d: 'right', |
| 23 | + }, |
| 24 | + }); |
| 25 | + |
| 26 | + p.intro("Welcome! Let's review propsed changes in your workspace") |
| 27 | + |
| 28 | + const cs = p.spinner({ |
| 29 | + onCancel: () => { |
| 30 | + process.exit(0); |
| 31 | + } |
| 32 | + }); |
| 33 | + cs.start(`${color.bgBlack(color.greenBright("Retrieving change sets"))}`); |
| 34 | + const response = await changeSetsApi.listChangeSets({ workspaceId }); |
| 35 | + const changeSets = response.data.changeSets as ChangeSetViewV1[]; |
| 36 | + cs.stop(); |
| 37 | + |
| 38 | + const options = changeSets.filter((c) => !c.isHead).map((c) => { |
| 39 | + return { |
| 40 | + value: c.id, |
| 41 | + label: c.name, |
| 42 | + } |
| 43 | + }) |
| 44 | + const changeSetId = await p.select({ |
| 45 | + message: "Choose a change set:", |
| 46 | + options, |
| 47 | + }); |
| 48 | + |
| 49 | + if (p.isCancel(changeSetId)) { |
| 50 | + p.cancel("Cancelled, exiting...") |
| 51 | + process.exit(0); |
| 52 | + } |
| 53 | + |
| 54 | + const c = p.spinner(); |
| 55 | + |
| 56 | + c.start(`${color.bgBlack(color.greenBright("Retrieving components"))}`); |
| 57 | + const componentList = await mvSetsApi.get({ |
| 58 | + workspaceId, |
| 59 | + changeSetId, |
| 60 | + entityId: workspaceId, |
| 61 | + kind: "ComponentList" |
| 62 | + }) |
| 63 | + |
| 64 | + // N+1 requests are horribly in-efficient |
| 65 | + // if we wanted to invest in a TUI we could do the same |
| 66 | + // "sync all the MVs" on start, open a web socket, etc |
| 67 | + const componentDetails = await Promise.all(componentList.data.data.components.map((c) => { |
| 68 | + return mvSetsApi.get({ |
| 69 | + workspaceId, |
| 70 | + changeSetId, |
| 71 | + entityId: c.id, |
| 72 | + kind: "ComponentInList" |
| 73 | + }) |
| 74 | + })); |
| 75 | + c.stop(); |
| 76 | + |
| 77 | + const componentOptions = componentDetails.filter((req) => { |
| 78 | + const c = req.data.data; |
| 79 | + return c.diffStatus !== "None"; |
| 80 | + }).map((req) => { |
| 81 | + const c = req.data.data; |
| 82 | + return { |
| 83 | + value: c.id, |
| 84 | + label: c.name, |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + if (componentOptions.length === 0) { |
| 89 | + p.outro(`${color.bgBlack(color.redBright("There are no modifications on this simulated change set."))}`); |
| 90 | + p.outro("Goodbye!"); |
| 91 | + process.exit(0); |
| 92 | + } |
| 93 | + componentOptions.unshift({value: EXIT, label: "[quit]"}) |
| 94 | + |
| 95 | + while (true) { |
| 96 | + const componentId = await p.select({ |
| 97 | + message: "Choose a modified component to review:", |
| 98 | + options: componentOptions, |
| 99 | + }); |
| 100 | + if (p.isCancel(componentId)) { |
| 101 | + p.cancel("Cancelled, exiting...") |
| 102 | + process.exit(0); |
| 103 | + } |
| 104 | + if (componentId === EXIT) break; |
| 105 | + |
| 106 | + const diff = await mvSetsApi.get({ |
| 107 | + workspaceId, |
| 108 | + changeSetId, |
| 109 | + entityId: componentId, |
| 110 | + kind: "ComponentDiff" |
| 111 | + }); |
| 112 | + |
| 113 | + p.outro(`Here is what changed:`) |
| 114 | + console.log(JSON.stringify(diff.data.data, undefined, 2)); |
| 115 | + } |
| 116 | + |
| 117 | + p.outro("Goodbye"); |
| 118 | +} |
0 commit comments