-
Notifications
You must be signed in to change notification settings - Fork 2
/
postinstall.js
77 lines (72 loc) · 3.14 KB
/
postinstall.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
// Usage: npm install
const writeTo = './wwwroot/lib'
const defaultPrefix = 'https://unpkg.com'
const files = {
mjs: {
'vue.mjs': '/vue@3/dist/vue.esm-browser.js',
'vue.min.mjs': '/vue@3/dist/vue.esm-browser.prod.js',
'servicestack-client.mjs': '/@servicestack/client@2/dist/servicestack-client.mjs',
'servicestack-client.min.mjs': '/@servicestack/client@2/dist/servicestack-client.min.mjs',
'servicestack-vue.mjs': '/@servicestack/vue@3/dist/servicestack-vue.mjs',
'servicestack-vue.min.mjs': '/@servicestack/vue@3/dist/servicestack-vue.min.mjs',
},
typings: {
'vue/index.d.ts': '/vue@3/dist/vue.d.ts',
'@vue/compiler-core.d.ts': '/@vue/compiler-core@3/dist/compiler-core.d.ts',
'@vue/compiler-dom.d.ts': '/@vue/compiler-dom@3/dist/compiler-dom.d.ts',
'@vue/runtime-dom.d.ts': '/@vue/runtime-dom@3/dist/runtime-dom.d.ts',
'@vue/runtime-core.d.ts': '/@vue/runtime-core@3/dist/runtime-core.d.ts',
'@vue/reactivity.d.ts': '/@vue/reactivity@3/dist/reactivity.d.ts',
'@vue/shared.d.ts': '/@vue/shared@3/dist/shared.d.ts',
'@servicestack/client/index.d.ts': '/@servicestack/client/dist/index.d.ts',
'@servicestack/vue/index.d.ts': '/@servicestack/vue@3/dist/index.d.ts',
},
}
const path = require('path')
const fs = require('fs')
const http = require('http')
const https = require('https')
let pending = 0
Object.keys(files).forEach(dir => {
const dirFiles = files[dir]
Object.keys(dirFiles).forEach(name => {
let url = dirFiles[name]
if (url.startsWith('/'))
url = defaultPrefix + url
const toFile = path.join(writeTo, dir, name)
const toDir = path.dirname(toFile)
if (!fs.existsSync(toDir)) {
fs.mkdirSync(toDir, { recursive: true })
}
pending++
httpDownload(url, toFile, 5)
})
})
function httpDownload(url, toFile, retries) {
const client = url.startsWith('https') ? https : http
const retry = (e) => {
console.log(`get ${url} failed: ${e}${retries > 0 ? `, ${retries-1} retries remaining...` : ''}`)
if (retries > 0) httpDownload(url, toFile, retries-1)
else --pending
}
client.get(url, res => {
if (res.statusCode === 301 || res.statusCode === 302) {
let redirectTo = res.headers.location;
if (redirectTo.startsWith('/'))
redirectTo = new URL(res.headers.location, new URL(url).origin).href
return httpDownload(redirectTo, toFile, retries)
} else if (res.statusCode >= 400) {
retry(`${res.statusCode} ${res.statusText || ''}`.trimEnd())
} else {
console.log(`writing ${url} to ${toFile}`)
const file = fs.createWriteStream(toFile)
res.pipe(file);
file.on('finish', () => {
file.close()
if (--pending <= 0) {
process.exit() //node v20 not exiting on its own
}
})
}
}).on('error', retry)
}