-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
163 lines (129 loc) · 4.07 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
'use strict'
const packlist = require('npm-packlist')
const tar = require('tar-fs')
const dockerPull = require('@vweevers/docker-pull')
const dockerRun = require('docker-run')
const logger = require('log-update')
const bytes = require('pretty-bytes')
const browserify = require('browserify')
const unixify = require('unixify')
const once = require('once')
const path = require('path')
module.exports = function (opts, callback) {
if (typeof opts === 'function') {
callback = opts
opts = null
}
opts = opts || {}
callback = once(callback)
const images = [].concat(opts.image || [])
const cwd = path.resolve(opts.cwd || '.')
const modules = opts.modules ? path.resolve(cwd, opts.modules) : null
const files = JSON.stringify(packageFiles(cwd))
const prebuilds = path.join(cwd, 'prebuilds')
const log = logger.create(process.stderr, { showCursor: true })
loop()
function loop () {
let image = images.shift()
if (!image) return process.nextTick(callback)
// Default to images from https://github.com/prebuild/docker-images
if (!image.includes('/')) {
image = 'ghcr.io/prebuild/' + image
// Pin to latest major version by default
if (!image.includes(':')) {
image = image + ':2'
}
}
dockerPull(image)
.on('progress', progress)
.on('error', callback)
.on('end', end)
function progress () {
if (process.env.CI) {
console.error(`> prebuildify-cross pull ${this.image}`)
return this.removeListener('progress', progress)
}
const count = `${this.layers} layers`
const ratio = `${bytes(this.transferred)} / ${bytes(this.length)}`
log(`> prebuildify-cross pull ${this.image}: ${count}, ${ratio}`)
}
function end () {
run(this.image)
}
}
function run (image) {
const argv = prebuildifyArgv(opts.argv || [], image)
console.error('> prebuildify-cross run %s', image)
console.error('> prebuildify %s\n', argv.join(' '))
const volumes = {
// Should but can't use :ro (mafintosh/docker-run#12)
[cygwin(cwd)]: '/input'
}
if (modules) {
volumes[modules] = '/input/node_modules'
}
const child = dockerRun(image, {
entrypoint: 'node',
argv: ['-'].concat(argv),
volumes,
env: {
PREBUILDIFY_CROSS_FILES: files,
// Disable npm update check
NO_UPDATE_NOTIFIER: 'true'
}
})
child
.on('error', callback)
.on('exit', onexit)
guestScript()
.pipe(child.stdin)
child.stderr
.pipe(process.stderr, { end: false })
child.stdout
.pipe(tar.extract(prebuilds), { dmode: 0o755, fmode: 0o644 })
.on('finish', loop)
.on('error', callback)
}
function onexit (code) {
if (code) return callback(new Error('Exited with code ' + code))
}
}
function packageFiles (path) {
return packlist.sync({ path }).filter(function (fp) {
return !/^prebuilds[/\\]/i.test(fp)
})
}
function guestScript () {
return browserify(require.resolve('./guest.js'), {
basedir: __dirname,
node: true
}).bundle()
}
function prebuildifyArgv (argv, image) {
argv = argv.slice()
for (let i = 0; i < argv.length - 1; i++) {
if (/^(-i|--image)$/.test(argv[i]) && argv[i + 1][0] !== '-') {
argv.splice(i--, 2)
}
if (/^(--cwd)$/.test(argv[i]) && argv[i + 1][0] !== '-') {
argv.splice(i--, 2)
}
if (/^(--modules)$/.test(argv[i]) && argv[i + 1][0] !== '-') {
argv.splice(i--, 2)
}
}
// TODO: move this to the docker images (https://github.com/prebuild/docker-images/issues/11)
if (/^(ghcr\.io\/)?prebuild\/(linux|android)-arm/.test(image)) argv.push('--tag-armv')
if (/^(ghcr\.io\/)?prebuild\/(centos|alpine)/.test(image)) argv.push('--tag-libc')
return argv
}
function cygwin (fp) {
if (process.platform !== 'win32') return fp
if (!truthy(process.env.COMPOSE_CONVERT_WINDOWS_PATHS)) return fp
const unix = unixify(fp)
const drive = fp.match(/^([A-Z]):/i)
return drive ? '/' + drive[1].toLowerCase() + unix : unix
}
function truthy (str) {
return str === 'true' || str === '1'
}