1+ #!/usr/bin/env node
2+
3+ import { execSync } from 'child_process' ;
4+ import { readFileSync , writeFileSync } from 'fs' ;
5+
6+ const versionType = process . argv [ 2 ] ;
7+ if ( ! versionType ) {
8+ console . error ( 'Usage: npm run release-custom <major|minor|patch|version>' ) ;
9+ console . error ( 'Examples:' ) ;
10+ console . error ( ' npm run release-custom patch' ) ;
11+ console . error ( ' npm run release-custom minor' ) ;
12+ console . error ( ' npm run release-custom 2.3.3' ) ;
13+ process . exit ( 1 ) ;
14+ }
15+
16+ function incrementVersion ( currentVersion , type ) {
17+ // Clean the version string and split
18+ const cleanVersion = currentVersion . replace ( / ^ v / , '' ) ; // Remove 'v' prefix if present
19+ const parts = cleanVersion . split ( '.' ) ;
20+
21+ if ( parts . length !== 3 ) {
22+ throw new Error ( `Invalid version format: ${ currentVersion } . Expected format: x.y.z` ) ;
23+ }
24+
25+ const [ major , minor , patch ] = parts . map ( part => {
26+ const num = parseInt ( part , 10 ) ;
27+ if ( isNaN ( num ) ) {
28+ throw new Error ( `Invalid version part: ${ part } in version ${ currentVersion } ` ) ;
29+ }
30+ return num ;
31+ } ) ;
32+
33+ console . log ( `π Current version parts: major=${ major } , minor=${ minor } , patch=${ patch } ` ) ;
34+
35+ switch ( type ) {
36+ case 'major' :
37+ return `${ major + 1 } .0.0` ;
38+ case 'minor' :
39+ return `${ major } .${ minor + 1 } .0` ;
40+ case 'patch' :
41+ return `${ major } .${ minor } .${ patch + 1 } ` ;
42+ default :
43+ // Validate specific version format
44+ if ( ! / ^ \d + \. \d + \. \d + $ / . test ( type ) ) {
45+ throw new Error ( `Invalid version format: ${ type } . Use 'major', 'minor', 'patch', or a version like '1.2.3'` ) ;
46+ }
47+ return type ;
48+ }
49+ }
50+
51+ try {
52+ // Read current version
53+ const pkg = JSON . parse ( readFileSync ( 'package.json' , 'utf8' ) ) ;
54+ const currentVersion = pkg . version ;
55+ const newVersion = incrementVersion ( currentVersion , versionType ) ;
56+
57+ console . log ( `π¦ Updating version from ${ currentVersion } to ${ newVersion } ` ) ;
58+
59+ // Update package.json version
60+ pkg . version = newVersion ;
61+ writeFileSync ( 'package.json' , JSON . stringify ( pkg , null , 2 ) + '\n' ) ;
62+
63+ // Update package-lock.json version
64+ console . log ( 'π Updating package-lock.json...' ) ;
65+ execSync ( 'npm install --package-lock-only' , { stdio : 'inherit' } ) ;
66+
67+ // Build project
68+ console . log ( 'π¨ Building project...' ) ;
69+ execSync ( 'npm run build' , { stdio : 'inherit' } ) ;
70+
71+ // Update manifest versions
72+ console . log ( 'π Updating manifest files...' ) ;
73+ const updateManifest = ( file ) => {
74+ const manifest = JSON . parse ( readFileSync ( file , 'utf8' ) ) ;
75+ manifest . version = newVersion ;
76+ writeFileSync ( file , JSON . stringify ( manifest , null , 2 ) + '\n' ) ;
77+ } ;
78+
79+ updateManifest ( 'manifest-full.json' ) ;
80+ updateManifest ( 'manifest-minimal.json' ) ;
81+
82+ // Commit and tag
83+ console . log ( 'π€ Committing and tagging...' ) ;
84+ execSync ( 'git add .' , { stdio : 'inherit' } ) ;
85+ execSync ( `git commit -m "chore: v${ newVersion } "` , { stdio : 'inherit' } ) ;
86+ execSync ( `git tag v${ newVersion } ` , { stdio : 'inherit' } ) ;
87+
88+ console . log ( `β
Released version ${ newVersion } ` ) ;
89+ console . log ( `π Push with: git push origin main --tags` ) ;
90+ } catch ( error ) {
91+ console . error ( 'β Release failed:' , error . message ) ;
92+ process . exit ( 1 ) ;
93+ }
0 commit comments