workspace-job-migrator

Script windmill

by dieriba.pro916 ยท 10/24/2025

  • Submitted by dieriba.pro916 Bun
    Created 200 days ago
    1
    import wmill from "windmill-cli";
    2
    
    
    3
    export async function main(
    4
      source_workspace_id: string,
    5
      target_workspace_id: string
    6
    ) {
    7
      console.log("DEBUG: Migrate jobs...");
    8
      await wmill_run(
    9
        "workspace",
    10
        "migrate",
    11
        target_workspace_id,
    12
        "--source-workspace",
    13
        source_workspace_id,
    14
        "--token",
    15
        process.env["WM_TOKEN"] ?? "",
    16
        "--remote",
    17
        process.env["BASE_URL"] + "/",
    18
      );
    19
      console.log("DEBUG: Migrate jons completed");
    20
    }
    21
    
    
    22
    async function wmill_run(
    23
      ...cmd: string[]
    24
    ) {
    25
      cmd = cmd.filter((elt) => elt !== "");
    26
    
    
    27
      console.log(`DEBUG: Running CLI command: 'wmill ${cmd.slice().join(" ")} ...'`);
    28
    
    
    29
      // Capture CLI output to parse JSON response
    30
      const originalLog = console.log;
    31
      let cliOutput = "";
    32
      console.log = (msg: string) => {
    33
        cliOutput += msg + "\n";
    34
        originalLog(msg);
    35
      };
    36
    
    
    37
      try {
    38
        await wmill.parse(cmd);
    39
        console.log = originalLog;
    40
        console.log("DEBUG: CLI command executed successfully");
    41
      } catch (error) {
    42
        console.log = originalLog;
    43
        console.log("DEBUG: CLI command execution failed:", error);
    44
        throw error;
    45
      }
    46
      // END capture log
    47
    
    
    48
      console.log("DEBUG: Captured CLI output length:", cliOutput.length);
    49
      console.log("DEBUG: Raw CLI output:", cliOutput);
    50
    
    
    51
      try {
    52
        console.log("DEBUG: Attempting to parse CLI output as JSON...");
    53
    
    
    54
        // Find the first occurrence of '{' which indicates the start of JSON
    55
        const jsonStartIndex = cliOutput.indexOf('{');
    56
        if (jsonStartIndex === -1) {
    57
          console.log("DEBUG: No JSON found in CLI output");
    58
          return {};
    59
        }
    60
    
    
    61
        // Extract everything from the first '{' to the end
    62
        const jsonString = cliOutput.substring(jsonStartIndex).trim();
    63
        console.log("DEBUG: Extracted JSON string:", jsonString);
    64
    
    
    65
        const res = JSON.parse(jsonString);
    66
        console.log("DEBUG: Successfully parsed JSON result:", res);
    67
        return res;
    68
      } catch (e) {
    69
        console.log("DEBUG: Failed to parse CLI output as JSON:", e);
    70
        console.log("DEBUG: Returning empty object");
    71
        return {};
    72
      }
    73
    }
    74
    
    
    75