From 257f0971f86938da71b879f32d93473976eaa920 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Wed, 9 Oct 2024 12:58:42 +0200 Subject: [PATCH] fix(cli): instance sync push does not require sync pull --- cli/instance.ts | 76 ++++++++++++++++++++++++++++++------------------ cli/workspace.ts | 21 +++++++------ 2 files changed, 59 insertions(+), 38 deletions(-) diff --git a/cli/instance.ts b/cli/instance.ts index 6ad60ad113..30459d9ef6 100644 --- a/cli/instance.ts +++ b/cli/instance.ts @@ -25,8 +25,8 @@ import { import { add as workspaceSetup, addWorkspace, - allWorkspaces, removeWorkspace, + setActiveWorkspace, } from "./workspace.ts"; import { pushInstanceSettings, @@ -35,8 +35,9 @@ import { pushInstanceConfigs, type SimplifiedSettings, } from "./settings.ts"; -import { sleep, deepEqual } from "./utils.ts"; +import { deepEqual } from "./utils.ts"; import { GlobalOptions } from "./types.ts"; +import { getActiveWorkspace } from "./workspace.ts"; export interface Instance { remote: string; @@ -293,22 +294,18 @@ async function instancePull(opts: GlobalOptions & InstanceSyncOptions) { log.info("No instance-level changes to apply"); } - sleep(1000); - if (opts.includeWorkspaces) { log.info("\nPulling all workspaces"); + const rootDir = Deno.cwd(); + const localWorkspaces = await getLocalWorkspaces(rootDir, instance.prefix); + + const previousActiveWorkspace = await getActiveWorkspace(undefined); const remoteWorkspaces = await wmill.listWorkspacesAsSuperAdmin({ page: 1, perPage: 1000, }); - let localWorkspaces = await allWorkspaces(); - localWorkspaces = localWorkspaces.filter((w) => - w.name.startsWith(instance.prefix + "_") - ); - const rootDir = Deno.cwd(); for (const remoteWorkspace of remoteWorkspaces) { log.info("\nPulling workspace " + remoteWorkspace.id); - sleep(1000); const workspaceName = instance.prefix + "_" + remoteWorkspace.id; await Deno.mkdir(path.join(rootDir, workspaceName), { recursive: true, @@ -341,7 +338,7 @@ async function instancePull(opts: GlobalOptions & InstanceSyncOptions) { } const localWorkspacesToDelete = localWorkspaces.filter( - (w) => !remoteWorkspaces.find((r) => r.id === w.workspaceId) + (w) => !remoteWorkspaces.find((r) => r.id === w.id) ); if (localWorkspacesToDelete.length > 0) { @@ -350,20 +347,23 @@ async function instancePull(opts: GlobalOptions & InstanceSyncOptions) { (await Confirm.prompt({ message: "Do you want to delete the local copy of workspaces that don't exist anymore on the instance?\n" + - localWorkspacesToDelete.map((w) => w.workspaceId).join(", "), + localWorkspacesToDelete.map((w) => w).join(", "), default: true, })); if (confirmDelete) { for (const workspace of localWorkspacesToDelete) { - await removeWorkspace(workspace.name, false, {}); - await Deno.remove(path.join(rootDir, workspace.name), { + await removeWorkspace(workspace.id, false, {}); + await Deno.remove(path.join(rootDir, workspace.dir), { recursive: true, }); } } } + if (previousActiveWorkspace) { + await setActiveWorkspace(previousActiveWorkspace?.name); + } log.info(colors.green.underline.bold("All workspaces pulled")); } } @@ -421,10 +421,10 @@ async function instancePush(opts: GlobalOptions & InstanceSyncOptions) { log.info("No instance-level changes to apply"); } - sleep(1000); - if (opts.includeWorkspaces) { instances = await allInstances(); + const rootDir = Deno.cwd(); + const localPrefix = (await Select.prompt({ message: "What is the prefix of the local workspaces you want to sync?", options: [ @@ -440,18 +440,18 @@ async function instancePush(opts: GlobalOptions & InstanceSyncOptions) { page: 1, perPage: 1000, }); - let localWorkspaces = await allWorkspaces(); - localWorkspaces = localWorkspaces.filter((w) => - w.name.startsWith(localPrefix + "_") - ); - log.info("\nPushing all workspaces"); - const rootDir = Deno.cwd(); + const previousActiveWorkspace = await getActiveWorkspace(undefined); + + const localWorkspaces = await getLocalWorkspaces(rootDir, localPrefix); + + log.info( + `\nPushing all workspaces: ${localWorkspaces.map((x) => x.id).join(", ")}` + ); for (const localWorkspace of localWorkspaces) { - log.info("\nPushing workspace " + localWorkspace.workspaceId); - sleep(1000); + log.info("\nPushing workspace " + localWorkspace.id); try { - await Deno.chdir(path.join(rootDir, localWorkspace.name)); + await Deno.chdir(path.join(rootDir, localWorkspace.dir)); } catch (_) { throw new Error( "Workspace folder not found, are you in the right directory?" @@ -471,8 +471,8 @@ async function instancePush(opts: GlobalOptions & InstanceSyncOptions) { createWorkspaceName: workspaceSettings.name, createUsername: undefined, }, - localWorkspace.name, - localWorkspace.workspaceId, + localWorkspace.dir, + localWorkspace.id, instance.remote ); } catch (_) { @@ -482,7 +482,7 @@ async function instancePush(opts: GlobalOptions & InstanceSyncOptions) { continue; } await push({ - workspace: localWorkspace.name, + workspace: localWorkspace.dir, token: undefined, baseUrl: undefined, includeGroups: true, @@ -495,7 +495,7 @@ async function instancePush(opts: GlobalOptions & InstanceSyncOptions) { } const workspacesToDelete = remoteWorkspaces.filter( - (w) => !localWorkspaces.find((l) => l.workspaceId === w.id) + (w) => !localWorkspaces.find((l) => l.id === w.id) ); if (workspacesToDelete.length > 0) { const confirmDelete = @@ -514,10 +514,28 @@ async function instancePush(opts: GlobalOptions & InstanceSyncOptions) { } } } + if (previousActiveWorkspace) { + await setActiveWorkspace(previousActiveWorkspace?.name); + } log.info(colors.green.underline.bold("All workspaces pushed")); } } +async function getLocalWorkspaces(rootDir: string, localPrefix: string) { + const localWorkspaces: { dir: string; id: string }[] = []; + + for await (const dir of Deno.readDir(rootDir)) { + const dirName = dir.name; + if (dirName.startsWith(localPrefix + "_")) { + localWorkspaces.push({ + dir: dirName, + id: dirName.substring(localPrefix.length + 1), + }); + } + } + return localWorkspaces; +} + async function switchI(opts: {}, instanceName: string) { const all = await allInstances(); if (all.findIndex((x) => x.name === instanceName) === -1) { diff --git a/cli/workspace.ts b/cli/workspace.ts index face504183..4338e21cfe 100644 --- a/cli/workspace.ts +++ b/cli/workspace.ts @@ -34,10 +34,10 @@ export async function allWorkspaces(): Promise { } async function getActiveWorkspaceName( - opts: GlobalOptions + opts: GlobalOptions | undefined ): Promise { - if (opts.workspace) { - return opts.workspace; + if (opts?.workspace) { + return opts?.workspace; } try { return await Deno.readTextFile((await getRootStore()) + "/activeWorkspace"); @@ -47,7 +47,7 @@ async function getActiveWorkspaceName( } export async function getActiveWorkspace( - opts: GlobalOptions + opts: GlobalOptions | undefined ): Promise { const name = await getActiveWorkspaceName(opts); if (!name) { @@ -115,7 +115,12 @@ async function switchC(opts: GlobalOptions, workspaceName: string) { return; } - return await Deno.writeTextFile( + await setActiveWorkspace(workspaceName); + return; +} + +export async function setActiveWorkspace(workspaceName: string) { + await Deno.writeTextFile( (await getRootStore()) + "/activeWorkspace", workspaceName ); @@ -241,10 +246,8 @@ export async function add( }, opts ); - await Deno.writeTextFile( - (await getRootStore()) + "/activeWorkspace", - workspaceName - ); + await setActiveWorkspace(workspaceName); + log.info( colors.green.underline( `Added workspace ${workspaceName} for ${workspaceId} on ${remote}!`