Skip to content

Commit 238de74

Browse files
authored
Remove Duplicate Command Setup Code (#85)
Introducing a setup function that is used by most (non-deprecated) commands. We also remove some unnecessary console logs from tests.
1 parent cb38317 commit 238de74

File tree

5 files changed

+36
-68
lines changed

5 files changed

+36
-68
lines changed

src/commands/delete.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
11
import { Command } from "commander";
22

3+
import { setup } from "./setup.ts";
34
import { PluginService } from "../services/plugin";
4-
import { deployedUrl } from "../utils/deployed-url";
5-
import { validateAndParseOpenApiSpec } from "../utils/openapi";
6-
import { getSpecUrl, getHostname } from "../utils/url";
75

86
export const deleteCommand = new Command()
97
.name("delete")
108
.description("Delete your AI agent plugin")
119
.option("-u, --url <url>", "Specify the deployment URL")
1210
.action(async (options) => {
13-
const url = options.url || deployedUrl;
14-
15-
if (!url) {
16-
console.error("Deployed URL could not be determined.");
17-
return;
18-
}
19-
20-
const pluginId = getHostname(url);
21-
const specUrl = getSpecUrl(url);
22-
const xMbSpec = await validateAndParseOpenApiSpec(specUrl);
23-
24-
if (!xMbSpec) {
25-
console.error("OpenAPI specification validation failed.");
26-
return;
27-
}
28-
const accountId = xMbSpec["account-id"];
29-
3011
const pluginService = new PluginService();
31-
const authentication = await pluginService.auth.getAuthentication();
12+
const [{ pluginId }, authentication] = await Promise.all([
13+
setup(options.url),
14+
pluginService.auth.getAuthentication(),
15+
]);
16+
3217
if (!authentication) {
3318
console.error("Authentication failed. Unable to delete the plugin.");
3419
return;

src/commands/deploy.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { Command } from "commander";
22

3+
import { setup } from "./setup.ts";
34
import { PluginService } from "../services/plugin";
4-
import { deployedUrl } from "../utils/deployed-url";
5-
import { validateAndParseOpenApiSpec } from "../utils/openapi";
6-
import { getSpecUrl, getHostname } from "../utils/url";
75

86
export const deployCommand = new Command()
97
.name("deploy")
@@ -12,33 +10,16 @@ export const deployCommand = new Command()
1210
)
1311
.option("-u, --url <url>", "Specify the deployment URL")
1412
.action(async (options) => {
15-
const url = options.url || deployedUrl;
16-
17-
if (!url) {
18-
console.error("Deployed URL could not be determined.");
19-
return;
20-
} else {
21-
console.log("Using deployment URL:", url);
22-
}
23-
24-
const id = getHostname(url);
25-
const specUrl = getSpecUrl(url);
26-
const xMbSpec = await validateAndParseOpenApiSpec(specUrl);
27-
if (!xMbSpec) {
28-
console.error("OpenAPI specification validation failed.");
29-
return;
30-
}
13+
const { pluginId } = await setup(options.url);
3114

3215
const pluginService = new PluginService();
3316
try {
34-
const updateRes = await pluginService.update(id);
17+
const updateRes = await pluginService.update(pluginId);
3518
if (!updateRes) {
3619
console.log("Attempting to register plugin...");
37-
await pluginService.register({
38-
pluginId: id,
39-
});
20+
await pluginService.register({ pluginId });
4021
}
4122
} catch (error) {
42-
console.error(`Failed to deploy plugin ${id}. Error: ${error}`);
23+
console.error(`Failed to deploy plugin ${pluginId}. Error: ${error}`);
4324
}
4425
});

src/commands/setup.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { XMbSpec } from "../config/types.ts";
2+
import { deployedUrl } from "../utils/deployed-url.ts";
3+
import { validateAndParseOpenApiSpec } from "../utils/openapi.ts";
4+
import { getHostname, getSpecUrl } from "../utils/url.ts";
5+
6+
export async function setup(
7+
optionsUrl?: string,
8+
): Promise<{ pluginId: string; xMbSpec: XMbSpec }> {
9+
const url = optionsUrl || deployedUrl;
10+
11+
if (!url) {
12+
throw new Error("Deployed URL could not be determined.");
13+
}
14+
15+
const pluginId = getHostname(url);
16+
const specUrl = getSpecUrl(url);
17+
const xMbSpec = await validateAndParseOpenApiSpec(specUrl);
18+
if (!xMbSpec) {
19+
throw new Error("OpenAPI specification validation failed.");
20+
}
21+
return { pluginId, xMbSpec };
22+
}

src/commands/verify.ts

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { Command } from "commander";
22

3+
import { setup } from "./setup.ts";
34
import type { VerifyData, XMbSpec } from "../config/types";
45
import { PluginService } from "../services/plugin";
5-
import { deployedUrl } from "../utils/deployed-url";
6-
import { validateAndParseOpenApiSpec } from "../utils/openapi";
7-
import { getHostname, getSpecUrl } from "../utils/url";
86

97
export const verifyCommand = new Command()
108
.name("verify")
@@ -43,20 +41,7 @@ export const verifyCommand = new Command()
4341
},
4442
)
4543
.action(async (options) => {
46-
const url = options.url || deployedUrl;
47-
48-
if (!url) {
49-
console.error("Deployed URL could not be determined.");
50-
return;
51-
}
52-
53-
const pluginId = getHostname(url);
54-
const specUrl = getSpecUrl(url);
55-
const xMbSpec = await validateAndParseOpenApiSpec(specUrl);
56-
if (!xMbSpec) {
57-
console.error("OpenAPI specification validation failed.");
58-
return;
59-
}
44+
const { pluginId, xMbSpec } = await setup(options.url);
6045

6146
try {
6247
const agentData = formVerifyData(options, xMbSpec);
@@ -71,7 +56,7 @@ export const verifyCommand = new Command()
7156
});
7257

7358
function formVerifyData(options: unknown, spec: XMbSpec): VerifyData {
74-
const verifyData: VerifyData = {
59+
return {
7560
accountId: spec["account-id"],
7661
email:
7762
((options as { email?: string }).email ?? spec.email) ||
@@ -91,5 +76,4 @@ function formVerifyData(options: unknown, spec: XMbSpec): VerifyData {
9176
chainIds:
9277
(options as { chains?: number[] }).chains ?? spec.assistant.chainIds,
9378
};
94-
return verifyData;
9579
}

tests/utils/deployed-url.spec.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ describe("deployed-url utilities", () => {
118118
"should return $name URL when in $name environment",
119119
({ env, expected }) => {
120120
Object.assign(process.env, env);
121-
console.log("process.env", process.env.VERCEL_ENV);
122-
console.log("process.env", env);
123-
console.log("expected", expected);
124-
console.log("getDeployedUrl", getDeployedUrl());
125121
expect(getDeployedUrl()).toBe(expected);
126122
},
127123
);

0 commit comments

Comments
 (0)