diff --git a/.husky/pre-commit b/.husky/pre-commit new file mode 100644 index 00000000..243c5c89 --- /dev/null +++ b/.husky/pre-commit @@ -0,0 +1 @@ +node build/update-version.js --stage commit diff --git a/.auto-changelog/config.json b/build/changelog/config.json similarity index 76% rename from .auto-changelog/config.json rename to build/changelog/config.json index 2cba6795..cc55ed95 100644 --- a/.auto-changelog/config.json +++ b/build/changelog/config.json @@ -1,10 +1,10 @@ { "output": "CHANGELOG.md", - "template": ".auto-changelog/template.hbs", - "handlebarsSetup": ".auto-changelog/setup.js", + "template": "build/changelog/template.hbs", + "handlebarsSetup": "build/changelog/setup.cjs", "sortCommits": "relevance", "commitLimit": false, - "ignoreCommitPattern": "\\(ignore\\)", + "ignoreCommitPattern": "(Chore|chore):|\\(ignore\\)", "replaceText": { "^(Feat|feat):": ":sparkles: Feat:", "^(Fix|fix):": ":bug: Fix:", diff --git a/.auto-changelog/setup.js b/build/changelog/setup.cjs similarity index 100% rename from .auto-changelog/setup.js rename to build/changelog/setup.cjs diff --git a/.auto-changelog/template.hbs b/build/changelog/template.hbs similarity index 100% rename from .auto-changelog/template.hbs rename to build/changelog/template.hbs diff --git a/build/update-version.js b/build/update-version.js new file mode 100644 index 00000000..b18a2114 --- /dev/null +++ b/build/update-version.js @@ -0,0 +1,46 @@ +import fs from 'fs'; +import { dirname, join } from 'path' +import { fileURLToPath } from 'url' +import { execSync } from 'child_process'; + +// node build/update-version.js --stage [stage] +const stage = process.argv[3] || 'commit'; +const match = [ + 'archetypes/', + 'assets/', + 'i18n/', + 'layouts/', + 'static/', + 'go.mod', + 'hugo.toml', + 'package.json', + 'package-lock.json', + 'theme.toml', +]; +const gitDiff = execSync('git diff --cached --name-only').toString().trim(); +if (stage !== 'version' && !match.some((item) => gitDiff.includes(item))) { + // console.log('No need to update the FixIt version.'); + process.exit(0); +} + +const __root = join(dirname(fileURLToPath(import.meta.url)), '../'); +const initHtmlPath = join(__root, 'layouts/partials/init/index.html'); +const packageJsonPath = join(__root, 'package.json'); +const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); +const version = packageJson.version; +// Get the short hash of the last commit (can not get this commit hash at pre-commit hook) +const shortHash = execSync('git rev-parse --short HEAD').toString().trim(); +// Build the development version v{major}.{minor}.{patch+1}-{shortHash} +const devVersion = `${version.replace(/(\d+)$/, (match, part) => parseInt(part) + 1)}-${shortHash}`; + +// Update the version number in layouts/partials/init/index.html +const initHtml = fs.readFileSync(initHtmlPath, 'utf8'); +const latestVersion = stage === 'version' ? version : devVersion; +const lastVersion = initHtml.match(/v\d+\.\d+\.\d+(-\w+)?/)[0]; +const newInitHtml = initHtml.replace(/v\d+\.\d+\.\d+(-\w+)?/, `v${latestVersion}`); +fs.writeFileSync(initHtmlPath, newInitHtml); +// Add the updated file to the git stage +execSync('git add .'); +console.log(`Update the FixIt version from ${lastVersion} to v${latestVersion}.`); + +export default latestVersion; diff --git a/layouts/partials/init/index.html b/layouts/partials/init/index.html index 75e9d7af..95ed7e76 100644 --- a/layouts/partials/init/index.html +++ b/layouts/partials/init/index.html @@ -1,4 +1,4 @@ -{{- .Scratch.Set "version" "v0.3.9" -}} +{{- .Scratch.Set "version" "v0.3.10-108512c2" -}} {{- .Scratch.Set "this" dict -}} {{- partial "init/detection-env.html" . -}} diff --git a/package-lock.json b/package-lock.json index f14044da..a1c369b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,8 @@ "version": "0.3.9", "license": "MIT", "devDependencies": { - "auto-changelog": "^2.4.0" + "auto-changelog": "^2.4.0", + "husky": "^9.1.4" } }, "node_modules/auto-changelog": { @@ -61,6 +62,21 @@ "uglify-js": "^3.1.4" } }, + "node_modules/husky": { + "version": "9.1.4", + "resolved": "https://registry.npmjs.org/husky/-/husky-9.1.4.tgz", + "integrity": "sha512-bho94YyReb4JV7LYWRWxZ/xr6TtOTt8cMfmQ39MQYJ7f/YE268s3GdghGwi+y4zAeqewE5zYLvuhV0M0ijsDEA==", + "dev": true, + "bin": { + "husky": "bin.js" + }, + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sponsors/typicode" + } + }, "node_modules/lru-cache": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", @@ -225,6 +241,12 @@ "wordwrap": "^1.0.0" } }, + "husky": { + "version": "9.1.4", + "resolved": "https://registry.npmjs.org/husky/-/husky-9.1.4.tgz", + "integrity": "sha512-bho94YyReb4JV7LYWRWxZ/xr6TtOTt8cMfmQ39MQYJ7f/YE268s3GdghGwi+y4zAeqewE5zYLvuhV0M0ijsDEA==", + "dev": true + }, "lru-cache": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", diff --git a/package.json b/package.json index 5a946696..60f5e9d8 100644 --- a/package.json +++ b/package.json @@ -18,16 +18,19 @@ }, "license": "MIT", "author": "Lruihao (https://lruihao.cn)", + "type": "module", "scripts": { "build": "hugo --source=demo --themesDir ../.. --theme $PWD --gc --minify --logLevel info", "server": "hugo server --source=demo --themesDir ../.. --theme $PWD -D --disableFastRender --navigateToChanged --ignoreCache", "server:production": "npm run server -- -e production", "server:docs": "hugo server --source=../fixit-docs -D --disableFastRender --navigateToChanged --ignoreCache", - "version": "sed -i '' \"s/v$npm_package_version-RC/v$npm_package_version/g\" layouts/partials/init/index.html && git add .", + "version": "node build/update-version.js --stage version", "postversion": "git push && git push --tags && npm run release", - "release": "auto-changelog --config .auto-changelog/config.json" + "release": "auto-changelog --config build/changelog/config.json", + "prepare": "husky" }, "devDependencies": { - "auto-changelog": "^2.4.0" + "auto-changelog": "^2.4.0", + "husky": "^9.1.4" } }