From d56ebca946528ab1fbf9a49dc539816894549b88 Mon Sep 17 00:00:00 2001 From: frostime Date: Wed, 11 Dec 2024 14:47:12 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20ci:=20git-tag?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 +- scripts/git-tag.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 scripts/git-tag.js diff --git a/package.json b/package.json index aa90dcc..80cb0a8 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "make-install": "pnpm run vite:build-no-minify && npx make-install", "auto-i18n": "i18n extract && i18n translate && i18n export", "export-types": "node scripts/export-types.js", - "replace-md-file": "node scripts/replace-md-file.js" + "replace-md-file": "node scripts/replace-md-file.js", + "git-tag": "node scripts/git-tag.js" }, "devDependencies": { "@sveltejs/vite-plugin-svelte": "^3.1.0", diff --git a/scripts/git-tag.js b/scripts/git-tag.js new file mode 100644 index 0000000..5c82c7b --- /dev/null +++ b/scripts/git-tag.js @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2024 by frostime. All Rights Reserved. + * @Author : frostime + * @Date : 2024-12-11 14:35:13 + * @FilePath : /scripts/git-tag.js + * @LastEditTime : 2024-12-11 14:41:11 + * @Description : + */ +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; +import process from 'process'; +import readline from'readline'; +import child_process from 'child_process'; + + +// First define __filename and __dirname +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); + + +const confirm = async (question) => { + const userConfirm = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + return new Promise((resolve) => { + userConfirm.question(question, (answer) => { + userConfirm.close(); + resolve(answer.toLowerCase() === 'y'); + }); + }); +} + + +// Then use process +process.chdir(path.join(__dirname, '..')); +console.log(process.cwd()); +const dirname = process.cwd(); + +const pluginJson = JSON.parse(fs.readFileSync(path.join(dirname, 'plugin.json'), 'utf-8')); +const tagName = `v${pluginJson.version}`; + +// 检查 tag 是否存在, 如果存在就删掉 +const gitTagListCommand = `git tag -l ${tagName}`; +console.log(gitTagListCommand); +const gitTagListResult = child_process.execSync(gitTagListCommand, { encoding: 'utf-8' }); +console.log(gitTagListResult); +if (gitTagListResult.trim() === tagName) { + console.log(`Tag ${tagName} already exists, it first.`); + let flag = await confirm(`Do you want to delete the tag ${tagName}? (y/n) `); + if (flag) { + const gitTagDeleteCommand = `git tag -d ${tagName}`; + console.log(gitTagDeleteCommand); + const gitTagDeleteResult = child_process.execSync(gitTagDeleteCommand, { encoding: 'utf-8' }); + console.log(gitTagDeleteResult); + } else { + console.log('Aborted.'); + process.exit(0); + } +} + +// git tag +const gitTagCommand = `git tag ${tagName}`; +console.log(gitTagCommand); +const gitTagResult = child_process.execSync(gitTagCommand, { encoding: 'utf-8' }); +console.log(gitTagResult); + + +let upload = await confirm(`Do you want to upload the tag ${tagName} to the remote repository? (y/n) `); +if (upload) { + const gitPushCommand = `git push origin ${tagName}`; + console.log(gitPushCommand); + const gitPushResult = child_process.execSync(gitPushCommand, { encoding: 'utf-8' }); + console.log(gitPushResult); +}