Skip to content

Commit 6b02253

Browse files
committed
refactor(target): add operation-scoped app resolver
1 parent 8a1f821 commit 6b02253

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

src/app.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ interface AppSummary {
1111
platform: Platform;
1212
}
1313

14+
export interface AppTargetOptions {
15+
appId?: string;
16+
config?: string;
17+
}
18+
19+
export interface ResolvedAppTarget {
20+
appId: string;
21+
appKey?: string;
22+
platform: Platform;
23+
configPath: string;
24+
}
25+
1426
export async function getPlatform(platform?: string) {
1527
return assertPlatform(
1628
platform || (await question(t('platformQuestion'))),
@@ -54,6 +66,44 @@ export async function getSelectedApp(
5466
};
5567
}
5668

69+
/** Resolve an explicit or selected app into a stable operation target. */
70+
export async function resolveAppTarget(
71+
platform: Platform,
72+
options: AppTargetOptions = {},
73+
): Promise<ResolvedAppTarget> {
74+
const configPath = options.config || updateJson;
75+
if (options.appId) {
76+
return {
77+
appId: String(options.appId),
78+
platform,
79+
configPath,
80+
};
81+
}
82+
83+
return {
84+
...(await getSelectedApp(platform, configPath)),
85+
configPath,
86+
};
87+
}
88+
89+
/** Cache app selection for one operation and retry after a failed lookup. */
90+
export function createAppTargetResolver(
91+
platform: Platform,
92+
options: AppTargetOptions = {},
93+
): () => Promise<ResolvedAppTarget> {
94+
let pending: Promise<ResolvedAppTarget> | undefined;
95+
96+
return () => {
97+
if (!pending) {
98+
pending = resolveAppTarget(platform, options).catch((error) => {
99+
pending = undefined;
100+
throw error;
101+
});
102+
}
103+
return pending;
104+
};
105+
}
106+
57107
export async function listApp(platform: Platform | '' = '') {
58108
const { data } = await get('/app/list');
59109
const allApps = data as AppSummary[];

0 commit comments

Comments
 (0)