From 678b574efcae66801a115d576db9d00aa9e4145d Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Mon, 15 May 2023 00:08:38 +0200 Subject: [PATCH] fix(cli): update wmill script push --- cli/flow.ts | 12 +++------ cli/script.ts | 74 +++++++-------------------------------------------- cli/sync.ts | 14 ++-------- cli/types.ts | 5 +--- 4 files changed, 16 insertions(+), 89 deletions(-) diff --git a/cli/flow.ts b/cli/flow.ts index e3f3427296..0358a36fec 100644 --- a/cli/flow.ts +++ b/cli/flow.ts @@ -26,8 +26,7 @@ const alreadySynced: string[] = []; export async function pushFlow( workspace: string, remotePath: string, - localFlowPath: string, - workspaceId: string + localFlowPath: string ): Promise { if (alreadySynced.includes(localFlowPath)) { return; @@ -36,7 +35,7 @@ export async function pushFlow( let flow: Flow | undefined = undefined; try { flow = await FlowService.getFlowByPath({ - workspace: workspaceId, + workspace: workspace, path: remotePath, }); } catch { @@ -102,12 +101,7 @@ async function push(opts: Options, filePath: string, remotePath: string) { const workspace = await resolveWorkspace(opts); await requireLogin(opts); - await pushFlow( - workspace.workspaceId, - remotePath, - filePath, - workspace.workspaceId - ); + await pushFlow(workspace.workspaceId, remotePath, filePath); console.log(colors.bold.underline.green("Flow pushed")); } diff --git a/cli/script.ts b/cli/script.ts index fb21694466..d0b376b656 100644 --- a/cli/script.ts +++ b/cli/script.ts @@ -1,5 +1,5 @@ // deno-lint-ignore-file no-explicit-any -import { GlobalOptions, parseFromFile, removeType } from "./types.ts"; +import { GlobalOptions } from "./types.ts"; import { requireLogin, resolveWorkspace, validatePath } from "./context.ts"; import { colors, @@ -38,18 +38,15 @@ async function push(opts: PushOptions, filePath: string) { if (!fstat.isFile) { throw new Error("file path must refer to a file."); } - let contentPath: string; - let metaPath: string | undefined; + if (filePath.endsWith(".script.json") || filePath.endsWith(".script.yaml")) { - metaPath = filePath; - contentPath = await findContentFile(filePath); - } else { - contentPath = filePath; - metaPath = undefined; + throw Error( + "Cannot push a script metadata file, point to the script content file instead (.py, .ts, .go|.sh)" + ); } await requireLogin(opts); - await pushScript(metaPath, contentPath, workspace.workspaceId, remotePath); + await handleFile(filePath, workspace.workspaceId, []); log.info(colors.bold.underline.green(`Script ${remotePath} pushed`)); } @@ -60,12 +57,7 @@ export async function handleScriptMetadata( ): Promise { if (path.endsWith(".script.json") || path.endsWith(".script.yaml")) { const contentPath = await findContentFile(path); - return handleFile( - contentPath, - await Deno.readTextFile(contentPath), - workspace, - alreadySynced - ); + return handleFile(contentPath, workspace, alreadySynced); } else { return false; } @@ -73,7 +65,6 @@ export async function handleScriptMetadata( export async function handleFile( path: string, - content: string, workspace: string, alreadySynced: string[] ): Promise { @@ -118,6 +109,7 @@ export async function handleFile( } catch { log.debug(`Script ${remotePath} does not exist on remote`); } + const content = await Deno.readTextFile(path); if (remote) { if (content === remote.content) { @@ -242,52 +234,6 @@ export function inferContentTypeFromFilePath( return language; } -export async function pushScript( - filePath: string | undefined, - contentPath: string, - workspace: string, - remotePath: string -) { - remotePath = removeType(remotePath, "script"); - - const data: ScriptFile | undefined = filePath - ? parseFromFile(filePath) - : undefined; - const content = await Deno.readTextFile(contentPath); - - const language = inferContentTypeFromFilePath(contentPath); - let parent_hash = data?.parent_hash; - if (!parent_hash) { - try { - parent_hash = ( - await ScriptService.getScriptByPath({ - workspace: workspace, - path: remotePath, - }) - ).hash; - } catch { - /* no parent. New Script. */ - } - } - - log.info(colors.bold.yellow(`Pushing script ${remotePath}...`)); - await ScriptService.createScript({ - workspace: workspace, - requestBody: { - path: remotePath, - summary: data?.summary ?? "", - content: content, - description: data?.description ?? "", - language: language as NewScript.language, - is_template: data?.is_template, - kind: data?.kind as NewScript.kind, - lock: data?.lock, - parent_hash: parent_hash, - schema: data?.schema, - }, - }); -} - async function list(opts: GlobalOptions & { showArchived?: boolean }) { const workspace = await resolveWorkspace(opts); await requireLogin(opts); @@ -473,9 +419,9 @@ const command = new Command() .action(list as any) .command( "push", - "push a local script spec. This overrides any remote versions. Can use a script file (.ts, .js, .py, .sh) or a script spec file (.json). " + "push a local script spec. This overrides any remote versions. Use the script file (.ts, .js, .py, .sh)" ) - .arguments("") + .arguments("") .action(push as any) .command("show", "show a scripts content") .arguments("") diff --git a/cli/sync.ts b/cli/sync.ts index b86d804d2f..853137b8e9 100644 --- a/cli/sync.ts +++ b/cli/sync.ts @@ -662,12 +662,7 @@ async function push( } continue; } else if ( - await handleFile( - change.path, - change.after, - workspace.workspaceId, - alreadySynced - ) + await handleFile(change.path, workspace.workspaceId, alreadySynced) ) { if (!opts.raw && stateExists) { await Deno.writeTextFile(stateTarget, change.after); @@ -700,12 +695,7 @@ async function push( ) { continue; } else if ( - await handleFile( - change.path, - change.content, - workspace.workspaceId, - alreadySynced - ) + await handleFile(change.path, workspace.workspaceId, alreadySynced) ) { continue; } diff --git a/cli/types.ts b/cli/types.ts index baea96d5a2..ca59a289ce 100644 --- a/cli/types.ts +++ b/cli/types.ts @@ -8,7 +8,6 @@ import { } from "https://deno.land/std@0.184.0/yaml/mod.ts"; import { equal } from "https://deno.land/x/equal@v1.5.0/equal.ts"; import { pushFolder } from "./folder.ts"; -import { pushScript } from "./script.ts"; import { pushFlow } from "./flow.ts"; import { pushResource } from "./resource.ts"; import { pushResourceType } from "./resource-type.ts"; @@ -111,13 +110,11 @@ export function pushObj( pushApp(workspace, p, befObj, newObj, checkForCreate); } else if (typeEnding === "folder") { pushFolder(workspace, p, befObj, newObj, checkForCreate); - } else if (typeEnding === "script") { - pushScript(workspace, p, befObj, newObj); } else if (typeEnding === "variable") { pushVariable(workspace, p, befObj, newObj, plainSecrets, checkForCreate); } else if (typeEnding === "flow") { const flowName = p.split(".flow/")[0]; - pushFlow(workspace, flowName, flowName + ".flow", workspace); + pushFlow(workspace, flowName, flowName + ".flow"); } else if (typeEnding === "resource") { pushResource(workspace, p, befObj, newObj, checkForCreate); } else if (typeEnding === "resource-type") {