1
|
|
|
import execa from 'execa' |
2
|
|
|
import * as fs from 'fs' |
3
|
|
|
import {chmodSync, existsSync, unlinkSync} from 'fs' |
4
|
|
|
import Tool from './tool' |
5
|
|
|
|
6
|
|
|
abstract class CustomTool extends Tool { |
7
|
|
|
|
8
|
|
|
abstract name: string |
9
|
|
|
abstract alias: string |
10
|
|
|
|
11
|
|
|
abstract url: string |
12
|
|
|
abstract shasum: string |
13
|
|
|
|
14
|
|
|
binLocation: string = '/usr/local/bin' |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Install the binary. |
18
|
|
|
*/ |
19
|
|
|
install = async (): Promise<boolean> => { |
20
|
|
|
if (await this.isInstalled()) { |
21
|
|
|
console.log(`${this.name} already is installed. Execute it by running ${this.alias}`) |
22
|
|
|
return false |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
const fileName = this.url.substring(this.url.lastIndexOf('/') + 1) |
26
|
|
|
|
27
|
|
|
console.log(`Downloading binary for ${this.name}...`) |
28
|
|
|
|
29
|
|
|
await execa('curl', ['-OL', this.url], {cwd: `/tmp/`}) |
30
|
|
|
|
31
|
|
|
if (!(await this.isValidShasum(`/tmp/${fileName}`))) { |
32
|
|
|
console.log(`Unable to install ${this.name}. The checksum ${this.shasum} is not equal to the one of the downloaded file.`) |
33
|
|
|
await unlinkSync(`/tmp/${fileName}`) |
34
|
|
|
return false |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
await fs.copyFileSync(`/tmp/${fileName}`, `${this.binLocation}/${this.alias}`) |
38
|
|
|
await chmodSync(`${this.binLocation}/${this.alias}`, 0o777) |
39
|
|
|
|
40
|
|
|
console.log(`Successfully installed ${this.name}`) |
41
|
|
|
|
42
|
|
|
return true |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Uninstall the binary |
47
|
|
|
*/ |
48
|
|
|
uninstall = async (): Promise<boolean> => { |
49
|
|
|
if (!(await this.isInstalled())) { |
50
|
|
|
console.log(`${this.name} is not installed`) |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
console.log(`Uninstalling ${this.name}...`) |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
await unlinkSync(`${this.binLocation}/${this.alias}`) |
57
|
|
|
} catch (e) { |
58
|
|
|
throw new Error(`Unable to uninstall ${this.name}. Please remove the file manually to continue:\nrm${this.binLocation}/${this.alias}`) |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
console.log(`Uninstalled ${this.name}`) |
62
|
|
|
|
63
|
|
|
return true |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check if the binary of the app exists. |
68
|
|
|
*/ |
69
|
|
|
isInstalled = async (): Promise<boolean> => { |
70
|
|
|
return existsSync(`${this.binLocation}/${this.alias}`) |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if the file has a valid shasum. |
75
|
|
|
* |
76
|
|
|
* @param path |
77
|
|
|
*/ |
78
|
|
|
isValidShasum = async (path: string): Promise<boolean> => { |
79
|
|
|
const {stdout} = await execa('shasum', ['-a256', path]) |
80
|
|
|
const shasum = stdout.replace(path, '').trim() |
81
|
|
|
|
82
|
|
|
return shasum === this.shasum |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
export default CustomTool |