forked from kevva/bin-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
236 lines (204 loc) · 5.54 KB
/
Copy pathindex.js
File metadata and controls
236 lines (204 loc) · 5.54 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import {promises as fs} from 'node:fs';
import path from 'node:path';
import binCheck from '@xhmikosr/bin-check';
import binaryVersionCheck from 'binary-version-check';
import downloader from '@xhmikosr/downloader';
import osFilterObject from '@xhmikosr/os-filter-obj';
/**
* @typedef {Object} BinWrapperOptions
* @property {number} [strip=1] - Number of leading paths to strip from the archive.
* @property {boolean} [skipCheck=false] - Skip binary checks.
* @property {object} [decompress={}] - Extra options forwarded to @xhmikosr/decompress (e.g. `{ plugins: [...] }`). The `strip` key here is ignored; use the top-level `strip` option.
*/
/**
* @typedef {Object} SourceFile
* @property {string} url - The URL of the file.
* @property {string} [os] - The operating system the file is for.
* @property {string} [arch] - The architecture the file is for.
* @property {string} [hash] - Expected hash as `"<algorithm>:<hex>"`, verified after download.
*/
export default class BinWrapper {
/**
* @param {BinWrapperOptions} [options]
*/
constructor(options = {}) {
const {strip = 1, skipCheck = false, decompress = {}} = options;
this.options = {
strip: Math.max(0, strip),
skipCheck,
decompress: {...decompress},
};
}
/**
* Get or set files to download
*
* @param {string} [src] - The source URL of the file.
* @param {string} [os] - The operating system the file is for.
* @param {string} [arch] - The architecture the file is for.
* @param {string} [hash] - Expected hash as `"<algorithm>:<hex>"`, verified after download.
* @returns {SourceFile[]|undefined|this} - Returns the source files if no arguments are provided, otherwise returns `this`.
*/
src(src, os, arch, hash) {
if (arguments.length === 0) {
return this._src;
}
this._src ||= [];
this._src.push({
url: src,
os,
arch,
hash,
});
return this;
}
/**
* Get or set the destination
*
* @param {string} [dest] - The destination path.
* @returns {string|undefined|this} - Returns the destination if no arguments are provided, otherwise returns `this`.
*/
dest(dest) {
if (arguments.length === 0) {
return this._dest;
}
this._dest = dest;
return this;
}
/**
* Get or set the binary
*
* @param {string} [bin] - The binary name.
* @returns {string|undefined|this} - Returns the binary name if no arguments are provided, otherwise returns `this`.
*/
use(bin) {
if (arguments.length === 0) {
return this._use;
}
this._use = bin;
return this;
}
/**
* Get or set a semver range to test the binary against
*
* @param {string} [range] - The semver range.
* @returns {string|undefined|this} - Returns the semver range if no arguments are provided, otherwise returns `this`.
*/
version(range) {
if (arguments.length === 0) {
return this._version;
}
this._version = range;
return this;
}
/**
* Get path to the binary
*
* @returns {string} - The full path to the binary.
*/
path() {
return path.join(this.dest(), this.use());
}
/**
* Filter the configured sources down to the ones matching the current OS and arch
*
* @returns {SourceFile[]}
*/
#resolveSources() {
return osFilterObject(this.src() || []);
}
/**
* Get the source URLs matching the current OS and arch
*
* @returns {string[]}
*/
resolvedUrls() {
return this.#resolveSources().map(file => file.url);
}
/**
* Check for the binary and download it if missing, then optionally verify it works.
*
* @param {string[]} [cmd=['--version']] - Arguments passed to the binary when checking it.
* @returns {Promise<void>}
*/
async run(cmd = ['--version']) {
await this.findExisting();
if (this.options.skipCheck) {
return;
}
await this.runCheck(cmd);
}
/**
* Run binary check
*
* @param {string[]} cmd - Arguments to pass to the binary.
* @returns {Promise<void>}
* @api private
*/
async runCheck(cmd) {
const works = await binCheck(this.path(), cmd);
if (!works) {
throw new Error(`The "${this.path()}" binary doesn't seem to work correctly`);
}
if (this.version()) {
await binaryVersionCheck(this.path(), this.version());
}
}
/**
* Check whether the binary exists; download it if not.
*
* @returns {Promise<void>}
* @api private
*/
async findExisting() {
try {
await fs.access(this.path());
} catch (error) {
if (error?.code === 'ENOENT') {
await this.download();
} else {
throw error;
}
}
}
/**
* Download files matching the current OS/arch and make them executable.
*
* @returns {Promise<void>}
* @api private
*/
async download() {
const sources = this.#resolveSources();
if (sources.length === 0) {
throw new Error('No binary found matching your system. It\'s probably not supported.');
}
const results = await Promise.all(sources.map(source =>
downloader(source.url, this.dest(), {
extract: true,
hash: source.hash,
decompress: {
...this.options.decompress,
strip: this.options.strip,
},
})));
const resultFiles = results.flatMap((item, index) => {
if (Array.isArray(item)) {
return item.map(file => file.path);
}
const parsedUrl = new URL(sources[index].url);
return path.parse(parsedUrl.pathname).base;
});
await Promise.all(resultFiles
.filter(Boolean)
.map(async file => {
try {
await fs.chmod(path.join(this.dest(), file), 0o755);
} catch (error) {
// We guess the saved name from the URL, but the downloader may
// have used a different one, so skip a missing file.
if (error?.code !== 'ENOENT') {
throw error;
}
}
}));
}
}