fix(cli): improve instance sync for CI/CD + --folder-per-instance

This commit is contained in:
Ruben Fiszel
2024-10-29 18:30:45 +01:00
parent eb369676ad
commit 212579a514
2 changed files with 55 additions and 20 deletions
+55 -19
View File
@@ -181,6 +181,7 @@ export type InstanceSyncOptions = {
instance?: string;
baseUrl?: string;
token?: string;
folderPerInstance?: boolean;
yes?: boolean;
};
@@ -297,7 +298,11 @@ async function instancePull(opts: GlobalOptions & InstanceSyncOptions) {
if (opts.includeWorkspaces) {
log.info("\nPulling all workspaces");
const rootDir = Deno.cwd();
const localWorkspaces = await getLocalWorkspaces(rootDir, instance.prefix);
const localWorkspaces = await getLocalWorkspaces(
rootDir,
instance.prefix,
opts.folderPerInstance
);
const previousActiveWorkspace = await getActiveWorkspace(undefined);
const remoteWorkspaces = await wmill.listWorkspacesAsSuperAdmin({
@@ -306,7 +311,9 @@ async function instancePull(opts: GlobalOptions & InstanceSyncOptions) {
});
for (const remoteWorkspace of remoteWorkspaces) {
log.info("\nPulling workspace " + remoteWorkspace.id);
const workspaceName = instance.prefix + "_" + remoteWorkspace.id;
const workspaceName = opts?.folderPerInstance
? instance.prefix + "/" + remoteWorkspace.id
: instance.prefix + "_" + remoteWorkspace.id;
await Deno.mkdir(path.join(rootDir, workspaceName), {
recursive: true,
});
@@ -425,16 +432,21 @@ async function instancePush(opts: GlobalOptions & InstanceSyncOptions) {
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: [
...instances.map((i) => ({
name: `${i.prefix} (${i.name} - ${i.remote})`,
value: i.prefix,
})),
],
default: instance.prefix as unknown,
})) as unknown as string;
let localPrefix;
if (opts.baseUrl && opts.token) {
localPrefix = "custom";
} else {
localPrefix = (await Select.prompt({
message: "What is the prefix of the local workspaces you want to sync?",
options: [
...instances.map((i) => ({
name: `${i.prefix} (${i.name} - ${i.remote})`,
value: i.prefix,
})),
],
default: instance.prefix as unknown,
})) as unknown as string;
}
const remoteWorkspaces = await wmill.listWorkspacesAsSuperAdmin({
page: 1,
@@ -443,7 +455,11 @@ async function instancePush(opts: GlobalOptions & InstanceSyncOptions) {
const previousActiveWorkspace = await getActiveWorkspace(undefined);
const localWorkspaces = await getLocalWorkspaces(rootDir, localPrefix);
const localWorkspaces = await getLocalWorkspaces(
rootDir,
localPrefix,
opts.folderPerInstance
);
log.info(
`\nPushing all workspaces: ${localWorkspaces.map((x) => x.id).join(", ")}`
@@ -521,17 +537,35 @@ async function instancePush(opts: GlobalOptions & InstanceSyncOptions) {
}
}
async function getLocalWorkspaces(rootDir: string, localPrefix: string) {
async function getLocalWorkspaces(
rootDir: string,
localPrefix: string,
folderPerInstance?: boolean
) {
const localWorkspaces: { dir: string; id: string }[] = [];
for await (const dir of Deno.readDir(rootDir)) {
const dirName = dir.name;
if (dirName.startsWith(localPrefix + "_")) {
if (!(await Deno.stat(localPrefix).catch(() => null))) {
await Deno.mkdir(localPrefix);
}
if (folderPerInstance) {
for await (const dir of Deno.readDir(rootDir + "/" + localPrefix)) {
const dirName = dir.name;
localWorkspaces.push({
dir: dirName,
id: dirName.substring(localPrefix.length + 1),
dir: localPrefix + "/" + dirName,
id: dirName,
});
}
log.info(localWorkspaces);
} else {
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;
}
@@ -650,6 +684,7 @@ const command = new Command()
.option("--skip-configs", "Skip pulling configs (worker groups and SMTP)")
.option("--skip-groups", "Skip pulling instance groups")
.option("--include-workspaces", "Also pull workspaces")
.option("--folder-per-instance", "Create a folder per instance")
.option(
"--instance <instance:string>",
"Name of the instance to push to, override the active instance"
@@ -665,6 +700,7 @@ const command = new Command()
.option("--skip-configs", "Skip pushing configs (worker groups and SMTP)")
.option("--skip-groups", "Skip pushing instance groups")
.option("--include-workspaces", "Also push workspaces")
.option("--folder-per-instance", "Create a folder per instance")
.option(
"--instance <instance:string>",
"Name of the instance to push to, override the active instance"
-1
View File
@@ -1,4 +1,3 @@
import { log } from "./deps.ts";
import crypto from "node:crypto";
// Helper function to convert strings to Uint8Array (binary)