From eb000f1fbc697869aab46b3be430c8d56b7a7e8c Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Thu, 18 Jan 2024 09:01:04 +0100 Subject: [PATCH] fix: make cli backcompatible with respect to lockfile --- cli/context.ts | 27 +++++++++++++++------------ cli/script.ts | 36 ++++++++++++++++++++++++------------ cli/sync.ts | 26 ++++++++++++++++++++++---- 3 files changed, 61 insertions(+), 28 deletions(-) diff --git a/cli/context.ts b/cli/context.ts index 787e6b71fb..7925cd26c7 100644 --- a/cli/context.ts +++ b/cli/context.ts @@ -95,16 +95,7 @@ export async function requireLogin( } } -export async function tryResolveVersion( - opts: GlobalOptions -): Promise { - if ((opts as any).__cache_version) { - return (opts as any).__cache_version; - } - - const workspaceRes = await tryResolveWorkspace(opts); - if (workspaceRes.isError) return undefined; - +export async function fetchVersion(baseUrl: string): Promise { const requestHeaders: HeadersInit = new Headers(); const extraHeaders = getHeaders(); @@ -115,10 +106,22 @@ export async function tryResolveVersion( } const response = await fetch( - new URL(new URL(workspaceRes.value.remote).origin + "/api/version"), + new URL(new URL(baseUrl).origin + "/api/version"), { headers: requestHeaders, method: "GET" } ); - const version = await response.text(); + return await response.text(); +} +export async function tryResolveVersion( + opts: GlobalOptions +): Promise { + if ((opts as any).__cache_version) { + return (opts as any).__cache_version; + } + + const workspaceRes = await tryResolveWorkspace(opts); + if (workspaceRes.isError) return undefined; + const version = await fetchVersion(workspaceRes.value.remote); + try { return Number.parseInt( version.split("-", 1)[0].replaceAll(".", "").replace("v", "") diff --git a/cli/script.ts b/cli/script.ts index 3061a483a7..fc9cea24be 100644 --- a/cli/script.ts +++ b/cli/script.ts @@ -67,18 +67,26 @@ async function push(opts: PushOptions, filePath: string) { } await requireLogin(opts); - await handleFile(filePath, workspace.workspaceId, []); + await handleFile(filePath, workspace.workspaceId, [], undefined, false); log.info(colors.bold.underline.green(`Script ${filePath} pushed`)); } export async function handleScriptMetadata( path: string, workspace: string, - alreadySynced: string[] + alreadySynced: string[], + message: string | undefined, + lockfileUseArray: boolean ): Promise { if (path.endsWith(".script.json") || path.endsWith(".script.yaml")) { const contentPath = await findContentFile(path); - return handleFile(contentPath, workspace, alreadySynced); + return handleFile( + contentPath, + workspace, + alreadySynced, + message, + lockfileUseArray + ); } else { return false; } @@ -98,8 +106,12 @@ async function parseMetadataFile( try { metadataFilePath = scriptPath + ".script.yaml"; await Deno.stat(metadataFilePath); + let payload: any = yamlParse(await Deno.readTextFile(metadataFilePath)); + if (Array.isArray(payload?.["lock"])) { + payload = payload["lock"].join("\n"); + } return { - payload: yamlParse(await Deno.readTextFile(metadataFilePath)), + payload, isJson: false, }; } catch { @@ -124,7 +136,8 @@ export async function handleFile( path: string, workspace: string, alreadySynced: string[], - message?: string + message: string | undefined, + lockfileUseArray: boolean ): Promise { if ( !path.includes(".inline_script.") && @@ -169,11 +182,10 @@ export async function handleFile( (typed.is_template ?? false) === (remote.is_template ?? false) && typed.kind == remote.kind && !remote.archived && - (remote?.lock ?? "").trim() == - (Array.isArray(typed.lock) - ? typed.lock.join("\n") - : typed?.lock ?? "" - ).trim() && + (Array.isArray(remote?.lock) + ? remote?.lock?.join("\n") + : remote?.lock ?? "" + ).trim() == (typed?.lock ?? "").trim() && deepEqual(typed.schema, remote.schema) && typed.tag == remote.tag && (typed.ws_error_handler_muted ?? false) == @@ -202,7 +214,7 @@ export async function handleFile( summary: typed?.summary ?? "", is_template: typed?.is_template, kind: typed?.kind, - lock: typed?.lock, + lock: lockfileUseArray ? typed?.lock.split("\n") : typed?.lock, parent_hash: remote.hash, schema: typed?.schema, tag: typed?.tag, @@ -229,7 +241,7 @@ export async function handleFile( summary: typed?.summary ?? "", is_template: typed?.is_template, kind: typed?.kind, - lock: typed?.lock, + lock: lockfileUseArray ? typed?.lock.split("\n") : typed?.lock, parent_hash: undefined, schema: typed?.schema, tag: typed?.tag, diff --git a/cli/sync.ts b/cli/sync.ts index eb82ff85f7..b3f67205b2 100644 --- a/cli/sync.ts +++ b/cli/sync.ts @@ -1,4 +1,4 @@ -import { requireLogin, resolveWorkspace } from "./context.ts"; +import { fetchVersion, requireLogin, resolveWorkspace } from "./context.ts"; import { colors, Command, @@ -710,6 +710,19 @@ async function push( opts ); + const version = await fetchVersion(workspace.remote); + + log.info(colors.gray("Remote version: " + version)); + + const reducedVersion = version + .split(" v")[1] + .split("-")[0] + .split(".") + .map((v) => parseInt(v)); + const lockfileUseArray = + reducedVersion[1] < 246 || + reducedVersion[1] == 246 || + reducedVersion[2] < 5; log.info( `remote (${workspace.name}) <- local: ${changes.length} changes to apply` ); @@ -726,6 +739,7 @@ async function push( return; } log.info(colors.gray(`Applying changes to files ...`)); + const alreadySynced: string[] = []; for await (const change of changes) { const stateTarget = path.join(Deno.cwd(), ".wmill", change.path); @@ -741,7 +755,9 @@ async function push( await handleScriptMetadata( change.path, workspace.workspaceId, - alreadySynced + alreadySynced, + opts.message, + lockfileUseArray ) ) { if (!opts.raw && stateExists) { @@ -753,7 +769,8 @@ async function push( change.path, workspace.workspaceId, alreadySynced, - opts.message + opts.message, + lockfileUseArray ) ) { if (!opts.raw && stateExists) { @@ -791,7 +808,8 @@ async function push( change.path, workspace.workspaceId, alreadySynced, - opts.message + opts.message, + lockfileUseArray ) ) { continue;