-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcross
71 lines (70 loc) · 2.96 KB
/
cross
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
#!/usr/bin/env node
const {Application} = require('lakutata')
const {Docker} = require('lakutata/com/docker')
const {program} = require('commander')
const {Glob, MD5} = require('lakutata/helper')
const {mkdir} = require('node:fs/promises')
const {existsSync} = require('node:fs')
const {homedir} = require('os')
const path = require('path')
const {createInterface} = require('node:readline')
Application.run(() => ({
id: 'cross.builder.tool',
name: 'Cross Builder Tool',
components: {
docker: {
class: Docker
}
},
bootstrap: [async (app) => {
program
.option('-p, --platform <platform>', 'target platform')
.action(async (options) => {
/**
* @type {Docker}
*/
const docker = await app.getObject('docker')
const files = await Glob('**/*')
if (existsSync(path.resolve(__dirname, '.npmrc'))) files.push('.npmrc')
try {
const image = await docker.buildImage({
files: files,
workdir: __dirname,
dockerfile: 'Dockerfile',
platform: options.platform,
outputCallback: output => output.stream && output.stream.trim() ? console.log(output.stream.trim()) : undefined
})
const distPath = path.resolve(__dirname, 'dist')
const cachePath = path.resolve(homedir(), `.nexe-lakutata-cross/${MD5(options.platform).toString('hex')}`)
await mkdir(distPath, {recursive: true})
await mkdir(cachePath, {recursive: true})
const container = await image.run({
binds: [
{
containerPath: '/data',
hostPath: distPath,
rw: true
},
{
containerPath: '/root/.nexe',
hostPath: cachePath,
rw: true
}
]
})
const logStream = await container.logs({follow: true})
await new Promise((resolve, reject) => {
createInterface({input: logStream}).on('line', line => console.log(line.toString().trim()))
logStream
.once('close', resolve)
.once('error', reject)
})
await container.remove({force: true})
await image.remove({force: true})
} catch (e) {
await docker.prune({images: true, containers: true})
console.error('error:', e.message)
}
}).parse()
}]
}))