-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathImageminBinary.ts
100 lines (97 loc) · 3.31 KB
/
ImageminBinary.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
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
import { SingletonProto } from '@eggjs/tegg';
import binaries, { BinaryName } from '../../../../config/binaries.js';
import { BinaryType } from '../../enum/Binary.js';
import { AbstractBinary, FetchResult, BinaryItem, BinaryAdapter } from './AbstractBinary.js';
@SingletonProto()
@BinaryAdapter(BinaryType.Imagemin)
export class ImageminBinary extends AbstractBinary {
async initFetch() {
// do nothing
return;
}
async fetch(dir: string, binaryName: BinaryName): Promise<FetchResult | undefined> {
const binaryConfig = binaries[binaryName];
const dirItems: {
[key: string]: BinaryItem[];
} = {};
const npmPackageName = binaryConfig.options?.npmPackageName ?? binaryName;
const pkgUrl = `https://registry.npmjs.com/${npmPackageName}`;
const data = await this.requestJSON(pkgUrl);
dirItems['/'] = [];
// mini version 4.0.0
// https://github.com/imagemin/jpegtran-bin/blob/v4.0.0/lib/index.js
// https://github.com/imagemin/pngquant-bin/blob/v4.0.0/lib/index.js
for (const version in data.versions) {
const major = parseInt(version.split('.', 1)[0]);
if (major < 4) continue;
// >= 4.0.0
const date = data.time[version];
// https://raw.githubusercontent.com/imagemin/jpegtran-bin/v${pkg.version}/vendor/`
dirItems['/'].push({
name: `v${version}/`,
date,
size: '-',
isDir: true,
url: '',
});
const versionDir = `/v${version}/`;
dirItems[versionDir] = [];
dirItems[versionDir].push({
name: 'vendor/',
date,
size: '-',
isDir: true,
url: '',
});
const versionVendorDir = `/v${version}/vendor/`;
dirItems[versionVendorDir] = [];
for (const platform of binaryConfig.options!.nodePlatforms!) {
dirItems[versionVendorDir].push({
name: `${platform}/`,
date,
size: '-',
isDir: true,
url: '',
});
const platformDir = `/v${version}/vendor/${platform}/`;
dirItems[platformDir] = [];
const archs = binaryConfig.options!.nodeArchs![platform];
if (archs.length === 0) {
for (const name of binaryConfig.options!.binFiles![platform]) {
dirItems[platformDir].push({
name,
date,
size: '-',
isDir: false,
url: `${binaryConfig.distUrl}/${binaryConfig.repo}${platformDir}${name}`,
ignoreDownloadStatuses: [ 404 ],
});
}
} else {
for (const arch of archs) {
dirItems[platformDir].push({
name: `${arch}/`,
date,
size: '-',
isDir: true,
url: '',
});
const platformArchDir = `/v${version}/vendor/${platform}/${arch}/`;
dirItems[platformArchDir] = [];
for (const name of binaryConfig.options!.binFiles![platform]) {
dirItems[platformArchDir].push({
name,
date,
size: '-',
isDir: false,
url: `${binaryConfig.distUrl}/${binaryConfig.repo}${platformArchDir}${name}`,
ignoreDownloadStatuses: [ 404 ],
});
}
}
}
}
}
return { items: dirItems[dir] };
}
}