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 :)
This commit is contained in:
Sam Saccone 2017-05-13 14:32:44 -07:00
Родитель 8a6c1aa8da
Коммит 320614c164
1 изменённых файлов: 12 добавлений и 15 удалений

Просмотреть файл

@ -33,6 +33,7 @@ const execSync = childProcess.execSync;
const isWindows = process.platform === 'win32'; const isWindows = process.platform === 'win32';
export class ChromeLauncher { export class ChromeLauncher {
type SupportedPlatforms = 'darwin'|'linux'|'win32';
prepared = false; prepared = false;
pollInterval: number = 500; pollInterval: number = 500;
autoSelectChrome: boolean; autoSelectChrome: boolean;
@ -75,8 +76,10 @@ export class ChromeLauncher {
return flags; return flags;
} }
prepare() { private prepare() {
switch (process.platform) { const platform = process.platform as SupportedPlatforms;
switch (platform) {
case 'darwin': case 'darwin':
case 'linux': case 'linux':
this.TMP_PROFILE_DIR = unixTmpDir(); this.TMP_PROFILE_DIR = unixTmpDir();
@ -87,7 +90,7 @@ export class ChromeLauncher {
break; break;
default: 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'); this.outFile = fs.openSync(`${this.TMP_PROFILE_DIR}/chrome-out.log`, 'a');
@ -107,19 +110,13 @@ export class ChromeLauncher {
this.prepare(); this.prepare();
} }
return Promise.resolve() const installations = await chromeFinder[process.platform as SupportedPlatforms]();
.then(() => { if (installations.length === 0) {
const installations = (<any>chromeFinder)[process.platform](); throw new Error('No Chrome Installations Found');
}
if (installations.length < 1) { const chromePath = installations[0];
return Promise.reject(new Error('No Chrome Installations Found')); return await this.spawn(chromePath);
} 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));
} }
spawn(execPath: string) { spawn(execPath: string) {