Skip to content

Commit c570afa

Browse files
feat: remove file i/o from sdk and add it in cli (#31)
1 parent 73781fc commit c570afa

File tree

17 files changed

+94
-159
lines changed

17 files changed

+94
-159
lines changed

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
},
6767
"devDependencies": {
6868
"@types/form-data": "^2.5.2",
69+
"@types/js-yaml": "^4.0.9",
6970
"@types/node": "^18",
7071
"ts-node": "^10.9.2"
7172
}

packages/cli/src/commands/compute/app/deploy.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
confirm,
2020
getPrivateKeyInteractive,
2121
} from "../../../utils/prompts";
22+
import { setAppName } from "../../../utils/appNames";
2223
import chalk from "chalk";
2324

2425
export default class AppDeploy extends Command {
@@ -169,6 +170,14 @@ export default class AppDeploy extends Command {
169170
logger,
170171
);
171172

173+
// 11. Save the app name mapping locally
174+
try {
175+
await setAppName(environment, res.appId, appName);
176+
logger.info(`App saved with name: ${appName}`);
177+
} catch (err: any) {
178+
logger.warn(`Failed to save app name: ${err.message}`);
179+
}
180+
172181
this.log(
173182
`\n✅ ${chalk.green(`App deployed successfully ${chalk.bold(`(id: ${res.appId}, ip: ${res.ipAddress})`)}`)}`,
174183
);

packages/cli/src/commands/compute/environment/list.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { Command } from "@oclif/core";
2-
import {
3-
getAvailableEnvironments,
4-
getEnvironmentConfig,
5-
getDefaultEnvironment,
6-
} from "@layr-labs/ecloud-sdk";
2+
import { getAvailableEnvironments, getEnvironmentConfig } from "@layr-labs/ecloud-sdk";
3+
import { getDefaultEnvironment } from "../../../utils/globalConfig";
74
import chalk from "chalk";
85

96
/**

packages/cli/src/commands/compute/environment/set.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {
33
getEnvironmentConfig,
44
getAvailableEnvironments,
55
isEnvironmentAvailable,
6-
setDefaultEnvironment,
76
} from "@layr-labs/ecloud-sdk";
7+
import { setDefaultEnvironment } from "../../../utils/globalConfig";
88
import { getEnvironmentInteractive } from "../../../utils/prompts";
99
import { confirm } from "@inquirer/prompts";
1010

packages/cli/src/commands/compute/environment/show.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { Command } from "@oclif/core";
2-
import {
3-
getDefaultEnvironment,
4-
getEnvironmentConfig,
5-
getAvailableEnvironments,
6-
} from "@layr-labs/ecloud-sdk";
2+
import { getEnvironmentConfig, getAvailableEnvironments } from "@layr-labs/ecloud-sdk";
3+
import { getDefaultEnvironment } from "../../../utils/globalConfig";
74
import chalk from "chalk";
85

96
export default class EnvironmentShow extends Command {

packages/cli/src/flags.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { getDefaultEnvironment } from "@layr-labs/ecloud-sdk";
21
import { Flags } from "@oclif/core";
32
import { getEnvironmentInteractive, getPrivateKeyInteractive } from "./utils/prompts";
3+
import { getDefaultEnvironment } from "./utils/globalConfig";
44

55
export type CommonFlags = {
66
verbose: boolean;

packages/sdk/src/client/common/registry/appNames.ts renamed to packages/cli/src/utils/appNames.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ function saveAppRegistry(environment: string, registry: AppRegistry): void {
8686
}
8787

8888
/**
89-
* Resolve app ID or name to app ID
89+
* Resolve app ID or name to app ID (for CLI use)
9090
*/
91-
function resolveAppID(environment: string, appIDOrName: string): string | null {
91+
export function resolveAppIDFromRegistry(environment: string, appIDOrName: string): string | null {
9292
// First check if it's already a valid hex address
9393
if (/^0x[a-fA-F0-9]{40}$/.test(appIDOrName)) {
9494
return appIDOrName;
@@ -117,7 +117,7 @@ export async function setAppName(
117117
const registry = loadAppRegistry(environment);
118118

119119
// Resolve the target app ID
120-
let targetAppID: string | null = resolveAppID(environment, appIDOrName);
120+
let targetAppID: string | null = resolveAppIDFromRegistry(environment, appIDOrName);
121121
if (!targetAppID) {
122122
// If can't resolve, check if it's a valid app ID
123123
if (/^0x[a-fA-F0-9]{40}$/.test(appIDOrName)) {
@@ -187,3 +187,34 @@ export function listApps(environment: string): Record<string, string> {
187187

188188
return result;
189189
}
190+
191+
/**
192+
* Check if an app name is available in the given environment
193+
*/
194+
export function isAppNameAvailable(environment: string, name: string): boolean {
195+
const apps = listApps(environment);
196+
return !apps[name];
197+
}
198+
199+
/**
200+
* Find an available app name by appending numbers if needed
201+
*/
202+
export function findAvailableName(environment: string, baseName: string): string {
203+
const apps = listApps(environment);
204+
205+
// Check if base name is available
206+
if (!apps[baseName]) {
207+
return baseName;
208+
}
209+
210+
// Try with incrementing numbers
211+
for (let i = 2; i <= 100; i++) {
212+
const candidate = `${baseName}-${i}`;
213+
if (!apps[candidate]) {
214+
return candidate;
215+
}
216+
}
217+
218+
// Fallback to timestamp if somehow we have 100+ duplicates
219+
return `${baseName}-${Date.now()}`;
220+
}

packages/sdk/src/client/common/config/globalConfig.ts renamed to packages/cli/src/utils/globalConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import * as fs from "fs";
1414
import * as path from "path";
1515
import * as os from "os";
1616
import { load as loadYaml, dump as dumpYaml } from "js-yaml";
17-
import { getBuildType } from "./environment";
17+
import { getBuildType } from "@layr-labs/ecloud-sdk";
1818

1919
const GLOBAL_CONFIG_FILE = "config.yaml";
2020

packages/cli/src/utils/prompts.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,19 @@ import {
1515
getEnvironmentConfig,
1616
getAvailableEnvironments,
1717
isEnvironmentAvailable,
18-
listApps,
1918
getAllAppsByDeveloper,
20-
getDefaultEnvironment,
2119
getCategoryDescriptions,
2220
fetchTemplateCatalog,
2321
PRIMARY_LANGUAGES,
2422
AppProfile,
25-
} from "@layr-labs/ecloud-sdk";
26-
27-
// Re-export helper functions from SDK for use in validation
28-
import {
2923
validateAppName,
3024
validateImageReference,
3125
validateFilePath,
3226
validatePrivateKeyFormat,
3327
extractAppNameFromImage,
34-
isAppNameAvailable,
35-
findAvailableName,
3628
} from "@layr-labs/ecloud-sdk";
29+
import { getDefaultEnvironment } from "./globalConfig";
30+
import { listApps, isAppNameAvailable, findAvailableName } from "./appNames";
3731

3832
// Helper to add hex prefix
3933
function addHexPrefix(value: string): `0x${string}` {

packages/sdk/src/client/common/contract/caller.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { addHexPrefix, getChainFromID } from "../utils";
2323

2424
import { EnvironmentConfig, Logger } from "../types";
2525
import { Release } from "../types";
26-
import { getAppName } from "../registry/appNames";
2726

2827
import AppControllerABI from "../abis/AppController.json";
2928
import PermissionControllerABI from "../abis/PermissionController.json";
@@ -526,11 +525,7 @@ export async function executeUpgradeBatch(
526525
gas: { maxFeePerGas?: bigint; maxPriorityFeePerGas?: bigint } | undefined,
527526
logger: Logger,
528527
): Promise<Hex> {
529-
const appName = getAppName(prepared.environmentConfig.name, prepared.appId);
530-
let pendingMessage = "Upgrading app...";
531-
if (appName !== "") {
532-
pendingMessage = `Upgrading app '${appName}'...`;
533-
}
528+
const pendingMessage = `Upgrading app ${prepared.appId}...`;
534529

535530
const txHash = await executeBatch(
536531
{

0 commit comments

Comments
 (0)