@funish/basis
Version:
A unified development toolkit with CLI for package management, versioning, publishing, linting, and git hooks management for JavaScript/TypeScript projects.
2 lines (1 loc) • 2.18 kB
JavaScript
;const node_child_process=require("node:child_process"),consola=require("consola"),pkgTypes=require("pkg-types"),n=require("semver"),utils=require("./basis.D3fInv-P.cjs");function _interopDefaultCompat(e){return e&&typeof e=="object"&&"default"in e?e.default:e}const n__default=_interopDefaultCompat(n);function P(e,a,c){if(a.version){if(n__default.valid(a.version))return a.version;throw new Error(`Invalid version format: ${a.version}. Please use semantic versioning format (e.g., 1.0.0, 2.1.0-alpha.1)`)}if(!n__default.valid(e))throw new Error(`Invalid current version format: ${e}. Please fix version in package.json to use semantic versioning format (e.g., 1.0.0)`);const r=n__default.prerelease(e),t=a.preid||(r&&typeof r[0]=="string"?r[0]:null)||c.prereleaseId||"edge";let o;a.major?o="major":a.minor?o="minor":a.prerelease?o=r?"prerelease":"prepatch":o=r?"prerelease":"patch";const i=o==="prerelease"||o.startsWith("pre")?n__default.inc(e,o,t):n__default.inc(e,o);if(!i)throw new Error(`Failed to calculate new version from ${e}. Please check your version increment options.`);return i}async function updatePackageVersion(e,a={}){const{config:c}=await utils.loadConfig({cwd:e}),r=c.version||{},t=await pkgTypes.readPackageJSON(e),o=t.version;if(!o)throw new Error("No version found in package.json");const i=P(o,a,r),u=await pkgTypes.resolvePackageJSON(e);await pkgTypes.writePackageJSON(u,{...t,version:i});const g={oldVersion:o,newVersion:i};if(r.autoCommit){const s=a.message||r.commitMessage?.replace("{version}",i)||`chore: release v${i}`;try{node_child_process.execSync("git add package.json",{cwd:e}),node_child_process.execSync(`git commit -m "${s}"`,{cwd:e})}catch(l){consola.consola.warn("Failed to commit changes:",l)}}if(r.autoTag){const s=`${r.tagPrefix||"v"}${i}`;try{node_child_process.execSync(`git tag ${s}`,{cwd:e}),g.tagName=s}catch(l){consola.consola.warn("Failed to create git tag:",l)}}if(r.autoPush)try{node_child_process.execSync("git push",{cwd:e}),r.autoTag&&node_child_process.execSync("git push --tags",{cwd:e})}catch(s){consola.consola.warn("Failed to push changes:",s)}return g}exports.updatePackageVersion=updatePackageVersion;