This repository has been archived by the owner on Aug 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 334
/
Copy pathbinary-install.js
146 lines (125 loc) · 4.26 KB
/
binary-install.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const { existsSync, mkdirSync } = require("fs");
const { homedir } = require("os");
const { join } = require("path");
const { spawnSync } = require("child_process");
const axios = require("axios");
const tar = require("tar");
const rimraf = require("rimraf");
const error = msg => {
console.error(msg);
process.exit(1);
};
class Binary {
constructor(url, data) {
if (typeof url !== "string") {
errors.push("url must be a string");
} else {
try {
new URL(url);
} catch (e) {
errors.push(e);
}
}
let errors = [];
if (data.name && typeof data.name !== "string") {
errors.push("name must be a string");
}
if (data.installDirectory && typeof data.installDirectory !== "string") {
errors.push("installDirectory must be a string");
}
if (!data.installDirectory && !data.name) {
errors.push("You must specify either name or installDirectory");
}
if (errors.length > 0) {
let errorMsg = "Your Binary constructor is invalid:";
errors.forEach(error => {
errorMsg += error;
});
error(errorMsg);
}
this.url = url;
this.name = data.name || -1;
this.installDirectory = data.installDirectory || join(__dirname, "bin");
this.binaryDirectory = -1;
this.binaryPath = -1;
}
_getInstallDirectory() {
if (!existsSync(this.installDirectory)) {
mkdirSync(this.installDirectory, { recursive: true });
}
return this.installDirectory;
}
_getBinaryDirectory() {
const installDirectory = this._getInstallDirectory();
const binaryDirectory = join(this.installDirectory, "bin");
if (existsSync(binaryDirectory)) {
this.binaryDirectory = binaryDirectory;
} else {
error(`You have not installed ${this.name ? this.name : "this package"}`);
}
return this.binaryDirectory;
}
_getBinaryPath() {
if (this.binaryPath === -1) {
const binaryDirectory = this._getBinaryDirectory();
this.binaryPath = join(binaryDirectory, this.name);
}
return this.binaryPath;
}
install() {
const dir = this._getInstallDirectory();
if (!existsSync(dir)) {
mkdirSync(dir, { recursive: true });
}
this.binaryDirectory = join(dir, "bin");
if (existsSync(this.binaryDirectory)) {
rimraf.sync(this.binaryDirectory);
}
mkdirSync(this.binaryDirectory, { recursive: true });
console.log(`Downloading release from ${this.url}`);
return axios({ url: this.url, responseType: "stream" })
.then(res => {
const writer = tar.x({ strip: 1, C: this.binaryDirectory });
return new Promise((resolve, reject) => {
res.data.pipe(writer);
let error = null;
writer.on('error', err => {
error = err;
reject(err);
});
writer.on('close', () => {
if (!error) {
resolve(true);
}
});
})
})
.then(() => {
console.log(
`${this.name ? this.name : "Your package"} has been installed!`
);
})
.catch(e => {
error(`Error fetching release: ${e.message}`);
});
}
uninstall() {
if (existsSync(this._getInstallDirectory())) {
rimraf.sync(this.installDirectory);
console.log(
`${this.name ? this.name : "Your package"} has been uninstalled`
);
}
}
run() {
const binaryPath = this._getBinaryPath();
const [, , ...args] = process.argv;
const options = { cwd: process.cwd(), stdio: "inherit" };
const result = spawnSync(binaryPath, args, options);
if (result.error) {
error(result.error);
}
process.exit(result.status);
}
}
module.exports.Binary = Binary;