• 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.targets
        releaseName = data.name
        for (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 image
    
    on:
      push:
        branches: [ main ]
        tags:
            - 'v*.*.*'
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
    
        - name: Checkout code
          uses: actions/checkout@v2
    
        - name: Npm Install
          run: npm install --registry=https://registry.npmmirror.com
    
        - name: Build Release
          run: npm run build
    
        - name: release
          uses: softprops/action-gh-release@v1
          if: startsWith(github.ref, 'refs/tags/')
          with:
            files: "dist/**"
          env:
            GITHUB_TOKEN: ${{ secrets.TOKEN }}
    
    
    效果

    请添加图片描述

  • 相关阅读:
    ARM 汇编语言教程
    PowerShell 美化(谁不想要一个好看的终端呢)
    hvv蓝初 看完可去 面试可用 面经
    docker部署-Linux
    Programming Languages PartC Week1学习笔记——Ruby与面向对象编程
    【零基础学Python】Day2 Python基本语法
    【LLM】浅谈 StreamingLLM中的attention sink和sink token
    Go 之烧脑的接口
    Zuul网关探秘
    CSS渐变色理论与分类、文字渐变色方案、炸裂渐变色方案以及主流专业渐变色工具网站推荐
  • 原文地址:https://blog.csdn.net/qq_37651169/article/details/139966718