/
installer.ts
56 lines (43 loc) · 1.56 KB
/
installer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Abstract Installer class */
import { window } from 'vscode'
import { envVars } from '../utils/shell/env-vars'
import { LanguageServerAPI } from '../server/language-server'
import { CliProvider } from '../flow-cli/cli-provider'
// InstallError is thrown if install fails
export class InstallError extends Error {}
export interface InstallerContext {
refreshDependencies: () => Promise<void>
languageServerApi: LanguageServerAPI
cliProvider: CliProvider
}
export type InstallerConstructor = new (context: InstallerContext) => Installer
export abstract class Installer {
dependencies: InstallerConstructor[]
#installerName: string
constructor (name: string, dependencies: InstallerConstructor[]) {
this.dependencies = dependencies
this.#installerName = name
}
getName (): string {
return this.#installerName
}
async runInstall (): Promise<void> {
void window.showInformationMessage('Running ' + this.#installerName + ' installer, please wait...')
try {
await this.install()
} catch {
throw new InstallError('Failed to install: ' + this.#installerName)
}
// Refresh env vars
envVars.invalidate()
// Check if install was successful
if (!(await this.verifyInstall())) {
throw new InstallError('Failed to install: ' + this.#installerName)
}
void window.showInformationMessage(this.#installerName + ' installed sucessfully.')
}
// Installation logic
protected abstract install (): Promise<void> | void
// Logic to check if dependency is installed
abstract verifyInstall (): Promise<boolean>
}