build(package): 更换pkg依赖并提升node要求至22

This commit is contained in:
Takanashi Hoshino
2026-10-17 19:48:29 +08:00
parent 19c7dad6db
commit 49c7800afe
6 changed files with 669 additions and 281 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 22, 24]
node-version: [22, 24]
steps:
- uses: actions/checkout@v6
@@ -78,17 +78,17 @@ jobs:
include:
- os: ubuntu-latest
platform: linux
target: node18-linux-x64
target: node22-linux-x64
output: ncm-api-linux-x64
- os: windows-latest
platform: windows
target: node18-win-x64
target: node22-win-x64
output: ncm-api-win-x64.exe
- os: macos-latest
platform: macos
target: node18-macos-x64
target: node22-macos-x64
output: ncm-api-macos-x64
steps:
+2 -2
View File
@@ -2,7 +2,7 @@
## 快速开始
- **包管理器**:开发与 CI 使用 `pnpm`,`Dockerfile` 也用 pnpm(`pnpm@9 --frozen-lockfile`,匹配 `pnpm-lock.yaml`)。仓库没有 `yarn.lock`,不要引入 yarn。
- **Node 版本**:README 推荐 Node 22+;`package.json` 的 `engines` 声明 `>=12`;CI/打包在 Node 18–24 上运行。现代 Node 均可。
- **Node 版本**:`package.json` 的 `engines` 声明 `>=22`;CI/打包在 Node 22–24 上运行。现代 Node 均可。
- **环境变量**:`server.js` 调用了 `dotenv.config()`,本地 `.env` 会被自动加载;所有支持的变量见 `.env.prod.example`。
## 常用命令
@@ -11,7 +11,7 @@
- 跑测试:`pnpm test`(Mocha,超时 60s)
- Lint:`pnpm lint`;自动修复:`pnpm lint-fix`
- 文档格式化检查/修复:`pnpm docs:check` / `pnpm docs:format`
- 打包独立二进制:`pnpm pkgwin` / `pkglinux` / `pkgmacos`
- 打包独立二进制:`pnpm pkgwin` / `pkglinux` / `pkgmacos`(@yao-pkg/pkg,node22 目标,`--no-bytecode` + `--public` 规避 node22 bytecode 回归)
## 目录内非路由文件
- `plugins/`——内部上传辅助(`upload.js`、`songUpload.js`),被 `module/*` require,本身不是路由。
+5 -5
View File
@@ -11,9 +11,9 @@
"prepare": "husky install",
"docs:format": "node scripts/format-docs.js",
"docs:check": "node scripts/format-docs.js --check",
"pkgwin": "node scripts/build-pkg.js . -t node18-win-x64 -C GZip -o precompiled/app",
"pkglinux": "node scripts/build-pkg.js . -t node18-linux-x64 -C GZip -o precompiled/app",
"pkgmacos": "node scripts/build-pkg.js . -t node18-macos-x64 -C GZip -o precompiled/app"
"pkgwin": "node scripts/build-pkg.js . -t node22-win-x64 -C GZip -o precompiled/app --no-bytecode --public-packages \"*\" --public",
"pkglinux": "node scripts/build-pkg.js . -t node22-linux-x64 -C GZip -o precompiled/app --no-bytecode --public-packages \"*\" --public",
"pkgmacos": "node scripts/build-pkg.js . -t node22-macos-x64 -C GZip -o precompiled/app --no-bytecode --public-packages \"*\" --public"
},
"bin": "./app.js",
"pkg": {
@@ -52,7 +52,7 @@
},
"homepage": "https://neteasecloudmusicapienhanced.js.org/",
"engines": {
"node": ">=12"
"node": ">=22"
},
"lint-staged": {
"*.js": [
@@ -112,7 +112,7 @@
"lint-staged": "^16.4.0",
"mocha": "^11.8.0",
"nodemon": "^3.1.14",
"pkg": "^5.8.1",
"@yao-pkg/pkg": "^6.23.0",
"power-assert": "^1.6.1",
"prettier": "^3.9.9",
"typescript": "^5.9.3"
+642 -262
View File
File diff suppressed because it is too large Load Diff
+16 -8
View File
@@ -1,10 +1,10 @@
/**
* Invoke the pkg packager (cross-platform, independent of node_modules layout).
*
* Background: on Windows, pnpm links node_modules/pkg to
* node_modules/.pnpm/pkg@<ver>/node_modules/pkg via a junction. Some CI
* environments (pnpm 11 + Windows runner) may fail to create that link,
* so executing node_modules/pkg/lib-es5/bin.js directly throws
* Background: on Windows, pnpm links node_modules/@yao-pkg/pkg to
* node_modules/.pnpm/@yao-pkg+pkg@<ver>/node_modules/@yao-pkg/pkg via a junction.
* Some CI environments (pnpm 11 + Windows runner) may fail to create that link,
* so executing node_modules/@yao-pkg/pkg/lib-es5/bin.js directly throws
* MODULE_NOT_FOUND.
*
* This script first resolves pkg via require.resolve and falls back to
@@ -17,16 +17,24 @@ const path = require('path')
function resolvePkgBin() {
try {
return require.resolve('pkg/lib-es5/bin.js')
return require.resolve('@yao-pkg/pkg/lib-es5/bin.js')
} catch (err) {
// require.resolve failed (e.g. missing node_modules/pkg link): scan .pnpm
// require.resolve failed (e.g. missing node_modules/@yao-pkg/pkg link): scan .pnpm
const pnpmDir = path.join(process.cwd(), 'node_modules', '.pnpm')
if (fs.existsSync(pnpmDir)) {
const candidates = fs
.readdirSync(pnpmDir)
.filter((dir) => dir.startsWith('pkg@'))
.filter((dir) => dir.startsWith('@yao-pkg+pkg@'))
.map((dir) =>
path.join(pnpmDir, dir, 'node_modules', 'pkg', 'lib-es5', 'bin.js'),
path.join(
pnpmDir,
dir,
'node_modules',
'@yao-pkg',
'pkg',
'lib-es5',
'bin.js',
),
)
.filter((file) => fs.existsSync(file))
if (candidates.length) {