您的位置:首页 > 游戏 > 游戏 > PKG打包sqlite3项目,如何添加node_sqlite3.node依赖

PKG打包sqlite3项目,如何添加node_sqlite3.node依赖

2024/10/6 4:10:04 来源:https://blog.csdn.net/qq_37651169/article/details/139966718  浏览:    关键词:PKG打包sqlite3项目,如何添加node_sqlite3.node依赖

项目地址:https://github.com/helson-lin/pkg_sqlite

ffandown项目内,由于项目使用了sqlite3,在跨平台打包的时候,除了本机外其他平台打包之后运行缺少node_sqlite3.node依赖。

为了解决问题,百度了很久,能够实现的方案就三种。

  1. 分别在不同平台打包,这个是最直接的方法。
  2. node_sqlite3.node文件放在可执行文件同级目录下,运行的时候binding会自动找到。
  3. 在每一次构建之后,手动将node_sqlite3.node依赖移动到node_modules/sqlite3/building/Release/, 该方案支持github actions自动打包。

方案3如何实现github actions自动打包

准备好各个平台的node_sqlite3.node文件

这里演示没有配置所有的node文件,其他平台自行配置

在这里插入图片描述

文件可以从TryGhost-GIthub下载

编写脚本打包之前替换node_sqlite3.node文件

脚本如下,主要思路就是,在每个平台打包之前,现将对应平台的文件移动到node_modules/sqlite3/building/Release/下面。
如果直接使用改脚本,请确保package下面文件的名称和我的保持一致。

📢 需要注意,这个脚本运行之前有一些前提的条件

  1. package.json配置好pkg配置项, 包括targets和assets
  2. 所有的node文件放在同级的package目录下
  3. 脚本文件放在根目录下面的
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const { execSync} = require('child_process')
// 源文件路径(根据你的项目结构调整)
let isDebug = false;
let releaseName;
const argv = process.argv.slice(2)
//  支持debug参数
if (argv && argv[0] === '--debug') isDebug = true
// package为我的项目根目录下面文件夹存放node文件
const sourcePath = path.join(__dirname, "package/");
// 目标路径
const targetPath = path.join(__dirname, "node_modules/sqlite3/build/Release/");const moveNodeSqlite = (targetPlatform) => {// 根据目标平台选择正确的文件,这里只写了几个平台可以自行补充let targetFile;const name = targetPlatform.split('-').slice(1).join('-')switch (name) {case "linux-x64":targetFile = "linux_x64_node_sqlite3.node";break;case "linux-arm64":targetFile = "linux_arm64_node_sqlite3.node";break;case "macos-arm64":targetFile = "macos_arm64_node_sqlite3.node";break;case "macos-arm64":targetFile = "macos_x64_node_sqlite3.node";break;default:console.error(`\n ❗️ Unsupported target platform:${targetPlatform} \n`);}if (targetFile) {// 复制文件fs.copyFileSync(path.join(sourcePath, targetFile),path.join(targetPath, "node_sqlite3.node"));console.log(`\n ✅ Copied ${path.join(sourcePath, targetFile)} to ${path.join(targetPath,"node_sqlite3.node")}\n`);}
};const pkgRelease = (targetPlatform) => {moveNodeSqlite(targetPlatform);// 执行打包命令//  --output指定输出的目录地址,和文件的名称execSync(`pkg . -t ${targetPlatform} --output ./dist/${releaseName}-${targetPlatform}${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.targetsreleaseName = data.namefor (let item of platforms) {pkgRelease(item)}} catch (e) {console.error('❗️ read package.json failed', e)}
}start()

package.json配置

这里只粘贴了pkg配置部分、
scripts配置项为需要打包的js文件
assets配置项必不可少,少了node文件不会被打包到可执行文件内
targets配置项填写需要构建的平台,这里被脚本引用了,少了脚本会出现问题。

{"name": "docker_sync","version": "1.0.0","description": "","main": "index.js","bin": "index.js","scripts": {"build": "node build.js"},"repository": {"type": "git","url": "git+https://github.com/helson-lin/docker_sync_template.git"},"keywords": ["demo"],"author": "helsonlin","license": "ISC","pkg": {"scripts": ["index.js","db.js"],"assets": ["/node_modules/sqlite3/build/**/*"],"targets": ["node14-macos-arm64","node14-macos-x64","node14-windows-x64","node14-linux-x64","node14-linux-arm64","node14-alpine-x64","node14-alpine-arm64"],"outputPath": "dist"},"bugs": {"url": "https://github.com/helson-lin/docker_sync_template/issues"},"homepage": "https://github.com/helson-lin/docker_sync_template#readme","devDependencies": {"body-parser": "^1.20.2","express": "^4.18.2","pkg": "^5.8.1","sqlite3": "^5.1.6"}
}

Github actions配置

该配置文件不做过多解释

name: Build and push Docker imageon:push:branches: [ main ]tags:- 'v*.*.*'jobs:build:runs-on: ubuntu-lateststeps:- name: Checkout codeuses: actions/checkout@v2- name: Npm Installrun: npm install --registry=https://registry.npmmirror.com- name: Build Releaserun: npm run build- name: releaseuses: softprops/action-gh-release@v1if: startsWith(github.ref, 'refs/tags/')with:files: "dist/**"env:GITHUB_TOKEN: ${{ secrets.TOKEN }}
效果

请添加图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com