From 320614c1649ec19b0e177b89cfb1a0c2618b244b Mon Sep 17 00:00:00 2001 From: Sam Saccone Date: Sat, 13 May 2017 14:32:44 -0700 Subject: [PATCH] Clean up supported platform launcher. This drops our need to have the magic any here, since the index type is explicitly defined, this also means that our compile will now break if a function is renamed that we were depending on :) --- chrome-launcher/chrome-launcher.ts | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/chrome-launcher/chrome-launcher.ts b/chrome-launcher/chrome-launcher.ts index bc1c4f71b7..316dcfeebe 100644 --- a/chrome-launcher/chrome-launcher.ts +++ b/chrome-launcher/chrome-launcher.ts @@ -33,6 +33,7 @@ const execSync = childProcess.execSync; const isWindows = process.platform === 'win32'; export class ChromeLauncher { +type SupportedPlatforms = 'darwin'|'linux'|'win32'; prepared = false; pollInterval: number = 500; autoSelectChrome: boolean; @@ -75,8 +76,10 @@ export class ChromeLauncher { return flags; } - prepare() { - switch (process.platform) { + private prepare() { + const platform = process.platform as SupportedPlatforms; + + switch (platform) { case 'darwin': case 'linux': this.TMP_PROFILE_DIR = unixTmpDir(); @@ -87,7 +90,7 @@ export class ChromeLauncher { break; default: - throw new Error('Platform ' + process.platform + ' is not supported'); + throw new Error(`Platform ${platform} is not supported`); } this.outFile = fs.openSync(`${this.TMP_PROFILE_DIR}/chrome-out.log`, 'a'); @@ -107,19 +110,13 @@ export class ChromeLauncher { this.prepare(); } - return Promise.resolve() - .then(() => { - const installations = (chromeFinder)[process.platform](); + const installations = await chromeFinder[process.platform as SupportedPlatforms](); + if (installations.length === 0) { + throw new Error('No Chrome Installations Found'); + } - if (installations.length < 1) { - return Promise.reject(new Error('No Chrome Installations Found')); - } else if (installations.length === 1 || this.autoSelectChrome) { - return installations[0]; - } - - return ask('Choose a Chrome installation to use with Lighthouse', installations); - }) - .then(execPath => this.spawn(execPath)); + const chromePath = installations[0]; + return await this.spawn(chromePath); } spawn(execPath: string) {