Skip to content

Commit 233ea19

Browse files
⚡ 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>
1 parent d48a755 commit 233ea19

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/app.ts

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

30-
export function getSelectedApp(platform: Platform) {
30+
export async function getSelectedApp(platform: Platform) {
3131
assertPlatform(platform);
3232

33-
if (!fs.existsSync('update.json')) {
34-
throw new Error(t('appNotSelected', { platform }));
33+
let updateInfo: Partial<Record<Platform, { appId: number; appKey: string }>> =
34+
{};
35+
try {
36+
updateInfo = JSON.parse(await fs.promises.readFile('update.json', 'utf8'));
37+
} catch (e: any) {
38+
if (e.code === 'ENOENT') {
39+
throw new Error(t('appNotSelected', { platform }));
40+
}
41+
throw e;
3542
}
36-
const updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
3743
if (!updateInfo[platform]) {
3844
throw new Error(t('appNotSelected', { platform }));
3945
}
@@ -125,10 +131,10 @@ export const appCommands = {
125131
let updateInfo: Partial<
126132
Record<Platform, { appId: number; appKey: string }>
127133
> = {};
128-
if (fs.existsSync('update.json')) {
129-
try {
130-
updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
131-
} catch (e) {
134+
try {
135+
updateInfo = JSON.parse(await fs.promises.readFile('update.json', 'utf8'));
136+
} catch (e: any) {
137+
if (e.code !== 'ENOENT') {
132138
console.error(t('failedToParseUpdateJson'));
133139
throw e;
134140
}
@@ -138,7 +144,7 @@ export const appCommands = {
138144
appId: id,
139145
appKey,
140146
};
141-
fs.writeFileSync(
147+
await fs.promises.writeFile(
142148
'update.json',
143149
JSON.stringify(updateInfo, null, 4),
144150
'utf8',

0 commit comments

Comments
 (0)