-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbuild.js
91 lines (87 loc) · 3 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
const fs = require('fs')
const path = require('path')
const { execSync } = require('child_process')
// 源文件路径(根据你的项目结构调整)
let isDebug = true
let releaseName
const argv = process.argv.slice(2)
if (argv && argv[0] === '--debug') isDebug = true
const sourcePath = path.join(__dirname, 'package/')
// 目标路径
const nodeSqlite3targetPath = path.join(__dirname, 'node_modules/sqlite3/build/Release/')
const bcryptTargetPath = path.join(__dirname, 'node_modules/bcrypt/lib/binding/napi-v3')
const moveNodeSqlite = (targetPlatform, packageName = 'node_sqlite3', targetPath = nodeSqlite3targetPath) => {
// 根据目标平台选择正确的文件
let targetFile
const name = targetPlatform.split('-').slice(1).join('-')
switch (name) {
case 'linux-x64':
targetFile = `linux_x64_${packageName}.node`
break
case 'linux-arm64':
targetFile = `linux_arm64_${packageName}.node`
break
case 'macos-arm64':
targetFile = `macos_arm64_${packageName}.node`
break
case 'macos-x64':
targetFile = `macos_x64_${packageName}.node`
break
case 'alpine-x64':
targetFile = `alpine_x64_${packageName}.node`
break
case 'alpine-arm64':
targetFile = `alpine_arm64_${packageName}.node`
break
case 'windows-x64':
targetFile = `windows_x64_${packageName}.node`
break
case 'windows-arm64':
targetFile = `windows_arm64_${packageName}.node`
break
default:
console.error(`\n ❗️ Unsupported target platform:${targetPlatform} \n`)
}
if (targetFile) {
if (!fs.existsSync(targetPath)) fs.mkdirSync(targetPath, { recursive: true })
// 复制文件
fs.copyFileSync(
path.join(sourcePath, targetFile),
path.join(targetPath, `${packageName}.node`),
)
console.log(
`\n ✅ Copied ${path.join(sourcePath, targetFile)} to ${path.join(
targetPath,
'node_sqlite3.node',
)}\n`,
)
}
}
const pkgRelease = (targetPlatform) => {
moveNodeSqlite(targetPlatform)
moveNodeSqlite(targetPlatform, 'bcrypt_lib', bcryptTargetPath)
// 执行打包命令
execSync(
`pkg . -t ${targetPlatform} --output ./dist/${releaseName}${targetPlatform.replace(/node\d+/g, '')}${targetPlatform.indexOf('windows') !== -1 ? '.exe' : ''
}` + (isDebug ? ' --debug' : ''),
{ stdio: 'inherit' },
)
}
const start = () => {
try {
const dataString = fs.readFileSync(
path.join(__dirname, 'package.json'),
'utf-8',
)
const data = JSON.parse(dataString)
const platforms = data.pkg.targets
releaseName = data.name
for (let item of platforms) {
pkgRelease(item)
}
} catch (e) {
console.error('❗️ read package.json failed', e)
}
}
start()