-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathduplicate-analysis.ts
More file actions
113 lines (98 loc) · 4.19 KB
/
Copy pathduplicate-analysis.ts
File metadata and controls
113 lines (98 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { Account, AnalysisInfo } from "@tago-io/sdk";
import kleur from "kleur";
import prompts from "prompts";
import zlib from "node:zlib";
import { getEnvironmentConfig } from "../../lib/config-file.js";
import { errorHandler, successMSG } from "../../lib/messages.js";
import { requireLocalScope } from "../../lib/resolve-scope.js";
import { pickAnalysisFromTagoIO } from "../../prompt/pick-analysis-from-tagoio.js";
/**
* Asks the user to choose the duplicated analysis name.
* @param initial - The initial value for the analysis name.
* @returns A promise that resolves to the analysis name chosen by the user.
*/
async function askAnalysisName(initial: string) {
const { name } = await prompts({
message: "Choose the duplicated analysis",
name: "name",
type: "text",
initial,
});
return name;
}
/**
* Creates a new analysis in TagoIO.
* @param account - The TagoIO account object.
* @param newAnalysisName - The name of the new analysis.
* @param scriptBase64 - The base64-encoded script for the new analysis.
* @param analysis - The analysis object to be duplicated.
* @returns A Promise that resolves when the new analysis is successfully created.
*/
async function createNewAnalysis(account: Account, newAnalysisName: string, scriptBase64: string, analysis: AnalysisInfo) {
const { id: new_analysis_id } = await account.analysis.create({
...analysis,
name: newAnalysisName,
});
await account.analysis.uploadScript(new_analysis_id, {
content: scriptBase64,
language: analysis.runtime || "node",
name: "script.js",
});
successMSG(`Analysis duplicated. source=${kleur.blue(analysis.id)} target=${kleur.blue(new_analysis_id)} name=${newAnalysisName}`);
}
/**
* Downloads the script of an analysis and returns it as a base64 string.
* @param account - The TagoIO account object.
* @param analysisId - The ID of the analysis to download the script from.
* @returns A promise that resolves to the base64-encoded script.
* @throws If the script download fails.
*/
async function downloadScriptBase64(account: Account, analysisId: string): Promise<string> {
try {
const script = await account.analysis.downloadScript(analysisId);
const response = await fetch(script.url);
if (!response.ok) {
throw new Error(`Request failed: ${response.status}`);
}
const buffer = Buffer.from(await response.arrayBuffer());
return zlib.gunzipSync(buffer).toString("base64");
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
errorHandler(`Failed to download script for analysis ID ${analysisId}: ${message}`);
}
}
/**
* Duplicates an analysis in TagoIO.
* @param analysisID - The ID of the analysis to be duplicated.
* @param options - The options for the duplication process.
* @param options.environment - The environment where the analysis is located.
* @param options.name - The name of the new analysis. If not provided, the name will be "original analysis name - Copy".
* @returns A Promise that resolves when the analysis is successfully duplicated.
* @throws An error if the analysis ID is not found or if the environment is not found.
*/
async function duplicateAnalysis(analysisID: string | void, options: { environment: string; name?: string }) {
requireLocalScope("analysis-duplicate");
const config = getEnvironmentConfig(options.environment);
if (!config || !config.profileToken) {
errorHandler("Environment not found");
}
const account = new Account({ token: config.profileToken });
if (!analysisID) {
const analysis = await pickAnalysisFromTagoIO(account, "Pick the analysis you want to duplicate");
analysisID = analysis.id;
}
if (!analysisID) {
errorHandler("Cancelled");
}
const analysis = await account.analysis.info(analysisID).catch(() => null);
if (!analysis) {
throw errorHandler(`Analysis ID ${analysisID} can't be found`);
}
const scriptBase64 = await downloadScriptBase64(account, analysisID);
const newAnalysisName = options.name ?? `${analysis.name} - Copy`;
if (!options.name) {
options.name = await askAnalysisName(newAnalysisName);
}
await createNewAnalysis(account, newAnalysisName, scriptBase64, analysis);
}
export { duplicateAnalysis };