101 lines
3.0 KiB
JavaScript
101 lines
3.0 KiB
JavaScript
|
const Database = require('better-sqlite3');
|
|||
|
const { getXNCorePath } = require('./file-utils');
|
|||
|
|
|||
|
// 版本阶段枚举
|
|||
|
const VERSION_STAGES = {
|
|||
|
ALPHA: 'alpha', // 开发版
|
|||
|
BETA: 'beta', // 测试版
|
|||
|
RELEASE_CANDIDATE: 'rc', // 候选版
|
|||
|
STABLE: 'stable' // 正式版
|
|||
|
};
|
|||
|
|
|||
|
// 获取所有版本记录
|
|||
|
function getAllVersions() {
|
|||
|
try {
|
|||
|
const xnCorePath = getXNCorePath();
|
|||
|
const dbPath = xnCorePath + '/database/XNSim.db';
|
|||
|
const db = new Database(dbPath, { readonly: true });
|
|||
|
|
|||
|
// 添加stage字段到查询中
|
|||
|
const versions = db.prepare(`
|
|||
|
SELECT verNum, date, time, title, note, author, stage
|
|||
|
FROM version
|
|||
|
ORDER BY date DESC, time DESC
|
|||
|
`).all();
|
|||
|
|
|||
|
db.close();
|
|||
|
return versions;
|
|||
|
} catch (error) {
|
|||
|
console.error('获取版本记录失败:', error);
|
|||
|
throw error;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 添加新版本记录
|
|||
|
function addVersion(versionData) {
|
|||
|
try {
|
|||
|
const { verNum, date, time, title, note, author, stage } = versionData;
|
|||
|
|
|||
|
// 数据验证
|
|||
|
if (!verNum || !date || !time || !title) {
|
|||
|
throw new Error('版本号、日期、时间和标题不能为空');
|
|||
|
}
|
|||
|
|
|||
|
// 验证版本阶段
|
|||
|
if (stage && !Object.values(VERSION_STAGES).includes(stage)) {
|
|||
|
throw new Error('无效的版本阶段');
|
|||
|
}
|
|||
|
|
|||
|
const xnCorePath = getXNCorePath();
|
|||
|
const dbPath = xnCorePath + '/database/XNSim.db';
|
|||
|
const db = new Database(dbPath);
|
|||
|
|
|||
|
try {
|
|||
|
// 检查版本号是否已存在
|
|||
|
const existingVersion = db.prepare('SELECT verNum FROM version WHERE verNum = ?').get(verNum);
|
|||
|
if (existingVersion) {
|
|||
|
throw new Error('该版本号已存在');
|
|||
|
}
|
|||
|
|
|||
|
// 插入新版本记录,包含stage字段
|
|||
|
const stmt = db.prepare(`
|
|||
|
INSERT INTO version (
|
|||
|
verNum, date, time, title, note, author, stage
|
|||
|
) VALUES (?, ?, ?, ?, ?, ?, ?)
|
|||
|
`);
|
|||
|
const result = stmt.run(
|
|||
|
verNum,
|
|||
|
date,
|
|||
|
time,
|
|||
|
title,
|
|||
|
note || '',
|
|||
|
author || '未知用户',
|
|||
|
stage || VERSION_STAGES.ALPHA // 默认为开发版
|
|||
|
);
|
|||
|
|
|||
|
db.close();
|
|||
|
|
|||
|
if (result.changes > 0) {
|
|||
|
return {
|
|||
|
success: true,
|
|||
|
message: '版本记录添加成功',
|
|||
|
id: result.lastInsertRowid
|
|||
|
};
|
|||
|
} else {
|
|||
|
throw new Error('添加版本记录失败');
|
|||
|
}
|
|||
|
} catch (dbError) {
|
|||
|
db.close();
|
|||
|
throw dbError;
|
|||
|
}
|
|||
|
} catch (error) {
|
|||
|
console.error('添加版本记录失败:', error);
|
|||
|
throw error;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
module.exports = {
|
|||
|
getAllVersions,
|
|||
|
addVersion,
|
|||
|
VERSION_STAGES
|
|||
|
};
|