diff --git a/package.json b/package.json index b64634b..04d0f17 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "cg": "node ./utils/generateChangelog.js", "c": "conventional-changelog -p angular -i CHANGELOG.md -s", "commit": "git pull && git add . && git-cz", - "r": "node ./utils/generateVersion.js && npm run cg && node ./utils/gitCommit.js && git push --follow-tags origin master", + "r": "npm run cg && node ./utils/gitCommit.js", "prepare": "husky install", "dev": "vitepress dev docs", "build": "vitepress build docs", diff --git a/utils/generateChangelog.js b/utils/generateChangelog.js index b296f7f..7477975 100644 --- a/utils/generateChangelog.js +++ b/utils/generateChangelog.js @@ -1,6 +1,7 @@ const gitlog = require("gitlog").default; const pkg = require('../package.json') const fs = require('fs') +const generateVersion = require('./utils').generateVersion const repo = pkg.repository.url.replace('.git', '') @@ -48,13 +49,19 @@ let commitObj = { for (let i = 0; i < commits.length; i++) { let commit = commits[i] firstCommit = commit - if (commit.tag.includes('tag:')) { + if (commit.tag.includes('tag:') || commit.tag.includes('HEAD')) { start = true if (!s) { s = i } // 对tag进行分类 - let tag = commit.tag.split(',').find(name => name.includes('tag:')) + let tag = commit.tag.split(',').find(name => { + return name.includes('tag:') || name.includes('HEAD') + }) + console.log(commit.tag) + if (tag.includes('HEAD')) { + tag = generateVersion(pkg.version) + } tag = tag.replace('tag:', '').replace('v', '').trim() let tagSort = tag.split('.').map(t => t.padStart(3, '0')).join('.') currentTag = tagSort diff --git a/utils/generateVersion.js b/utils/generateVersion.js deleted file mode 100644 index b2eafdf..0000000 --- a/utils/generateVersion.js +++ /dev/null @@ -1,44 +0,0 @@ -const standardVersion = require("standard-version"); -const pkg = require("../package.json"); - -// Options are the same as command line, except camelCase -// standardVersion returns a Promise -function generateVersion(version) { - let maxTag = 9 - let versionA = version.split("."); - let [major, minor, patch] = versionA; - if (Number(patch) !== maxTag) { - patch = parseInt(patch) + 1; - return `${major}.${minor}.${patch}`; - } - if (Number(patch) === maxTag) { - patch = "0"; - minor = parseInt(minor) + 1; - } - if ((Number(patch) === 0 && Number(minor) === (maxTag + 1)) || Number(minor) === maxTag) { - minor = "0"; - major = parseInt(major) + 1; - } - return `${major}.${minor}.${patch}`; -} - -function run(version) { - let nextVersion = generateVersion(version); - - standardVersion({ - releaseAs: nextVersion, - skip: { - changelog: true, - tag: false, - commit: true, - }, - }) - .then(() => { - // standard-version is done - }) - .catch((err) => { - console.error(`standard-version failed with message: ${err.message}`); - }); -} - -run(pkg.version); diff --git a/utils/gitCommit.js b/utils/gitCommit.js index 30395de..bdc3e3a 100644 --- a/utils/gitCommit.js +++ b/utils/gitCommit.js @@ -1,12 +1,33 @@ const shell = require('shelljs') const pkg = require('../package.json') +const generateVersion = require("./utils").generateVersion +const updatedVersion = require("./updatedVersion").updatedVersion + +// 先获取下个 版本号 +const nextVersion = generateVersion(pkg.version) let gitAddMsg = shell.exec('git add .') if (gitAddMsg.code !== 0) { console.error(gitAddMsg.stderr) } -let gitCommitMsg = shell.exec(`git commit -m "chore(release): ${pkg.version}"`) +// 更新项目的版本号 +updatedVersion(pkg.version) + +// 提交代码 +let gitCommitMsg = shell.exec(`git commit -m "chore(release): ${nextVersion}"`) if (gitCommitMsg.code !== 0) { console.error(gitCommitMsg.stderr) +} + +// 添加git tag +let gitTagMsg = shell.exec(`git tag ${'v' + nextVersion}`) +if (gitTagMsg.code !== 0) { + console.error(gitTagMsg.stderr) +} + +// 推送到远程仓库 +let gitPushMsg = shell.exec('git push --follow-tags origin master') +if (gitPushMsg.code !== 0) { + console.error(gitPushMsg.stderr) } \ No newline at end of file diff --git a/utils/updatedVersion.js b/utils/updatedVersion.js new file mode 100644 index 0000000..15a1761 --- /dev/null +++ b/utils/updatedVersion.js @@ -0,0 +1,20 @@ +const generateVersion = require("./utils").generateVersion +const fs = require("fs"); + +function updatedVersion(version) { + let newVersion = generateVersion(version); + const packageJson = JSON.parse(fs.readFileSync("package.json", "utf8")); + const packageLockJson = JSON.parse(fs.readFileSync("package-lock.json", "utf8")); + + packageJson.version = newVersion; + packageLockJson.version = newVersion; + packageLockJson.packages[""].version = newVersion; + + fs.writeFileSync("package.json", JSON.stringify(packageJson, null, 2) + '\n'); + fs.writeFileSync("package-lock.json", JSON.stringify(packageLockJson, null, 2) + '\n'); + +} + +module.exports = { + updatedVersion, +}; diff --git a/utils/utils.js b/utils/utils.js new file mode 100644 index 0000000..8cbf01a --- /dev/null +++ b/utils/utils.js @@ -0,0 +1,24 @@ +const pkg = require("../package.json"); + +function generateVersion(version = pkg.version) { + let maxTag = 9 + let versionA = version.split("."); + let [major, minor, patch] = versionA; + if (Number(patch) !== maxTag) { + patch = parseInt(patch) + 1; + return `${major}.${minor}.${patch}`; + } + if (Number(patch) === maxTag) { + patch = "0"; + minor = parseInt(minor) + 1; + } + if ((Number(patch) === 0 && Number(minor) === (maxTag + 1)) || Number(minor) === maxTag) { + minor = "0"; + major = parseInt(major) + 1; + } + return `${major}.${minor}.${patch}`; +} + +module.exports = { + generateVersion, +};