Skip to content

Commit 123102c

Browse files
⚡ optimize: replace synchronous I/O with asynchronous in app.ts (#23)
* ⚡ optimize: replace synchronous I/O with asynchronous in app.ts - Replaced `fs.readFileSync` and `fs.existsSync` with `fs.promises.readFile` in `getSelectedApp` and `selectApp`. - Replaced `fs.writeFileSync` with `fs.promises.writeFile` in `selectApp`. - Converted `getSelectedApp` to an `async` function. - Improved responsiveness by preventing event loop blocking during file I/O. - Handled `ENOENT` to maintain original "file not found" behavior efficiently. Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com> * ⚡ optimize: replace synchronous I/O with asynchronous in app.ts and fix TypeScript errors - Replaced `fs.readFileSync`, `fs.writeFileSync`, and `fs.existsSync` with `fs.promises` counterparts in `src/app.ts`. - Converted `getSelectedApp` to an `async` function. - Standardized `getSelectedApp` to always return a string `appId` and include the `platform` property. - Fixed TypeScript type mismatch errors in `src/package.ts`, `src/provider.ts`, and `src/versions.ts` resulting from the `getSelectedApp` return type change. - Added necessary `String()` conversions and type fixes for `appId` in various API calls and command handlers. - Improved application responsiveness by avoiding event loop blocking during file I/O. Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com> --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: sunnylqm <615282+sunnylqm@users.noreply.github.com>
1 parent e5f1533 commit 123102c

3 files changed

Lines changed: 39 additions & 26 deletions

File tree

src/app.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,30 @@ export function assertPlatform(platform: string): Platform {
2727
return platform as Platform;
2828
}
2929

30-
export function getSelectedApp(platform: Platform) {
30+
export async function getSelectedApp(
31+
platform: Platform,
32+
): Promise<{ appId: string; appKey: string; platform: Platform }> {
3133
assertPlatform(platform);
3234

33-
if (!fs.existsSync('update.json')) {
34-
throw new Error(t('appNotSelected', { platform }));
35+
let updateInfo: Partial<Record<Platform, { appId: number; appKey: string }>> =
36+
{};
37+
try {
38+
updateInfo = JSON.parse(await fs.promises.readFile('update.json', 'utf8'));
39+
} catch (e: any) {
40+
if (e.code === 'ENOENT') {
41+
throw new Error(t('appNotSelected', { platform }));
42+
}
43+
throw e;
3544
}
36-
const updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
37-
if (!updateInfo[platform]) {
45+
const info = updateInfo[platform];
46+
if (!info) {
3847
throw new Error(t('appNotSelected', { platform }));
3948
}
40-
return updateInfo[platform];
49+
return {
50+
appId: String(info.appId),
51+
appKey: info.appKey,
52+
platform,
53+
};
4154
}
4255

4356
export async function listApp(platform: Platform | '' = '') {
@@ -125,10 +138,10 @@ export const appCommands = {
125138
let updateInfo: Partial<
126139
Record<Platform, { appId: number; appKey: string }>
127140
> = {};
128-
if (fs.existsSync('update.json')) {
129-
try {
130-
updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
131-
} catch (e) {
141+
try {
142+
updateInfo = JSON.parse(await fs.promises.readFile('update.json', 'utf8'));
143+
} catch (e: any) {
144+
if (e.code !== 'ENOENT') {
132145
console.error(t('failedToParseUpdateJson'));
133146
throw e;
134147
}
@@ -138,7 +151,7 @@ export const appCommands = {
138151
appId: id,
139152
appKey,
140153
};
141-
fs.writeFileSync(
154+
await fs.promises.writeFile(
142155
'update.json',
143156
JSON.stringify(updateInfo, null, 4),
144157
'utf8',

src/package.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async function uploadNativePackage(
9999
const { appId: appIdInPkg, appKey: appKeyInPkg } = info;
100100
const { appId, appKey } = await getSelectedApp(config.platform);
101101

102-
if (appIdInPkg && appIdInPkg != appId) {
102+
if (appIdInPkg && String(appIdInPkg) !== appId) {
103103
throw new Error(t(config.appIdMismatchKey, { appIdInPkg, appId }));
104104
}
105105

@@ -330,7 +330,7 @@ export const packageCommands = {
330330
packages: async ({ options }: { options: { platform: Platform } }) => {
331331
const platform = await getPlatform(options.platform);
332332
const { appId } = await getSelectedApp(platform);
333-
await listPackage(appId);
333+
await listPackage(String(appId));
334334
},
335335
deletePackage: async ({
336336
args,
@@ -348,7 +348,7 @@ export const packageCommands = {
348348

349349
if (!appId) {
350350
const platform = await getPlatform(options.platform);
351-
appId = (await getSelectedApp(platform)).appId as string;
351+
appId = (await getSelectedApp(platform)).appId;
352352
}
353353

354354
// If no packageId provided as argument, let user choose from list

src/versions.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,12 @@ export const versionCommands = {
480480
versions: async ({ options }: { options: VersionCommandOptions }) => {
481481
const platform = await getPlatform(options.platform);
482482
const { appId } = await getSelectedApp(platform);
483-
await listVersions(appId);
483+
await listVersions(String(appId));
484484
},
485485
update: async ({ options }: { options: VersionCommandOptions }) => {
486486
const platform = await getPlatform(options.platform);
487487
const appId = options.appId || (await getSelectedApp(platform)).appId;
488-
let versionId = options.versionId || (await chooseVersion(appId)).id;
488+
let versionId = options.versionId || (await chooseVersion(String(appId))).id;
489489
if (versionId === 'null') {
490490
versionId = undefined;
491491
}
@@ -508,7 +508,7 @@ export const versionCommands = {
508508
}
509509
}
510510

511-
const allPkgs = await getAllPackages(appId);
511+
const allPkgs = await getAllPackages(String(appId));
512512

513513
if (!allPkgs) {
514514
throw new Error(t('noPackagesFound', { appId }));
@@ -558,7 +558,7 @@ export const versionCommands = {
558558
}
559559
} else {
560560
if (!pkgId) {
561-
pkgId = (await choosePackage(appId)).id;
561+
pkgId = (await choosePackage(String(appId))).id;
562562
}
563563

564564
if (!pkgId) {
@@ -575,15 +575,15 @@ export const versionCommands = {
575575
}
576576

577577
await printDepsChangesForPublish({
578-
appId,
579-
versionId,
578+
appId: String(appId),
579+
versionId: String(versionId),
580580
pkgs: pkgsToBind,
581581
providedVersionDeps: options.versionDeps,
582582
});
583583

584584
await bindVersionToPackages({
585-
appId,
586-
versionId,
585+
appId: String(appId),
586+
versionId: String(versionId),
587587
pkgs: pkgsToBind,
588588
rollout,
589589
dryRun: options.dryRun,
@@ -596,14 +596,14 @@ export const versionCommands = {
596596
}) => {
597597
const platform = await getPlatform(options.platform);
598598
const { appId } = await getSelectedApp(platform);
599-
const versionId = options.versionId || (await chooseVersion(appId)).id;
599+
const versionId = options.versionId || (await chooseVersion(String(appId))).id;
600600

601601
const updateParams: Record<string, string> = {};
602602
if (options.name) updateParams.name = options.name;
603603
if (options.description) updateParams.description = options.description;
604604
if (options.metaInfo) updateParams.metaInfo = options.metaInfo;
605605

606-
await put(`/app/${appId}/version/${versionId}`, updateParams);
606+
await put(`/app/${String(appId)}/version/${versionId}`, updateParams);
607607
console.log(t('operationSuccess'));
608608
},
609609
deleteVersion: async ({
@@ -619,11 +619,11 @@ export const versionCommands = {
619619

620620
let versionId = options.versionId;
621621
if (!versionId) {
622-
versionId = (await chooseVersion(appId as string)).id;
622+
versionId = (await chooseVersion(String(appId))).id;
623623
}
624624

625625
try {
626-
await doDelete(`/app/${appId}/version/${versionId}`);
626+
await doDelete(`/app/${String(appId)}/version/${versionId}`);
627627
console.log(t('deleteVersionSuccess', { versionId }));
628628
} catch (error: any) {
629629
throw new Error(

0 commit comments

Comments
 (0)