-
Notifications
You must be signed in to change notification settings - Fork 309
/
build.ts
182 lines (164 loc) · 6.84 KB
/
build.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
import {Interfaces} from '@oclif/core'
import * as findYarnWorkspaceRoot from 'find-yarn-workspace-root'
import {log} from '../log'
import * as path from 'path'
import * as fs from 'fs-extra'
import {writeBinScripts} from './bin'
import {BuildConfig} from './config'
import {fetchNodeBinary} from './node'
import {commitAWSDir, templateShortKey} from '../upload-util'
import {prettifyPaths, hash} from '../util'
import {exec as execSync} from 'child_process'
import {promisify} from 'node:util'
const exec = promisify(execSync)
const pack = async (from: string, to: string) => {
const cwd = path.dirname(from)
await fs.promises.mkdir(path.dirname(to), {recursive: true})
log(`packing tarball from ${prettifyPaths(path.dirname(from))} to ${prettifyPaths(to)}`);
(to.endsWith('gz') ?
await exec(`tar czf ${to} ${(path.basename(from))}`, {cwd}) :
await exec(`tar cfJ ${to} ${(path.basename(from))}`, {cwd}))
}
export async function build(c: BuildConfig, options: {
platform?: string;
pack?: boolean;
tarball?: string;
parallel?: boolean;
} = {}): Promise<void> {
const {xz, config} = c
const packCLI = async () => {
const {stdout} = await exec('npm pack --unsafe-perm', {cwd: c.root})
return path.join(c.root, stdout.trim().split('\n').pop()!)
}
const extractCLI = async (tarball: string) => {
await fs.emptyDir(c.workspace())
const tarballNewLocation = path.join(c.workspace(), path.basename(tarball))
await fs.move(tarball, tarballNewLocation)
await exec(`tar -xzf "${tarballNewLocation}"`, {cwd: c.workspace()})
await Promise.all(
(await fs.promises.readdir(path.join(c.workspace(), 'package'), {withFileTypes: true}))
.map(i => fs.move(path.join(c.workspace(), 'package', i.name), path.join(c.workspace(), i.name))),
)
await Promise.all([
fs.promises.rm(path.join(c.workspace(), 'package'), {recursive: true}),
fs.promises.rm(path.join(c.workspace(), path.basename(tarball)), {recursive: true}),
fs.remove(path.join(c.workspace(), 'bin', 'run.cmd')),
])
}
const updatePJSON = async () => {
const pjsonPath = path.join(c.workspace(), 'package.json')
const pjson = await fs.readJSON(pjsonPath)
pjson.version = config.version
pjson.oclif.update = pjson.oclif.update || {}
pjson.oclif.update.s3 = pjson.oclif.update.s3 || {}
pjson.oclif.update.s3.bucket = c.s3Config.bucket
await fs.writeJSON(pjsonPath, pjson, {spaces: 2})
}
const addDependencies = async () => {
const yarnRoot = findYarnWorkspaceRoot(c.root) || c.root
if (fs.existsSync(path.join(yarnRoot, 'yarn.lock'))) {
await fs.copy(path.join(yarnRoot, 'yarn.lock'), path.join(c.workspace(), 'yarn.lock'))
await exec('yarn --no-progress --production --non-interactive', {cwd: c.workspace()})
} else {
const lockpath = fs.existsSync(path.join(c.root, 'package-lock.json')) ?
path.join(c.root, 'package-lock.json') :
path.join(c.root, 'npm-shrinkwrap.json')
await fs.copy(lockpath, path.join(c.workspace(), path.basename(lockpath)))
await exec('npm install --production', {cwd: c.workspace()})
}
}
const pretarball = async () => {
const pjson = await fs.readJSON(path.join(c.workspace(), 'package.json'))
const yarnRoot = findYarnWorkspaceRoot(c.root) || c.root
const yarn = fs.existsSync(path.join(yarnRoot, 'yarn.lock'))
if (pjson.scripts.pretarball) {
yarn ?
await exec('yarn run pretarball', {cwd: c.workspace()}) :
await exec('npm run pretarball', {cwd: c.workspace()})
}
}
const buildTarget = async (target: { platform: Interfaces.PlatformTypes; arch: Interfaces.ArchTypes}) => {
const workspace = c.workspace(target)
const gzLocalKey = templateShortKey('versioned', '.tar.gz', {
arch: target.arch,
bin: c.config.bin,
platform: target.platform,
sha: c.gitSha,
version: config.version,
})
const xzLocalKey = templateShortKey('versioned', '.tar.xz', {
arch: target.arch,
bin: c.config.bin,
platform: target.platform,
sha: c.gitSha,
version: config.version,
})
const base = path.basename(gzLocalKey)
log(`building target ${base}`)
log('copying workspace', c.workspace(), workspace)
await fs.emptyDir(workspace)
await fs.copy(c.workspace(), workspace)
await fetchNodeBinary({
nodeVersion: c.nodeVersion,
output: path.join(workspace, 'bin', 'node'),
platform: target.platform,
arch: target.arch,
tmp: path.join(config.root, 'tmp'),
})
if (options.pack === false) return
if (options.parallel) {
await Promise.all(
[pack(workspace, c.dist(gzLocalKey))]
.concat(xz ? [pack(workspace, c.dist(xzLocalKey))] : []),
)
} else {
await pack(workspace, c.dist(gzLocalKey))
if (xz) await pack(workspace, c.dist(xzLocalKey))
}
if (!c.updateConfig.s3.host) return
const rollout = (typeof c.updateConfig.autoupdate === 'object' && c.updateConfig.autoupdate.rollout)
const gzCloudKey = `${commitAWSDir(config.version, c.gitSha, c.updateConfig.s3)}/${gzLocalKey}`
const xzCloudKey = `${commitAWSDir(config.version, c.gitSha, c.updateConfig.s3)}/${xzLocalKey}`
const [sha256gz, sha256xz] = await Promise.all([hash('sha256', c.dist(gzLocalKey))].concat(xz ? [hash('sha256', c.dist(xzLocalKey))] : []))
const manifest: Interfaces.S3Manifest = {
rollout: rollout === false ? undefined : rollout,
version: config.version,
sha: c.gitSha,
baseDir: templateShortKey('baseDir', target, {bin: c.config.bin}),
gz: config.s3Url(gzCloudKey),
xz: xz ? config.s3Url(xzCloudKey) : undefined,
sha256gz,
sha256xz,
node: {
compatible: config.pjson.engines.node,
recommended: c.nodeVersion,
},
}
const manifestFilepath = c.dist(templateShortKey('manifest', {
arch: target.arch,
bin: c.config.bin,
platform: target.platform,
sha: c.gitSha,
version: config.version,
}))
await fs.writeJSON(manifestFilepath, manifest, {spaces: 2})
}
log(`gathering workspace for ${config.bin} to ${c.workspace()}`)
await extractCLI(options.tarball ? options.tarball : await packCLI())
await updatePJSON()
await addDependencies()
await writeBinScripts({config, baseWorkspace: c.workspace(), nodeVersion: c.nodeVersion})
await pretarball()
const targetsToBuild = c.targets.filter(t => !options.platform || options.platform === t.platform)
if (options.parallel) {
log(`will build ${targetsToBuild.length} targets in parallel`)
await Promise.all(targetsToBuild.map(t => buildTarget(t)))
} else {
log(`will build ${targetsToBuild.length} targets sequentially`)
for (const target of targetsToBuild) {
// eslint-disable-next-line no-await-in-loop
await buildTarget(target)
}
log(`finished building ${targetsToBuild.length} targets sequentially`)
}
}