|
| 1 | +import { InCloudClient, InLiveClient } from "@inspatial/cloud-client"; |
| 2 | +import { cliLog } from "@inspatial/cloud/incloud"; |
| 3 | +import { joinPath } from "~/utils/path-utils.ts"; |
| 4 | +interface BaseOptions { |
| 5 | + host?: string; |
| 6 | + port?: number; |
| 7 | + onNotify?: (message: string) => void; |
| 8 | +} |
| 9 | +interface Credential { |
| 10 | + email: string; |
| 11 | + password: string; |
| 12 | +} |
| 13 | + |
| 14 | +interface TokenCredentials { |
| 15 | + token: string; |
| 16 | +} |
| 17 | + |
| 18 | +type Credentials = Credential | TokenCredentials; |
| 19 | +const argsMap = new Map<string, string>(Object.entries({ |
| 20 | + "-t": "token", |
| 21 | + "--token": "token", |
| 22 | + "-h": "host", |
| 23 | + "--host": "host", |
| 24 | + "-P": "port", |
| 25 | + "--port": "port", |
| 26 | + "-f": "filePath", |
| 27 | + "--filePath": "filePath", |
| 28 | +})); |
| 29 | + |
| 30 | +async function listenForEscapeKey(callback?: () => void) { |
| 31 | + Deno.stdin.setRaw(true); |
| 32 | + |
| 33 | + const escapeKey = new Uint8Array([27]); // Escape key in ASCII |
| 34 | + while (true) { |
| 35 | + const buffer = new Uint8Array(1); |
| 36 | + const bytesRead = await Deno.stdin.read(buffer); |
| 37 | + if (bytesRead === null) { |
| 38 | + break; // EOF |
| 39 | + } |
| 40 | + if (buffer[0] === escapeKey[0]) { |
| 41 | + console.log("Escape key pressed. Exiting..."); |
| 42 | + Deno.stdin.setRaw(false); |
| 43 | + if (callback) { |
| 44 | + callback(); |
| 45 | + } |
| 46 | + |
| 47 | + Deno.exit(0); |
| 48 | + } |
| 49 | + } |
| 50 | + Deno.stdin.setRaw(false); // Reset to normal mode |
| 51 | +} |
| 52 | +function parseArgs(): Map<string, string | boolean> { |
| 53 | + const argsRecord = new Map<string, string | boolean>(); |
| 54 | + for (let i = 1; i < Deno.args.length; i++) { |
| 55 | + const arg = Deno.args[i]; |
| 56 | + const nextArg = Deno.args[i + 1]?.trim(); |
| 57 | + const parseArg = (trim: number) => { |
| 58 | + if (!nextArg.startsWith("-")) { |
| 59 | + argsRecord.set(arg.slice(trim), nextArg); |
| 60 | + i++; // Skip the next argument as it's the value for this flag |
| 61 | + return; |
| 62 | + } |
| 63 | + argsRecord.set(arg.slice(trim), true); |
| 64 | + }; |
| 65 | + if (arg.startsWith("--")) { |
| 66 | + parseArg(2); |
| 67 | + continue; |
| 68 | + } |
| 69 | + if (arg.startsWith("-")) { |
| 70 | + parseArg(1); |
| 71 | + continue; |
| 72 | + } |
| 73 | + } |
| 74 | + return argsRecord; |
| 75 | +} |
| 76 | +export async function syncEntryInterface() { |
| 77 | + const args = parseArgs(); |
| 78 | + const token = args.get("token") || args.get("t"); |
| 79 | + let filePath = args.get("filePath") || args.get("f"); |
| 80 | + if (typeof filePath === "boolean" || filePath === undefined) { |
| 81 | + filePath = Deno.cwd(); |
| 82 | + } |
| 83 | + filePath = Deno.realPathSync(filePath); |
| 84 | + const file = joinPath(filePath, "cloudTypes.d.ts"); |
| 85 | + let host = args.get("host") || args.get("h"); |
| 86 | + if (typeof host === "boolean") { |
| 87 | + host = undefined; // If the host is set to true, we treat it as not provided |
| 88 | + } |
| 89 | + const port = args.get("port") || args.get("P"); |
| 90 | + if (host && port) { |
| 91 | + host += `:${port}`; |
| 92 | + } |
| 93 | + if (!host) { |
| 94 | + host = `http://localhost:${port || "8000"}`; |
| 95 | + } |
| 96 | + if (!token || typeof token === "boolean") { |
| 97 | + cliLog.warn( |
| 98 | + "No credentials provided. Use --token <token>, or -t <token>", |
| 99 | + "MissingCredentialsError", |
| 100 | + ); |
| 101 | + Deno.exit(1); |
| 102 | + } |
| 103 | + const inCloud = new InCloudClient(`${host}/api`); |
| 104 | + inCloud.headers.set("Authorization", `Bearer ${token}`); |
| 105 | + const writeClientFile = async () => { |
| 106 | + cliLog.warn("Syncing client interfaces", { |
| 107 | + compact: true, |
| 108 | + subject: "Sync", |
| 109 | + }); |
| 110 | + const response = await fetch( |
| 111 | + `${host}/api?group=orm&action=getClientInterfaces |
| 112 | +`, |
| 113 | + { |
| 114 | + headers: { |
| 115 | + "Authorization": `Bearer ${token}`, |
| 116 | + }, |
| 117 | + }, |
| 118 | + ); |
| 119 | + const text = await response.text(); |
| 120 | + Deno.writeTextFileSync(file, text, { |
| 121 | + create: true, |
| 122 | + append: false, |
| 123 | + }); |
| 124 | + cliLog.info(`Client interfaces written to ${file}`, { |
| 125 | + compact: true, |
| 126 | + subject: "Sync", |
| 127 | + }); |
| 128 | + }; |
| 129 | + const session = await inCloud.auth.authCheck(); |
| 130 | + if (!session) { |
| 131 | + cliLog.warn( |
| 132 | + "Session check failed. Please ensure your credentials are correct.", |
| 133 | + ); |
| 134 | + Deno.exit(1); |
| 135 | + } |
| 136 | + cliLog.info( |
| 137 | + [ |
| 138 | + `Connected to InCloud at ${host}`, |
| 139 | + `as ${session.firstName} ${session.lastName} (${session.email})`, |
| 140 | + ], |
| 141 | + "ConnectionSuccess", |
| 142 | + ); |
| 143 | + const inLive = new InLiveClient(`${host}/ws`); |
| 144 | + writeClientFile(); |
| 145 | + inLive.onConnectionStatus((status) => { |
| 146 | + switch (status) { |
| 147 | + case "reconnected": |
| 148 | + writeClientFile(); |
| 149 | + break; |
| 150 | + } |
| 151 | + }); |
| 152 | + inLive.start(token); |
| 153 | + |
| 154 | + listenForEscapeKey(() => { |
| 155 | + inLive.client.disconnect(); |
| 156 | + }).catch((err) => { |
| 157 | + cliLog.error("Error listening for escape key:", err); |
| 158 | + }); |
| 159 | +} |
0 commit comments