-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun-analysis.ts
More file actions
156 lines (136 loc) · 5.24 KB
/
Copy pathrun-analysis.ts
File metadata and controls
156 lines (136 loc) · 5.24 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { SpawnOptions, spawn } from "node:child_process";
import path from "node:path";
import { Account } from "@tago-io/sdk";
import { getEnvironmentConfig, IEnvironment, resolveCLIPath } from "../../lib/config-file.js";
import { detectRuntime } from "../../lib/current-runtime.js";
import { errorHandler, highlightMSG, successMSG } from "../../lib/messages.js";
import { requireLocalScope } from "../../lib/resolve-scope.js";
import { searchName } from "../../lib/search-name.js";
import { pickAnalysisFromConfig } from "../../prompt/pick-analysis-from-config.js";
/**
* Builds the command to run the analysis.
* @param options - The options to configure the command.
* @param options.tsnd - Whether to use `tsnd` to run the command.
* @param options.debug - Whether to enable debugging for the command.
* @param options.clear - Whether to clear the console before running the command.
* @param options.runtime - The runtime to use ('deno' or 'node').
* @returns The built command as a string.
*/
function _buildCMD(options: { tsnd: boolean; debug: boolean; clear: boolean }, runtimeParam: string): string {
let cmd: string = "";
const runtime = runtimeParam === "--deno" ? "deno" : "node";
if (runtime === "deno") {
cmd = `deno run --allow-all --watch `;
if (options.debug) {
cmd += "--inspect ";
}
} else {
switch (options.tsnd) {
case true: {
cmd = `tsnd `;
if (options.debug) {
cmd += "--inspect -- ";
}
break;
}
default: {
// tsx wraps node with a CJS/ESM-aware TypeScript loader. Needed
// because Node's native --experimental-transform-types forces ESM
// resolution, which breaks legacy analyses that import CJS
// subpaths without a `.js` extension (e.g. "@tago-io/sdk/lib/types").
cmd = `node ${resolveCLIPath("/node_modules/tsx/dist/cli.mjs")} watch `;
if (options.debug) {
cmd += "--inspect ";
}
break;
}
}
if (options.clear) {
cmd += "--clear ";
}
}
return cmd;
}
/**
* Runs an analysis script.
* @param scriptName - The name of the script to run.
* @param options - The options for running the script.
* @returns void
*/
async function runAnalysis(
scriptName: string | undefined,
options: { environment: string; debug: boolean; clear: boolean; tsnd: boolean; deno: boolean; node: boolean },
) {
// Analysis development requires a project directory.
const scope = requireLocalScope("analysis-run");
const config = getEnvironmentConfig(options.environment);
if (!config || !config.profileToken) {
errorHandler("Environment not found");
}
const analysisList = (config.analysisList ?? []).filter((x) => x.fileName);
let scriptToRun: NonNullable<IEnvironment["analysisList"]>[number];
if (scriptName) {
scriptName = scriptName.toLowerCase();
scriptToRun = searchName(
scriptName,
analysisList.map((x) => ({ names: [x.name, x.fileName], value: x })),
);
} else {
scriptToRun = await pickAnalysisFromConfig(analysisList);
}
if (!scriptToRun || !scriptToRun.id) {
errorHandler(`Analysis couldn't be found: ${scriptName}`);
}
const account = new Account({ token: config.profileToken, region: config.profileRegion });
let { token: analysisToken, run_on, name, runtime: runtimeParam } = await account.analysis.info(scriptToRun.id);
const tokenSuffix = analysisToken ? ` [${highlightMSG(analysisToken)}]` : "";
successMSG(`> Analysis found: ${highlightMSG(scriptToRun.fileName)} (${name})${tokenSuffix}.`);
const analysisEnv: { [key: string]: string } = {
...process.env,
T_EXTERNAL: "external",
T_ANALYSIS_TOKEN: analysisToken,
T_ANALYSIS_ID: scriptToRun.id,
};
if (typeof config.profileRegion === "object") {
analysisEnv.TAGOIO_API = config.profileRegion.api;
if (config.profileRegion.sse) {
analysisEnv.TAGOIO_SSE = config.profileRegion.sse;
}
}
const spawnOptions: SpawnOptions = {
shell: true,
cwd: scope.root,
stdio: "inherit",
env: analysisEnv,
};
let scriptPath;
if (scriptToRun.path) {
scriptPath = path.join(config.analysisPath.concat("/", scriptToRun.path + "/"), scriptToRun.fileName).normalize();
} else {
scriptPath = path.join(config.analysisPath, scriptToRun.fileName).normalize();
}
let runtime;
if (options.deno && options.node) {
errorHandler("Cannot specify both --deno and --node flags");
} else if (options.deno) {
runtime = "--deno";
} else if (options.node) {
runtime = "--node";
} else {
runtime = detectRuntime(runtimeParam || "");
}
const cmd = _buildCMD(options, runtime);
if (run_on === "tago") {
await account.analysis.edit(scriptToRun.id, { run_on: "external" });
await new Promise((resolve) => setTimeout(resolve, 200)); // sleep
({ token: analysisToken, run_on, name } = await account.analysis.info(scriptToRun.id));
if (spawnOptions?.env) {
spawnOptions.env.T_ANALYSIS_TOKEN = analysisToken;
}
}
const spawnProccess = spawn(`${cmd}${scriptPath}`, spawnOptions);
const killAnalysis = async () => await account.analysis.edit(scriptToRun.id, { run_on: "tago" });
spawnProccess.on("close", killAnalysis);
spawnProccess.on("SIGINT", killAnalysis);
}
export { runAnalysis, _buildCMD };