-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
executable file
·208 lines (181 loc) · 5.54 KB
/
index.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
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
#!/usr/bin/env bun
import fs from 'fs'
import path from 'path'
import os from 'os'
import semver from 'semver'
import https from 'https'
import ora from 'ora'
import minimist from 'minimist'
import { mkdirp } from 'mkdirp'
import { exec, execSync } from 'child_process'
import { readPackage } from 'read-pkg'
interface ArgvType {
help: boolean
platform: string
arch: string
version: string
mirror_base_url: string
d: boolean
_: string[]
}
const argv = minimist(process.argv.slice(2), {
alias: {
h: 'help',
p: 'platform',
a: 'arch',
v: 'version',
m: 'mirror_base_url'
},
boolean: ['help']
}) as ArgvType
const MIRROR_URL = 'https://cdn.npmmirror.com/binaries/electron/'
function getPackageVersion(packageName: string): Promise<string[]> {
return new Promise((resolve, reject) => {
const command = `npm show ${packageName} versions`
exec(command, (error, stdout, stderr) => {
if (error || stderr) {
reject(error)
return
}
const str = stdout.trim()
let arr: string[]
eval(`arr = ${str}`)
// @ts-ignore
resolve(arr)
})
})
}
const getConfig = () => {
const platform = process.env.npm_config_platform || process.platform
let arch = argv.arch || process.env.npm_config_arch || process.arch
if (
!argv.arch &&
platform === 'darwin' &&
process.platform === 'darwin' &&
arch === 'x64' &&
process.env.npm_config_arch === undefined
) {
// When downloading for macOS ON macOS and we think we need x64 we should
// check if we're running under rosetta and download the arm64 version if appropriate
try {
const output = execSync('sysctl -in sysctl.proc_translated')
if (output.toString().trim() === '1') {
arch = 'arm64'
}
} catch {
// Ignore failure
}
}
return {
platform: argv.platform || platform,
currentPlatform: platform,
arch
}
}
const getArtifactRemoteUrl = (config: { platform: string; arch: string; version: string }) => {
const { platform, arch, version } = config
const url = path.join(
argv.mirror_base_url || MIRROR_URL,
version,
`electron-v${version}-${platform}-${arch}.zip`
)
return url
}
const getTargetPath = (platform: string) => {
let result: string
if (platform === 'darwin') {
result = path.join(os.homedir(), 'Library/Caches/electron/')
} else if (platform === 'win32') {
result = path.join(os.homedir(), 'AppData/Local/electron/Cache/')
} else if (platform === 'linux') {
result = path.join(os.homedir(), '.cache/electron/')
} else {
console.error(`error: Unsupported platform: ${platform}`)
process.exit(1)
}
if (!fs.existsSync(result)) {
try {
mkdirp.sync(result)
} catch (error) {
console.info(error)
process.exit(1)
}
}
return result
}
const downloadFile = async (url: string, outputPath: string): Promise<void> => {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(outputPath)
https
.get(url, (response) => {
if (response.statusCode !== 200) {
fs.unlink(outputPath, () =>
reject(new Error(`Failed to get '${url}' (${response.statusCode})`))
)
return
}
response.pipe(file)
file.on('finish', () => {
file.close(() => {
resolve()
})
})
file.on('error', (err) => {
fs.unlink(outputPath, () => reject(err))
})
})
.on('error', (err) => {
fs.unlink(outputPath, () => reject(err))
})
})
}
const helpMsg = (v: string, destPath: string) => `\
Usage: [fk-electron|electron-nb] [OPTION]...
Download Electron binary files from npmmirror to ${destPath}
Options:
-h, --help display help message and exit
-v, --version specify version (default: ${v})
-p, --platform [win32|darwin|linux] specify platform (default: ${process.platform})
-a, --arch [arm64|x64|ia32] specify architecture (default: ${process.arch})
-m, --mirror_base_url specify mirror base url (default: ${MIRROR_URL})
`
const run = async () => {
const config = getConfig()
const targetDir = getTargetPath(config.currentPlatform)
const pkg = await readPackage()
const name = 'electron'
const versionRange = argv?.d
? '33.1.0'
: pkg?.dependencies?.[name] || pkg?.devDependencies?.[name] || pkg.peerDependencies?.[name]
if (argv.help) {
console.info(helpMsg(versionRange || '-', targetDir))
return
}
if (!argv.version && !versionRange) {
console.error(`error: No version specified for specifier "${name}"`)
process.exit(1)
}
const versions = await getPackageVersion(name)
if (!versions) {
console.error(`error: Get ${name} versions failed.`)
process.exit(1)
}
const actualVersionRange = argv.version || versionRange
const installedVersion = semver.maxSatisfying(versions, actualVersionRange!)
if (!installedVersion) {
console.info(
`error No version matching "${actualVersionRange}" found for specifier "${name}" (but package exists)`
)
process.exit(1)
}
const url = getArtifactRemoteUrl({ ...config, version: installedVersion })
const targetFilePath = path.join(targetDir, path.basename(url))
if (fs.existsSync(targetFilePath)) {
console.warn(`warning: ${path.basename(url)} already exists.`)
process.exit(0)
}
const spinner = ora().start(`Downloading ${path.basename(url)} from ${url}`)
await downloadFile(url, targetFilePath)
spinner.succeed('Download complete')
}
run()