From fa4f5d17f51a1db3fed3948666889987a4b3415c Mon Sep 17 00:00:00 2001 From: EmmaYuan1015 Date: Fri, 14 Aug 2026 09:59:03 +0800 Subject: [PATCH] Refactor Expo EAS app config reading --- src/extension/exponent/exponentHelper.ts | 53 ++++++++++++++---------- 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/src/extension/exponent/exponentHelper.ts b/src/extension/exponent/exponentHelper.ts index 636ba407e..024bbc314 100644 --- a/src/extension/exponent/exponentHelper.ts +++ b/src/extension/exponent/exponentHelper.ts @@ -5,7 +5,6 @@ /// import * as path from "path"; -import * as fs from "fs"; import * as semver from "semver"; import * as vscode from "vscode"; import { sync as globSync } from "glob"; @@ -40,6 +39,18 @@ const DBL_SLASHES = /\\/g; const NGROK_PACKAGE = "@expo/ngrok"; +interface ExpoAppConfig { + expo?: { + owner?: string; + name?: string; + extra?: { + eas?: { + projectId?: string; + }; + }; + }; +} + export class ExponentHelper { private workspaceRootPath: string; private projectRootPath: string; @@ -47,6 +58,7 @@ export class ExponentHelper { private hasInitialized: boolean; private nodeModulesGlobalPathAddedToEnv: boolean; private logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); + private appConfigCache?: { mtimeMs: number; config: ExpoAppConfig }; public constructor( workspaceRootPath: string, @@ -507,34 +519,33 @@ require('${entryPoint}');`; } public async getExpoEasProjectOwner(): Promise { - const appJsonPath = this.pathToFileInWorkspace(APP_JSON); - try { - return JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.owner == undefined - ? null - : JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.owner; - } catch { - return null; - } + const config = await this.getAppJsonConfig(); + return config?.expo?.owner ?? null; } public async getExpoEasProjectId(): Promise { - const appJsonPath = this.pathToFileInWorkspace(APP_JSON); - try { - return JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.extra.eas.projectId == - undefined - ? null - : JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.extra.eas.projectId; - } catch { - return null; - } + const config = await this.getAppJsonConfig(); + return config?.expo?.extra?.eas?.projectId ?? null; } public async getExpoEasProjectName(): Promise { + const config = await this.getAppJsonConfig(); + return config?.expo?.name ?? null; + } + + private async getAppJsonConfig(): Promise { const appJsonPath = this.pathToFileInWorkspace(APP_JSON); try { - return JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.name == undefined - ? null - : JSON.parse(fs.readFileSync(appJsonPath, "utf-8")).expo.name; + const { mtimeMs } = await this.fs.stat(appJsonPath); + if (this.appConfigCache?.mtimeMs === mtimeMs) { + return this.appConfigCache.config; + } + + const config = JSON.parse( + (await this.fs.readFile(appJsonPath, "utf-8")).toString(), + ) as ExpoAppConfig; + this.appConfigCache = { mtimeMs, config }; + return config; } catch { return null; }