Skip to content

Commit 92eedfa

Browse files
committed
fix: Remove outerloops
1 parent 8960843 commit 92eedfa

File tree

1 file changed

+11
-41
lines changed

1 file changed

+11
-41
lines changed

lib/commands/app.ts

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -174,52 +174,22 @@ export async function changeRootElement(this: NovaWindowsDriver, pathOrNativeWin
174174
if (path.includes('!') && path.includes('_') && !(path.includes('/') || path.includes('\\'))) {
175175
this.log.debug('Detected app path to be in the UWP format.');
176176
await this.sendPowerShellCommand(/* ps1 */ `Start-Process 'explorer.exe' 'shell:AppsFolder\\${path}'${this.caps.appArguments ? ` -ArgumentList '${this.caps.appArguments}'` : ''}`);
177-
await sleep((this.caps['ms:waitForAppLaunch'] ?? 0) * 1000 || SLEEP_INTERVAL_MS);
178-
for (let i = 1; i <= 20; i++) {
179-
const result = await this.sendPowerShellCommand(/* ps1 */ `(Get-Process -Name 'ApplicationFrameHost').Id`);
180-
const processIds = result.split('\n').map((pid) => pid.trim()).filter(Boolean).map(Number);
181-
182-
this.log.debug(`Process IDs of ApplicationFrameHost processes (${processIds.length}): ` + processIds.join(', '));
183-
try {
184-
await this.attachToApplicationWindow(processIds);
185-
return;
186-
} catch (err) {
187-
if (err instanceof Error) {
188-
this.log.debug(`attachToApplicationWindow failed: ${err.message}`);
189-
}
190-
}
191-
192-
this.log.info(`Failed to locate window of the app. Sleeping for ${SLEEP_INTERVAL_MS} milliseconds and retrying... (${i}/20)`); // TODO: make a setting for the number of retries or timeout
193-
await sleep(SLEEP_INTERVAL_MS); // TODO: make a setting for the sleep timeout
194-
}
177+
const result = await this.sendPowerShellCommand(/* ps1 */ `(Get-Process -Name 'ApplicationFrameHost').Id`);
178+
const processIds = result.split('\n').map((pid) => pid.trim()).filter(Boolean).map(Number);
179+
this.log.debug(`Process IDs of ApplicationFrameHost processes (${processIds.length}): ` + processIds.join(', '));
180+
await this.attachToApplicationWindow(processIds);
195181
} else {
196182
this.log.debug('Detected app path to be in the classic format.');
197183
const normalizedPath = normalize(path);
198184
await this.sendPowerShellCommand(/* ps1 */ `Start-Process '${normalizedPath}'${this.caps.appArguments ? ` -ArgumentList '${this.caps.appArguments}'` : ''}`);
199-
await sleep((this.caps['ms:waitForAppLaunch'] ?? 0) * 1000 || 500);
200-
for (let i = 1; i <= 20; i++) {
201-
try {
202-
const breadcrumbs = normalizedPath.toLowerCase().split('\\').flatMap((x) => x.split('/'));
203-
const executable = breadcrumbs[breadcrumbs.length - 1];
204-
const processName = executable.endsWith('.exe') ? executable.slice(0, executable.length - 4) : executable;
205-
const result = await this.sendPowerShellCommand(/* ps1 */ `(Get-Process -Name '${processName}' | Sort-Object StartTime -Descending).Id`);
206-
const processIds = result.split('\n').map((pid) => pid.trim()).filter(Boolean).map(Number);
207-
this.log.debug(`Process IDs of '${processName}' processes: ` + processIds.join(', '));
208-
209-
await this.attachToApplicationWindow(processIds);
210-
return;
211-
} catch (err) {
212-
if (err instanceof Error) {
213-
this.log.debug(`Received error:\n${err.message}`);
214-
}
215-
}
216-
217-
this.log.info(`Failed to locate window of the app. Sleeping for ${SLEEP_INTERVAL_MS} milliseconds and retrying... (${i}/20)`); // TODO: make a setting for the number of retries or timeout
218-
await sleep(SLEEP_INTERVAL_MS); // TODO: make a setting for the sleep timeout
219-
}
185+
const breadcrumbs = normalizedPath.toLowerCase().split('\\').flatMap((x) => x.split('/'));
186+
const executable = breadcrumbs[breadcrumbs.length - 1];
187+
const processName = executable.endsWith('.exe') ? executable.slice(0, executable.length - 4) : executable;
188+
const result = await this.sendPowerShellCommand(/* ps1 */ `(Get-Process -Name '${processName}' | Sort-Object StartTime -Descending).Id`);
189+
const processIds = result.split('\n').map((pid) => pid.trim()).filter(Boolean).map(Number);
190+
this.log.debug(`Process IDs of '${processName}' processes: ` + processIds.join(', '));
191+
await this.attachToApplicationWindow(processIds);
220192
}
221-
222-
throw new errors.UnknownError('Failed to locate window of the app.');
223193
}
224194

225195
export async function back(this: NovaWindowsDriver): Promise<void> {

0 commit comments

Comments
 (0)