-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
67 lines (57 loc) · 1.99 KB
/
build.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
const fs = require('fs')
const path = require('path')
const { exec } = require('child_process')
const { version } = require('../package.json')
const platforms = [
{ realPlatform: 'win32/x64', goPlatform: 'windows/amd64' },
{ realPlatform: 'darwin/x64', goPlatform: 'darwin/amd64' },
{ realPlatform: 'darwin/arm64', goPlatform: 'darwin/arm64' },
{ realPlatform: 'linux/x64', goPlatform: 'linux/amd64' },
]
const npmPath = path.join(__dirname, '../npm')
platforms.forEach((platform) => {
const [GOOS, GOARCH] = platform.goPlatform.split('/')
const [realOs, realArch] = platform.realPlatform.split('/')
const childPackageName = `${realOs}-${realArch}`
const childPackageDir = path.join(npmPath, childPackageName)
const binName = realOs === 'win32' ? 'grprogress.exe' : 'grprogress'
const childPackageBinName = path.join(childPackageDir, binName)
if (!fs.existsSync(childPackageDir)) fs.mkdirSync(childPackageDir)
const templatePath = path.join(npmPath, 'package.json.template')
let templateContent = fs.readFileSync(templatePath, 'utf8')
const replaceMap = {
name: '@grprogress/' + childPackageName,
version: process.argv[2] || version,
file: binName,
os: realOs,
cpu: realArch,
}
Object.keys(replaceMap).forEach((key) => {
templateContent = templateContent.replace(new RegExp(`{{${key}}}`, 'g'), replaceMap[key])
})
const packagePath = path.join(childPackageDir, 'package.json')
fs.writeFileSync(packagePath, templateContent)
const output = childPackageBinName
const command = `go build -o ${output} main.go`
exec(
command,
{
env: {
...process.env,
GOOS,
GOARCH,
},
},
(error, stdout, stderr) => {
if (error) {
console.error(`[error]: ${platform.realPlatform} ${error.message}`)
return
}
if (stderr) {
console.error(`[stderr]: ${platform.realPlatform} ${stderr}`)
return
}
console.log(`[success]: ${platform.realPlatform} ${stdout}`)
},
)
})