forked from aredridel/node-bin-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·216 lines (180 loc) · 5.95 KB
/
index.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
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
#!/usr/bin/env node
"use strict"
const fs = require('fs')
const path = require('path')
const os = require('os')
const util = require('util')
const execFile = util.promisify(require('child_process').execFile)
const unlink = util.promisify(fs.unlink)
const mkdir = util.promisify(fs.mkdir)
const writeFile = util.promisify(fs.writeFile)
const readFile = util.promisify(fs.readFile)
const fetch = require('make-fetch-happen').defaults({
cacheManager: path.resolve(os.homedir(), '.node-bin-gen-cache')
})
const VError = require('verror')
const rimraf = util.promisify(require('rimraf'))
const zlib = require('zlib')
const { spawn } = require('child_process')
const yargs = require('yargs')
const pump = util.promisify(require('pump'))
const debug = require('util').debuglog('node-bin-gen')
const eos = util.promisify(require('end-of-stream'))
yargs.option('skip-binaries', { describe: 'Skip downloading the binaries', boolean: true })
yargs.option('only', { describe: 'Only download this binary package' })
yargs.option('package-name', { alias: 'n', describe: 'Use this as the main package name', default: 'node' })
yargs.version()
yargs.demandCommand(1, 2, 'You must specify version, and optionally a prerelease')
yargs.help('help').wrap(76)
const argv = yargs.argv
const versionprime = argv._[0]
const pre = argv._[1]
if (!versionprime) {
console.warn("Use: " + argv.$0 + " version [pre]")
process.exit(1)
return
}
const version = (versionprime[0] != 'v') ? 'v' + versionprime : version
async function buildArchPackage(os, cpu, version, pre) {
debug("building architecture specific package", os, cpu, version, pre)
const platform = os == 'win' ? 'win32' : os
const arch = os == 'win' && cpu == 'ia32' ? 'x86' : cpu
const executable = os == 'win' ? 'bin/node.exe' : 'bin/node'
const dir = "node-" + os + '-' + cpu
const base = "node-" + version + "-" + os + "-" + cpu
const filename = base + (os == 'win' ? '.zip' : ".tar.gz")
const pkg = {
name: 'node' + "-" + os + "-" + cpu,
version: version + (pre != null ? '-' + pre : ''),
description: 'node',
bin: {
node: executable
},
files: [
os == 'win' ? 'bin/node.exe' : 'bin/node',
'share',
'include',
'*.md',
'LICENSE'
],
os: platform,
cpu: arch
}
debug('removing', dir)
await rimraf(dir, { glob: false })
await mkdir(dir).catch(e => {
if (e.code != 'EEXIST') throw e
})
const url = "https://nodejs.org" + (
/rc/.test(version) ? "/download/rc/" :
/test/.test(version) ? "/download/test/" :
"/dist/"
) + version + "/" + filename
debug("Fetching", url)
const res = await fetch(url)
if (res.status != 200 && res.status != 304) {
throw new VError("not ok: fetching %j got status code %s", url, res.status)
}
debug("Unpacking into", dir)
if (os == 'win') {
const f = fs.createWriteStream(filename)
const written = pump(res.body, f)
const closed = eos(f)
await Promise.all([written, closed])
await execFile('unzip', ['-d', `${dir}/bin`, '-o', '-j', filename, `${base}/node.exe` ])
await unlink(filename)
} else {
const c = spawn('tar', ['--strip-components=1', '-C', dir, '-x'], {
stdio: [ 'pipe', process.stdout, process.stderr ]
})
await pump(res.body, zlib.createGunzip(), c.stdin)
}
await writeFile(path.join(dir, 'package.json'), JSON.stringify(pkg, null, 2))
return pkg
}
async function fetchManifest(version) {
const base = 'http://nodejs.org'
const url = (function () {
if (/rc/.test(version)) {
return `${base}/download/rc/index.json`
} else if (/test/.test(version)) {
return `${base}/download/test/index.json`
} else {
return `${base}/dist/index.json`
}
})()
const res = await fetch(url)
return res.json()
}
main().catch(err => {
console.warn(err.stack)
process.exit(1)
})
async function main() {
const manifest = argv['skip-binaries'] ? [] : await fetchManifest(version)
const v = manifest.filter(function(ver) {
return ver.version == version
}).shift()
if (!v) {
throw new VError("No such version '%s'", version)
}
debug("manifest", v)
if (!v.files || !v.files.length) {
debug("No files, defaulting")
v.files = ['darwin-x64', 'linux-arm64', 'linux-armv7l', 'linux-ppc64', 'linux-ppc64le', 'linux-s390x', 'linux-x64', 'linux-x86', 'sunos-x64', 'win-x64', 'win-x86']
}
const files = argv.only ? [argv.only] : v.files
const binaries = files.filter(function(f) {
return !/^headers|^src/.test(f) && !/pkg$/.test(f) && !/^win-...-(exe|msi|7z)/.test(f)
}).map(function(f) {
const bits = f.split('-')
return {
os: bits[0].replace(/^osx$/, 'darwin'),
cpu: bits[1],
format: bits[2] || 'tar.gz'
}
})
await Promise.all(binaries.map(v => buildArchPackage(v.os, v.cpu, version, pre)))
const pkg = buildMetapackage(version + (pre != null ? '-' + pre : ''))
try {
await mkdir(pkg.name)
} catch(err) {
if (err && err.code != 'EEXIST') {
throw err
}
}
const script = `require('node-bin-setup')("${pkg.version}", require)`
await Promise.all([
readFile(path.resolve(__dirname, 'node-bin-README.md'), 'utf8')
.then(readme => writeFile(path.resolve(pkg.name, 'README.md'), readme.replace(/\$\{packagename\}/g, pkg.name))),
writeFile(path.resolve(pkg.name, 'package.json'), JSON.stringify(pkg, null, 2)),
writeFile(path.resolve(pkg.name, 'installArchSpecificPackage.js'), script)
])
}
function buildMetapackage(version) {
const pkg = {
"name": argv['package-name'],
"version": version.replace(/^v/, ''),
"description": "node",
"main": "index.js",
"keywords": [
"runtime"
],
"repository": require('./package.json').repository,
"scripts": {
"preinstall": "node installArchSpecificPackage"
},
"bin": {
"node": "bin/node"
},
"dependencies": {
"node-bin-setup": "^1.0.0"
},
"license": "ISC",
"author": "",
"engines": {
"npm": ">=5.0.0"
}
}
return pkg
}