File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #!/usr/bin/env node
2+ // Syncs config.xml's version/versionCode with package.json's version.
3+ // Invoked automatically by `npm version <major|minor|patch>` via the "version" npm script.
4+ const fs = require ( 'fs' ) ;
5+ const path = require ( 'path' ) ;
6+
7+ const rootDir = path . join ( __dirname , '..' ) ;
8+ const pkg = require ( path . join ( rootDir , 'package.json' ) ) ;
9+ const configPath = path . join ( rootDir , 'config.xml' ) ;
10+
11+ let config = fs . readFileSync ( configPath , 'utf8' ) ;
12+
13+ const versionCodeMatch = config . match ( / a n d r o i d - v e r s i o n C o d e = " ( \d + ) " / ) ;
14+ if ( ! versionCodeMatch ) {
15+ throw new Error ( 'Could not find android-versionCode in config.xml' ) ;
16+ }
17+ const newVersionCode = String ( Number ( versionCodeMatch [ 1 ] ) + 1 ) ;
18+
19+ config = config
20+ . replace ( / a n d r o i d - v e r s i o n C o d e = " \d + " / , `android-versionCode="${ newVersionCode } "` )
21+ . replace ( / i o s - C F B u n d l e V e r s i o n = " \d + " / , `ios-CFBundleVersion="${ newVersionCode } "` )
22+ . replace ( / v e r s i o n = " [ ^ " ] + " / , `version="${ pkg . version } "` ) ;
23+
24+ fs . writeFileSync ( configPath , config ) ;
25+ console . log ( `Updated config.xml to version ${ pkg . version } (versionCode ${ newVersionCode } )` ) ;
Original file line number Diff line number Diff line change 1919 "build-prod-android" : " npm run export-prod && npx cordova build android" ,
2020 "build-prod-ios" : " npm run export-prod && npx cordova build ios" ,
2121 "build-prod-android-release" : " npm run export-prod && npx cordova build android --release" ,
22- "test" : " npx jest"
22+ "test" : " npx jest" ,
23+ "version" : " node bin/sync_config_version.js && git add config.xml"
2324 },
2425 "devDependencies" : {
2526 "@babel/core" : " ^7.21.3" ,
You canβt perform that action at this time.
0 commit comments