build: add scripts

This commit is contained in:
Moy 2025-05-07 22:00:41 +08:00
parent 7dfac7b742
commit bbd7c9332b
5 changed files with 316 additions and 2 deletions

View file

@ -1,6 +1,6 @@
{
"name": "obsidian-sample-plugin",
"version": "1.0.0",
"version": "1.1.0",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "main.js",
"scripts": {
@ -8,7 +8,7 @@
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
"build:local": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production && npm run copy-to-vault",
"copy-to-vault": "node scripts/copy-to-vault.mjs",
"version": "node version-bump.mjs && git add manifest.json versions.json"
"version": "node scripts/version-bump-new.mjs && git add manifest.json versions.json"
},
"keywords": [],
"author": "",

View file

@ -0,0 +1,61 @@
module.exports = {
writerOpts: {
transform: (commit, context) => {
// 创建一个新对象而不是修改原对象
const transformedCommit = { ...commit };
// 定义完整的commit类型映射
const typeMap = {
feat: "✨ Features",
fix: "🐛 Bug Fixes",
// docs: "📝 Documentation",
style: "🎨 Styles",
refactor: "♻️ Refactor",
perf: "⚡️ Performance",
// test: "✅ Tests",
// build: "👷 Build",
// ci: "🔧 CI",
// chore: "🔨 Chore",
revert: "⏪️ Reverts",
};
// 应用类型映射
if (transformedCommit.type && typeMap[transformedCommit.type]) {
transformedCommit.type = typeMap[transformedCommit.type];
}
// 如果没有匹配的类型,跳过
if (transformedCommit.type === commit.type) {
return false;
}
// 保留URL链接等信息
if (commit.scope === "*") {
transformedCommit.scope = "";
}
// 确保 hash 作为链接文本
if (transformedCommit.hash) {
transformedCommit.shortHash = transformedCommit.hash.substring(
0,
7
);
}
// 处理 BREAKING CHANGE - 创建深层复制
if (commit.notes && commit.notes.length > 0) {
transformedCommit.notes = commit.notes.map((note) => {
// 创建笔记的副本
const noteCopy = { ...note };
// 修改副本而不是原对象
if (noteCopy.title === "BREAKING CHANGE") {
noteCopy.title = "💥 BREAKING CHANGE";
}
return noteCopy;
});
}
return transformedCommit;
},
},
};

36
scripts/release-tag.mjs Normal file
View file

@ -0,0 +1,36 @@
/**
* 执行发版和打标签操作的专用脚本
*/
import { execSync } from "child_process";
// 从 package.json 获取当前版本号
const version = process.env.npm_package_version;
console.log(`📦 Preparing to release version: ${version}`);
try {
// 执行 git add 操作
console.log("📝 添加文件到 git...");
execSync("git add .", { stdio: "inherit" });
// 执行 git commit 操作
console.log("💾 创建提交...");
execSync(`git commit -m "build: ${version}"`, { stdio: "inherit" });
// 执行 git push 操作
console.log("🚀 推送到远程...");
execSync("git push", { stdio: "inherit" });
// 创建版本标签
console.log(`🏷️ 创建标签: ${version}`);
execSync(`git tag ${version}`, { stdio: "inherit" });
// 推送标签到远程
console.log("📤 推送标签到远程...");
execSync("git push --tags", { stdio: "inherit" });
console.log(`✅ 成功发布版本 ${version}!`);
} catch (error) {
console.error(`❌ 发布失败: ${error.message}`);
process.exit(1);
}

View file

@ -0,0 +1,217 @@
#!/usr/bin/env node
import { existsSync, readFileSync, writeFileSync } from "fs";
import readline from "readline";
// 检查是否为直接调用模式
const directVersion = process.argv[2];
const isDirectMode = !!directVersion;
// 如果是直接调用模式,直接更新版本
if (isDirectMode) {
updateAllFiles(directVersion);
process.exit(0);
}
// 否则,启动交互式模式
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// 获取当前版本
const packageJson = JSON.parse(readFileSync("package.json", "utf8"));
const currentVersion = packageJson.version;
console.log(`当前版本: ${currentVersion}`);
// 解析版本号
const [major, minor, patch] = currentVersion.split(".").map(Number);
// 显示选项
console.log("\n请选择版本更新类型:");
console.log(`1. 主版本 (${major + 1}.0.0)`);
console.log(`2. 次版本 (${major}.${minor + 1}.0)`);
console.log(`3. 补丁版本 (${major}.${minor}.${patch + 1})`);
console.log(`4. 自定义版本`);
console.log(`5. Beta 版本 (${currentVersion}-beta.1)`);
rl.question("\n请输入选项 (1-5): ", (answer) => {
let newVersion;
let isBeta = false;
switch (answer) {
case "1":
newVersion = `${major + 1}.0.0`;
break;
case "2":
newVersion = `${major}.${minor + 1}.0`;
break;
case "3":
newVersion = `${major}.${minor}.${patch + 1}`;
break;
case "4":
rl.question("请输入自定义版本号 (x.y.z): ", (customVersion) => {
isBeta = customVersion.includes("-beta");
updateAllFiles(customVersion, isBeta);
rl.close();
});
return;
case "5":
// 检查当前版本是否已经是 beta
if (currentVersion.includes("-beta.")) {
const betaRegex = /-beta\.(\d+)$/;
const match = currentVersion.match(betaRegex);
if (match) {
const betaNum = parseInt(match[1], 10);
newVersion = currentVersion.replace(
betaRegex,
`-beta.${betaNum + 1}`
);
} else {
newVersion = `${currentVersion}-beta.1`;
}
} else {
newVersion = `${currentVersion}-beta.1`;
}
isBeta = true;
break;
default:
console.log("无效选项,使用补丁版本更新");
newVersion = `${major}.${minor}.${patch + 1}`;
}
updateAllFiles(newVersion, isBeta);
rl.close();
});
/**
* 更新所有相关文件的版本号
* @param {string} version 新版本号
* @param {boolean} isBeta 是否为Beta版本
*/
function updateAllFiles(version, isBeta = false) {
try {
console.log(`\n正在更新至版本 ${version}...`);
// 1. 更新 package.json
updatePackageJson(version);
// 2. 更新 manifest.json 或 manifest-beta.json
const minAppVersion = updateManifestJson(version, isBeta);
// 3. 更新 versions.json
updateVersionsJson(version, minAppVersion);
// 提示提交更改
console.log("\n版本已更新。建议执行以下命令:");
if (isBeta) {
console.log(
`git add package.json manifest-beta.json versions.json`
);
} else {
console.log(`git add package.json manifest.json versions.json`);
}
console.log(`git commit -m "build: bump version to ${version}"`);
console.log(`git tag ${version}`);
console.log("\n版本更新完成");
} catch (error) {
console.error("更新版本时出错:", error);
process.exit(1);
}
}
/**
* 更新 package.json 文件
* @param {string} version 新版本号
*/
function updatePackageJson(version) {
try {
const packageJson = JSON.parse(readFileSync("package.json", "utf8"));
packageJson.version = version;
writeFileSync(
"package.json",
JSON.stringify(packageJson, null, "\t") + "\n"
);
console.log(`已更新 package.json 版本至 ${version}`);
} catch (error) {
console.error("更新 package.json 时出错:", error);
throw error;
}
}
/**
* 更新 manifest.json manifest-beta.json 文件
* @param {string} version 新版本号
* @param {boolean} isBeta 是否为Beta版本
* @returns {string} 最低应用版本
*/
function updateManifestJson(version, isBeta = false) {
try {
const manifestFile = isBeta ? "manifest-beta.json" : "manifest.json";
// 检查文件是否存在
if (!existsSync(manifestFile) && isBeta) {
// 如果manifest-beta.json不存在则从manifest.json复制一份
if (existsSync("manifest.json")) {
const manifest = JSON.parse(
readFileSync("manifest.json", "utf8")
);
manifest.version = version;
writeFileSync(
manifestFile,
JSON.stringify(manifest, null, "\t") + "\n"
);
console.log(`已创建并更新 ${manifestFile} 版本至 ${version}`);
return manifest.minAppVersion;
}
}
const manifest = JSON.parse(readFileSync(manifestFile, "utf8"));
const { minAppVersion } = manifest;
manifest.version = version;
writeFileSync(
manifestFile,
JSON.stringify(manifest, null, "\t") + "\n"
);
console.log(`已更新 ${manifestFile} 版本至 ${version}`);
return minAppVersion;
} catch (error) {
console.error(`更新 manifest 文件时出错:`, error);
throw error;
}
}
/**
* 更新 versions.json 文件
* @param {string} version 新版本号
* @param {string} minAppVersion 最低应用版本
*/
function updateVersionsJson(version, minAppVersion) {
try {
// 读取或创建 versions.json
let versions = {};
try {
if (existsSync("versions.json")) {
const versionsContent = readFileSync("versions.json", "utf8");
if (versionsContent.trim()) {
versions = JSON.parse(versionsContent);
}
}
} catch (error) {
console.log("创建新的 versions.json 文件");
}
// 更新版本信息
versions[version] = minAppVersion;
writeFileSync(
"versions.json",
JSON.stringify(versions, null, "\t") + "\n"
);
console.log(
`已更新 versions.json添加版本 ${version} -> ${minAppVersion}`
);
} catch (error) {
console.error("更新 versions.json 时出错:", error);
throw error;
}
}