89 lines
2.4 KiB
JavaScript
89 lines
2.4 KiB
JavaScript
/**
|
|
* JNote 后端打包脚本
|
|
* 支持 Node.js 16.x
|
|
*/
|
|
|
|
const { execSync } = require('child_process');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const serverDir = __dirname;
|
|
|
|
// Node 16.x -> pkg 5.8.1
|
|
const PKG_VERSION = '5.8.1';
|
|
const TARGET_NODE_VERSION = 'node16';
|
|
|
|
function log(msg) {
|
|
console.log(`[Build] ${msg}`);
|
|
}
|
|
|
|
function run(cmd) {
|
|
log(`执行: ${cmd}`);
|
|
execSync(cmd, { cwd: serverDir, stdio: 'inherit' });
|
|
}
|
|
|
|
function checkNodeVersion() {
|
|
const version = execSync('node --version', { encoding: 'utf8' }).trim();
|
|
const match = version.match(/^v(\d+)\./);
|
|
if (match) {
|
|
return parseInt(match[1]);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function main() {
|
|
console.log('=========================================');
|
|
console.log(' JNote 后端打包工具');
|
|
console.log(` 当前平台: ${isWindows ? 'Windows' : 'Linux'}`);
|
|
console.log('=========================================');
|
|
|
|
// 检查 Node.js 环境
|
|
const nodeMajor = checkNodeVersion();
|
|
if (!nodeMajor) {
|
|
console.error('❌ 错误: 未找到 Node.js');
|
|
process.exit(1);
|
|
}
|
|
log(`✓ Node.js 版本: v${nodeMajor}.x`);
|
|
|
|
// 安装依赖
|
|
console.log('');
|
|
log('📦 安装依赖...');
|
|
run('npm install');
|
|
|
|
// 安装 pkg
|
|
try {
|
|
execSync('pkg --version', { encoding: 'utf8', stdio: 'ignore' });
|
|
log('✓ pkg 已安装');
|
|
} catch {
|
|
console.log('');
|
|
log(`📦 安装 pkg ${PKG_VERSION}...`);
|
|
run(`npm install -g pkg@${PKG_VERSION}`);
|
|
}
|
|
|
|
// 创建 dist 目录
|
|
const distDir = path.join(serverDir, 'dist');
|
|
if (!fs.existsSync(distDir)) {
|
|
fs.mkdirSync(distDir, { recursive: true });
|
|
}
|
|
|
|
// 打包 Linux 版本
|
|
console.log('');
|
|
log(`🔨 打包 Linux 版本 (${TARGET_NODE_VERSION}-linux-x64)...`);
|
|
run(`pkg src/index.js --targets ${TARGET_NODE_VERSION}-linux-x64 --output dist/jnote-api`);
|
|
|
|
console.log('');
|
|
console.log('=========================================');
|
|
console.log(' 打包完成!');
|
|
console.log('=========================================');
|
|
console.log('');
|
|
console.log('输出文件:');
|
|
console.log(' - dist/jnote-api (Linux 可执行文件)');
|
|
console.log('');
|
|
console.log('下一步:');
|
|
console.log(' 1. 将 dist/ 目录下的文件上传到服务器');
|
|
console.log(' 2. 修改 .env 配置数据库等信息');
|
|
console.log(' 3. 运行: chmod +x jnote-api && ./jnote-api');
|
|
console.log('');
|
|
}
|
|
|
|
main(); |