| 1 |
name: Node.js |
| 2 |
|
| 3 |
on: |
| 4 |
push: |
| 5 |
branches: [master] |
| 6 |
pull_request: |
| 7 |
branches: [master] |
| 8 |
|
| 9 |
concurrency: |
| 10 |
group: ${{ github.workflow }}-${{ github.ref }} |
| 11 |
cancel-in-progress: true |
| 12 |
|
| 13 |
jobs: |
| 14 |
build-and-test: |
| 15 |
permissions: |
| 16 |
contents: read |
| 17 |
runs-on: ${{ matrix.os }} |
| 18 |
|
| 19 |
strategy: |
| 20 |
matrix: |
| 21 |
|
| 22 |
os: [ubuntu-latest, macos-latest] |
| 23 |
node-version: [20, 22, 24, 25] |
| 24 |
|
| 25 |
include: |
| 26 |
- os: macos-14 |
| 27 |
node-version: 24 |
| 28 |
- os: macos-15-intel |
| 29 |
node-version: 24 |
| 30 |
- os: windows-2022 |
| 31 |
node-version: 24 |
| 32 |
steps: |
| 33 |
- uses: actions/checkout@v5 |
| 34 |
with: |
| 35 |
fetch-depth: 0 |
| 36 |
- name: Compute version |
| 37 |
id: version |
| 38 |
shell: bash |
| 39 |
run: | |
| 40 |
VERSION=$(bash scripts/compute-version.sh) |
| 41 |
echo "VERSION=$VERSION" >> "$GITHUB_ENV" |
| 42 |
echo "version=$VERSION" >> "$GITHUB_OUTPUT" |
| 43 |
echo "Computed version: $VERSION" |
| 44 |
- name: Use Node.js ${{ matrix.node-version }} |
| 45 |
uses: actions/setup-node@v5 |
| 46 |
with: |
| 47 |
node-version: ${{ matrix.node-version }} |
| 48 |
- run: npm ci |
| 49 |
- run: npm test |
| 50 |
- name: Build Node.js prebuild |
| 51 |
if: ${{ matrix.node-version == 24 }} |
| 52 |
run: npm run prebuild |
| 53 |
- name: Verify Node.js prebuild layout |
| 54 |
if: ${{ matrix.node-version == 24 }} |
| 55 |
run: | |
| 56 |
node -e "const fs=require('fs'); const path=require('path'); const dirs=fs.readdirSync('prebuilds',{withFileTypes:true}).filter((entry)=>entry.isDirectory()).map((entry)=>entry.name); if(!dirs.includes('assets')) throw new Error('missing prebuilds/assets'); const platformDirs=dirs.filter((name)=>name!=='assets'); if(platformDirs.length!==1) throw new Error('expected one platform prebuild directory, found '+platformDirs.join(',')); const addon=path.join('prebuilds', platformDirs[0], 'opencc.node'); if(!fs.existsSync(addon)) throw new Error('missing '+addon); console.log('Verified '+addon+' with shared assets');" |
| 57 |
- name: Test Node.js prebuild |
| 58 |
if: ${{ matrix.node-version == 24 }} |
| 59 |
env: |
| 60 |
PREBUILDS_ONLY: 1 |
| 61 |
run: npm test |
| 62 |
- name: upload artifacts |
| 63 |
if: ${{ always() }} |
| 64 |
uses: actions/upload-artifact@v7 |
| 65 |
with: |
| 66 |
name: opencc-${{ env.VERSION }}-nodejs-${{ matrix.os }}-node-${{ matrix.node-version }} |
| 67 |
path: | |
| 68 |
node/build/** |
| 69 |
prebuilds/** |
| 70 |
**/npm-debug.log |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
pack-source-install: |
| 76 |
name: Source build from npm tarball (${{ matrix.os }}) |
| 77 |
permissions: |
| 78 |
contents: read |
| 79 |
runs-on: ${{ matrix.os }} |
| 80 |
|
| 81 |
strategy: |
| 82 |
fail-fast: false |
| 83 |
matrix: |
| 84 |
os: [ubuntu-latest, macos-latest, macos-15-intel] |
| 85 |
steps: |
| 86 |
- uses: actions/checkout@v5 |
| 87 |
with: |
| 88 |
fetch-depth: 0 |
| 89 |
- name: Use Node.js |
| 90 |
uses: actions/setup-node@v5 |
| 91 |
with: |
| 92 |
node-version: 24 |
| 93 |
- run: npm ci |
| 94 |
- name: Build prebuild assets |
| 95 |
run: npm run prebuild |
| 96 |
- name: Pack npm tarball |
| 97 |
shell: bash |
| 98 |
run: | |
| 99 |
mkdir -p dist/npm-artifacts |
| 100 |
npm pack --pack-destination dist/npm-artifacts |
| 101 |
- name: Install tarball from source and smoke test |
| 102 |
shell: bash |
| 103 |
run: | |
| 104 |
tarball="$(ls "$PWD"/dist/npm-artifacts/opencc-*.tgz)" |
| 105 |
install_dir="$(mktemp -d)" |
| 106 |
cd "$install_dir" |
| 107 |
npm init -y |
| 108 |
npm install --omit=optional --foreground-scripts "$tarball" |
| 109 |
test -f node_modules/opencc/build/Release/opencc.node |
| 110 |
node -e ' |
| 111 |
const assert = require("assert"); |
| 112 |
const OpenCC = require("opencc"); |
| 113 |
const converter = new OpenCC("s2t.json"); |
| 114 |
const converted = converter.convertSync("汉字"); |
| 115 |
assert.strictEqual(converted, "漢字"); |
| 116 |
console.log("Source-built opencc converts 汉字 ->", converted); |
| 117 |
' |