1+ const { execSync } = require ( 'node:child_process' ) ;
2+ const { readFileSync } = require ( 'node:fs' ) ;
3+ const { join } = require ( 'node:path' ) ;
4+ const { styleText } = require ( 'node:util' ) ;
5+
6+ const packages = [
7+ 'artifacts-common' ,
8+ 'az-blobstorage-provider' ,
9+ 'azure-arm-rest' ,
10+ 'azurermdeploycommon' ,
11+ 'codeanalysis-common' ,
12+ 'codecoverage-tools' ,
13+ 'docker-common' ,
14+ 'ios-signing-common' ,
15+ 'java-common' ,
16+ 'kubernetes-common' ,
17+ 'msbuildhelpers' ,
18+ 'packaging-common' ,
19+ 'securefiles-common' ,
20+ 'utility-common' ,
21+ 'webdeployment-common' ,
22+ ] ;
23+
24+ const gitDiffResult = execSync ( 'git diff --name-only main' , { encoding : 'utf8' } ) . split ( '\n' ) ;
25+ const changedFolders = new Set ( ) ;
26+
27+ for ( const filePath of gitDiffResult ) {
28+ for ( const pkg of packages ) {
29+ // Check if the file path starts with the package folder name
30+ if ( filePath . startsWith ( `common-npm-packages/${ pkg } /` ) ) {
31+ changedFolders . add ( pkg ) ;
32+ }
33+ }
34+ }
35+
36+ if ( changedFolders . size > 0 ) {
37+ const errors = [ ] ;
38+
39+ for ( const pkg of changedFolders ) {
40+ const pkgJsonPath = join ( __dirname , '../common-npm-packages' , pkg , 'package.json' ) ;
41+ try {
42+ const pkgJson = JSON . parse ( readFileSync ( pkgJsonPath , 'utf8' ) ) ;
43+ const currentVersion = pkgJson . version ;
44+ // Get package.json from main branch
45+ let mainVersion = null ;
46+ try {
47+ const mainPkgJsonRaw = execSync ( `git show main:common-npm-packages/${ pkg } /package.json` , { encoding : 'utf8' } ) ;
48+ const mainPkgJson = JSON . parse ( mainPkgJsonRaw ) ;
49+ mainVersion = mainPkgJson . version ;
50+ } catch ( err ) {
51+ // If package.json doesn't exist in main, treat as new package
52+ }
53+
54+ if ( mainVersion && compareVersions ( mainVersion , currentVersion ) >= 0 ) {
55+ errors . push ( {
56+ pkg,
57+ mainVersion,
58+ currentVersion
59+ } ) ;
60+ }
61+ } catch ( err ) {
62+ console . warn ( `Could not read version for ${ pkg } :` , err . message ) ;
63+ }
64+ }
65+ if ( errors . length > 0 ) {
66+ console . error ( styleText ( 'red' , 'Error: The following packages have not been updated correctly:' ) ) ;
67+ errors . forEach ( error => {
68+ console . error ( `Local version ${ styleText ( 'green' , error . currentVersion ) } <= main version ${ styleText ( 'redBright' , error . mainVersion ) } for package ${ styleText ( 'blueBright' , error . pkg ) } .` ) ;
69+ } ) ;
70+ process . exit ( 1 ) ;
71+ }
72+ } else {
73+ console . log ( 'No changed package folders detected.' ) ;
74+ }
75+
76+ /**
77+ * Compare two semver strings. Returns 1 if a > b, 0 if a == b, -1 if a < b
78+ * @param {string } a - The first version string to compare
79+ * @param {string } b - The second version string to compare
80+ * @returns
81+ */
82+ function compareVersions ( a , b ) {
83+ const pa = a . split ( '.' ) . map ( Number ) ;
84+ const pb = b . split ( '.' ) . map ( Number ) ;
85+ for ( let i = 0 ; i < Math . max ( pa . length , pb . length ) ; i ++ ) {
86+ const na = pa [ i ] || 0 ;
87+ const nb = pb [ i ] || 0 ;
88+ if ( na > nb ) return 1 ;
89+ if ( na < nb ) return - 1 ;
90+ }
91+ return 0 ;
92+ }
0 commit comments