@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) • 1.74 kB
JavaScript
import{execSync as t}from"node:child_process";import{consola as l}from"consola";import{readPackageJSON as d,resolvePackageJSON as w,writePackageJSON as v}from"pkg-types";import s from"semver";import{l as u}from"./basis.BrlVPuDi.mjs";function h(e,o,m){if(o.version){if(s.valid(o.version))return o.version;throw new Error(`Invalid version format: ${o.version}. Please use semantic versioning format (e.g., 1.0.0, 2.1.0-alpha.1)`)}if(!s.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=s.prerelease(e),c=o.preid||(r&&typeof r[0]=="string"?r[0]:null)||m.prereleaseId||"edge";let a;o.major?a="major":o.minor?a="minor":o.prerelease?a=r?"prerelease":"prepatch":a=r?"prerelease":"patch";const i=a==="prerelease"||a.startsWith("pre")?s.inc(e,a,c):s.inc(e,a);if(!i)throw new Error(`Failed to calculate new version from ${e}. Please check your version increment options.`);return i}async function P(e,o={}){const{config:m}=await u({cwd:e}),r=m.version||{},c=await d(e),a=c.version;if(!a)throw new Error("No version found in package.json");const i=h(a,o,r),f=await w(e);await v(f,{...c,version:i});const p={oldVersion:a,newVersion:i};if(r.autoCommit){const n=o.message||r.commitMessage?.replace("{version}",i)||`chore: release v${i}`;try{t("git add package.json",{cwd:e}),t(`git commit -m "${n}"`,{cwd:e})}catch(g){l.warn("Failed to commit changes:",g)}}if(r.autoTag){const n=`${r.tagPrefix||"v"}${i}`;try{t(`git tag ${n}`,{cwd:e}),p.tagName=n}catch(g){l.warn("Failed to create git tag:",g)}}if(r.autoPush)try{t("git push",{cwd:e}),r.autoTag&&t("git push --tags",{cwd:e})}catch(n){l.warn("Failed to push changes:",n)}return p}export{P as u};