Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
filip131311 committed Feb 27, 2025
1 parent 7351228 commit 229d1b4
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 128 deletions.
12 changes: 6 additions & 6 deletions packages/vscode-extension/src/builders/customBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export async function runExternalBuild(
buildCommand: string,
env: Env,
platform: DevicePlatform,
appRoot: string
cwd: string
) {
const output = await runExternalScript(buildCommand, env, appRoot, cancelToken);
const output = await runExternalScript(buildCommand, env, cwd, cancelToken);

if (!output) {
return undefined;
Expand Down Expand Up @@ -64,8 +64,8 @@ export async function runExternalBuild(
return binaryPath;
}

export async function runfingerprintCommand(externalCommand: string, env: Env, appRoot: string) {
const output = await runExternalScript(externalCommand, env, appRoot);
export async function runfingerprintCommand(externalCommand: string, env: Env, cwd: string) {
const output = await runExternalScript(externalCommand, env, cwd);
if (!output) {
return undefined;
}
Expand All @@ -75,10 +75,10 @@ export async function runfingerprintCommand(externalCommand: string, env: Env, a
async function runExternalScript(
externalCommand: string,
env: Env,
appRoot: string,
cwd: string,
cancelToken?: CancelToken
) {
let process = command(externalCommand, { cwd: appRoot, env });
let process = command(externalCommand, { cwd, env });
process = cancelToken ? cancelToken.adapt(process) : process;
Logger.info(`Running external script: ${externalCommand}`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export class DependencyManager implements Disposable, DependencyManagerInterface
this.checkEasCliInstallationStatus();
}

public async validateNodeVersion(appRoot: string) {
public async validateNodeVersion() {
const appRoot = this.appRootFolder;
const { stdout: nodeVersion } = await exec("node", ["-v"]);
const minimumNodeVersion = getMinimumSupportedNodeVersion(appRoot);
const isMinimumNodeVersion = semver.satisfies(nodeVersion, minimumNodeVersion);
Expand Down
2 changes: 1 addition & 1 deletion packages/vscode-extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ export async function activate(context: ExtensionContext) {
);

context.subscriptions.push(
workspace.onDidChangeConfiguration(async (event: ConfigurationChangeEvent) => {
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration("RadonIDE.panelLocation")) {
showIDEPanel();
}
Expand Down
151 changes: 87 additions & 64 deletions packages/vscode-extension/src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { Logger } from "../Logger";
import { DeviceInfo } from "../common/DeviceManager";
import { DeviceAlreadyUsedError, DeviceManager } from "../devices/DeviceManager";
import {
AppRootFolder,
extensionContext,
findAppRootFolder,
getCurrentLaunchConfig,
Expand Down Expand Up @@ -76,11 +75,11 @@ export class Project
public metro: Metro;
public toolsManager: ToolsManager;

public launchConfig: LaunchConfigController;
public dependencyManager: DependencyManager;
public launchConfig?: LaunchConfigController;
public dependencyManager?: DependencyManager;

private appRootFolder: AppRootFolder;
private buildCache: BuildCache;
private appRootFolder?: string;
private buildCache?: BuildCache;

private devtools = new Devtools();
private eventEmitter = new EventEmitter();
Expand All @@ -105,26 +104,7 @@ export class Project
private readonly deviceManager: DeviceManager,
private readonly utils: UtilsInterface
) {
const appRoot = findAppRootFolder();
if (!appRoot) {
window.showErrorMessage(
"Failed to determine any application root candidates, you can set it up manually in launch configuration",
"Dismiss"
);
Logger.error("[Project] The application root could not be found.");
throw Error(
"Couldn't find app root folder. The extension should not be activated without reachable app root."
);
}

Logger.info(`Found app root folder: ${appRoot}`);
migrateOldBuildCachesToNewStorage(appRoot);

this.appRootFolder = new AppRootFolder(appRoot);

this.launchConfig = new LaunchConfigController(appRoot);
this.dependencyManager = new DependencyManager(appRoot);
this.buildCache = new BuildCache(appRoot);
this.setupAppRoot();

this.deviceSettings = extensionContext.workspaceState.get(DEVICE_SETTINGS_KEY) ?? {
appearance: "dark",
Expand Down Expand Up @@ -163,14 +143,14 @@ export class Project
);

this.disposables.push(
workspace.onDidChangeConfiguration(async (event: ConfigurationChangeEvent) => {
workspace.onDidChangeConfiguration((event: ConfigurationChangeEvent) => {
if (event.affectsConfiguration("launch")) {
const config = getCurrentLaunchConfig();
const oldAppRoot = this.appRootFolder?.getAppRoot();
const oldAppRoot = this.appRootFolder;
if (config.appRoot === oldAppRoot) {
return;
}
const newAppRoot = await this.setupAppRoot();
const newAppRoot = this.setupAppRoot();

if (newAppRoot === undefined) {
window.showErrorMessage(
Expand All @@ -182,43 +162,50 @@ export class Project
}
})
);

this.disposables.push(
this.appRootFolder.onChangeAppRoot((newAppRoot: string) => {
const oldDependencyManager = this.dependencyManager;
this.dependencyManager = new DependencyManager(newAppRoot);
oldDependencyManager?.dispose();

const oldLaunchConfig = this.launchConfig;
this.launchConfig = new LaunchConfigController(newAppRoot);
oldLaunchConfig?.dispose();

this.buildCache = new BuildCache(newAppRoot);

this.reload("reboot");
})
);
}

private async setupAppRoot() {
const appRoot = findAppRootFolder();
if (!appRoot) {
return;
private setupAppRoot() {
const newAppRoot = findAppRootFolder();
if (!newAppRoot) {
window.showErrorMessage(
"Failed to determine any application root candidates, you can set it up manually in launch configuration",
"Dismiss"
);
Logger.error("[Project] The application root could not be found.");
throw Error(
"Couldn't find app root folder. The extension should not be activated without reachable app root."
);
}

this.appRootFolder.setAppRoot(appRoot);
Logger.info(`Found app root folder: ${newAppRoot}`);
migrateOldBuildCachesToNewStorage(newAppRoot);

this.appRootFolder = newAppRoot;

if (Platform.OS === "macos") {
try {
await setupPathEnv(appRoot);
setupPathEnv(newAppRoot);
} catch (error) {
window.showWarningMessage(
"Error when setting up PATH environment variable, RN IDE may not work correctly.",
"Dismiss"
);
}
}
return appRoot;

const oldDependencyManager = this.dependencyManager;
this.dependencyManager = new DependencyManager(newAppRoot);
oldDependencyManager?.dispose();

const oldLaunchConfig = this.launchConfig;
this.launchConfig = new LaunchConfigController(newAppRoot);
oldLaunchConfig?.dispose();

this.buildCache = new BuildCache(newAppRoot);

this.reload("reboot");

return newAppRoot;
}

//#region Build progress
Expand Down Expand Up @@ -480,6 +467,13 @@ export class Project
platform: this.projectState.selectedDevice?.platform,
});

if (this.dependencyManager === undefined) {
Logger.error(
"[PROJECT] Dependency manager not initialized. this code should be unreachable."
);
throw new Error("[PROJECT] Dependency manager not initialized");
}

if (await this.dependencyManager.checkProjectUsesExpoRouter()) {
await this.openNavigation(homeUrl);
} else {
Expand Down Expand Up @@ -580,6 +574,10 @@ export class Project
}

private async start(restart: boolean, resetMetroCache: boolean) {
if (this.appRootFolder === undefined) {
Logger.error("[PROJECT] App root folder not initialized. this code should be unreachable.");
throw new Error("[PROJECT] App root folder not initialized");
}
if (restart) {
const oldDevtools = this.devtools;
const oldMetro = this.metro;
Expand All @@ -604,7 +602,7 @@ export class Project
this.reportStageProgress(stageProgress, StartupMessage.WaitingForAppToLoad);
}, 100),
[waitForNodeModules],
this.appRootFolder.getAppRoot()
this.appRootFolder
);
}
//#endregion
Expand Down Expand Up @@ -729,6 +727,13 @@ export class Project
}

public async showStorybookStory(componentTitle: string, storyName: string) {
if (this.dependencyManager === undefined) {
Logger.error(
"[PROJECT] Dependency manager not initialized. this code should be unreachable."
);
throw new Error("[PROJECT] Dependency manager not initialized");
}

if (await this.dependencyManager.checkProjectUsesStorybook()) {
this.devtools.send("RNIDE_showStorybookStory", { componentTitle, storyName });
} else {
Expand Down Expand Up @@ -813,7 +818,12 @@ export class Project
}

private async ensureDependenciesAndNodeVersion() {
const appRoot = this.appRootFolder.getAppRoot();
if (this.dependencyManager === undefined) {
Logger.error(
"[PROJECT] Dependency manager not initialized. this code should be unreachable."
);
throw new Error("[PROJECT] Dependency manager not initialized");
}

const installed = await this.dependencyManager.checkNodeModulesInstallationStatus();

Expand All @@ -825,7 +835,7 @@ export class Project
Logger.debug("Node modules already installed - skipping");
}

await this.dependencyManager.validateNodeVersion(appRoot);
await this.dependencyManager.validateNodeVersion();
}

//#region Select device
Expand Down Expand Up @@ -860,6 +870,23 @@ export class Project
}

public async selectDevice(deviceInfo: DeviceInfo, forceCleanBuild = false) {
if (this.dependencyManager === undefined) {
Logger.error(
"[PROJECT] Dependency manager not initialized. this code should be unreachable."
);
throw new Error("[PROJECT] Dependency manager not initialized");
}
if (this.appRootFolder === undefined) {
Logger.error("[PROJECT] App root folder not initialized. this code should be unreachable.");
throw new Error("[PROJECT] App root folder not initialized");
}
if (this.buildCache === undefined) {
Logger.error(
"[PROJECT] Build cache folder not initialized. this code should be unreachable."
);
throw new Error("[PROJECT] Build cache not initialized");
}

const device = await this.selectDeviceOnly(deviceInfo);
if (!device) {
return false;
Expand Down Expand Up @@ -890,16 +917,12 @@ export class Project
);
this.deviceSession = newDeviceSession;

const previewURL = await newDeviceSession.start(
this.deviceSettings,
this.appRootFolder.getAppRoot(),
{
cleanBuild: forceCleanBuild,
previewReadyCallback: (url) => {
this.updateProjectStateForDevice(deviceInfo, { previewURL: url });
},
}
);
const previewURL = await newDeviceSession.start(this.deviceSettings, this.appRootFolder, {
cleanBuild: forceCleanBuild,
previewReadyCallback: (url) => {
this.updateProjectStateForDevice(deviceInfo, { previewURL: url });
},
});
this.updateProjectStateForDevice(this.projectState.selectedDevice!, {
previewURL,
status: "running",
Expand Down
Loading

0 comments on commit 229d1b4

Please sign in to comment.