11#!/usr/bin/env node
22
33/**
4- * This runs after `npm version` command.
5- * This will update the `package.json` optional native dependencies
4+ * This runs after `npm version` command updates the version but before changes are commited.
5+ * This will update the `cargo.toml` version to match the new `package.json` verson.
6+ * This will also update the `package.json` optional native dependencies
67 * to match the same version as the version of this package.
78 * This maintains the same version between this master package
89 * and the optional native dependencies.
1213 * to prevent `npm` from attempting to download unpublished packages.
1314 */
1415
16+ const process = require ( 'process' ) ;
17+ const path = require ( 'path' ) ;
18+ const fs = require ( 'fs' ) ;
1519const os = require ( 'os' ) ;
1620const childProcess = require ( 'child_process' ) ;
1721const packageJSON = require ( '../package.json' ) ;
@@ -20,6 +24,27 @@ const platform = os.platform();
2024
2125/* eslint-disable no-console */
2226async function main ( ) {
27+
28+ console . error (
29+ 'Updating the cargo.toml version to match new version' ,
30+ ) ;
31+ const projectRoot = path . join ( __dirname , '..' ) ;
32+ const cargoTOMLPath = path . join ( projectRoot , 'Cargo.toml' ) ;
33+ const cargoTOML = await fs . promises . readFile ( cargoTOMLPath , 'utf-8' ) ;
34+ const cargoTOMLMatch = cargoTOML . match ( / v e r s i o n \s * = \s * " ( .* ) " / ) ;
35+ const cargoTOMLUpdated = cargoTOML . replace ( cargoTOMLMatch [ 0 ] , `version = "${ packageJSON . version } "` ) ;
36+ await fs . promises . writeFile ( cargoTOMLPath , cargoTOMLUpdated , 'utf-8' ) ;
37+
38+ console . error (
39+ 'Staging changes in git' ,
40+ ) ;
41+ childProcess . execFileSync ( 'git' , [ 'add' , cargoTOMLPath ] , {
42+ stdio : [ 'inherit' , 'inherit' , 'inherit' ] ,
43+ windowsHide : true ,
44+ encoding : 'utf-8' ,
45+ shell : platform === 'win32' ? true : false ,
46+ } ) ;
47+
2348 console . error (
2449 'Updating the package.json with optional native dependencies and package-lock.json' ,
2550 ) ;
0 commit comments