@@ -98,8 +98,10 @@ class LayerDependencyAnalysis {
9898 this . comparator = comparator ;
9999 }
100100
101- run ( layerPackageJson , domainPackageJsons ) {
102- console . log ( 'Running layer dependency analysis' ) ;
101+ run ( layerPackageJson , domainPackageJsons , outputFormat = 'console' ) {
102+ if ( outputFormat !== 'json' ) {
103+ console . log ( 'Running layer dependency analysis' ) ;
104+ }
103105
104106 const reports = domainPackageJsons . map ( domainPackageJson => {
105107 return {
@@ -110,36 +112,66 @@ class LayerDependencyAnalysis {
110112
111113 const reportsWithWarnings = reports . filter ( report => report . report . mismatches . length > 0 ) ;
112114
113- if ( reportsWithWarnings . length > 0 ) {
114- console . log ( 'Reports with mismatched dependencies:' ) ;
115- reportsWithWarnings . forEach ( report => {
116- // output warning to github actions
117- console . log ( `::warning file=${ report . project } /package.json::${ this . comparator . formatReport ( report . report ) } ` ) ;
118- } ) ;
115+ if ( outputFormat === 'json' ) {
116+ // Output structured JSON data for piping to other scripts
117+ const jsonOutput = {
118+ timestamp : new Date ( ) . toISOString ( ) ,
119+ layerPackageJson : layerPackageJson . name || 'unknown' ,
120+ totalDomainsAnalyzed : domainPackageJsons . length ,
121+ domainsWithMismatches : reportsWithWarnings . length ,
122+ reports : reports . map ( report => ( {
123+ project : report . project ,
124+ hasMismatches : report . report . mismatches . length > 0 ,
125+ mismatches : report . report . mismatches ,
126+ missingInFirst : report . report . missingInFirst ,
127+ missingInSecond : report . report . missingInSecond ,
128+ formattedReport : this . comparator . formatReport ( report . report )
129+ } ) )
130+ } ;
131+
132+ console . log ( JSON . stringify ( jsonOutput , null , 2 ) ) ;
119133 } else {
120- console . log ( 'No mismatched dependencies found' ) ;
134+ // Original console output behavior
135+ if ( reportsWithWarnings . length > 0 ) {
136+ console . log ( 'Reports with mismatched dependencies:' ) ;
137+ reportsWithWarnings . forEach ( report => {
138+ // output warning to github actions
139+ console . log ( `::warning file=${ report . project } /package.json::${ this . comparator . formatReport ( report . report ) } ` ) ;
140+ } ) ;
141+ } else {
142+ console . log ( 'No mismatched dependencies found' ) ;
143+ }
121144 }
122145 }
123146}
124147
125148function main ( ) {
126- if ( process . argv . length < 4 ) {
127- console . error ( 'Usage: node layer_dependency_analysis.js <layer-package-json> <domains>' ) ;
149+ const args = process . argv . slice ( 2 ) ;
150+ const jsonOutput = args . includes ( '--json' ) ;
151+ const filteredArgs = args . filter ( arg => arg !== '--json' ) ;
152+
153+ if ( filteredArgs . length < 2 ) {
154+ console . error ( 'Usage: node layer_dependency_analysis.js [--json] <layer-package-json> <domains>' ) ;
155+ console . error ( ' --json: Output structured JSON data instead of console warnings' ) ;
128156 process . exit ( 1 ) ;
129157 }
130158
131- console . log ( 'Layer package.json:' , process . argv [ 2 ] ) ;
132- console . log ( 'Domains:' , process . argv [ 3 ] ) ;
133- console . log ( 'Current working directory:' , process . cwd ( ) ) ;
159+ if ( ! jsonOutput ) {
160+ console . log ( 'Layer package.json:' , filteredArgs [ 0 ] ) ;
161+ console . log ( 'Domains:' , filteredArgs [ 1 ] ) ;
162+ console . log ( 'Current working directory:' , process . cwd ( ) ) ;
163+ }
134164
135165 // Example usage:
136166 const comparator = new PackageJsonDependencyComparator ( ) ;
137167
138168 const layerDependencyAnalysis = new LayerDependencyAnalysis ( comparator ) ;
139169
140- console . log ( 'Reading layer package.json:' , process . cwd ( ) + '/' + process . argv [ 2 ] ) ;
141- const layerPackageJson = JSON . parse ( fs . readFileSync ( process . cwd ( ) + '/' + process . argv [ 2 ] , 'utf8' ) ) ;
142- const domains = JSON . parse ( process . argv [ 3 ] ) ; // {"include": [{"project": "domain1"}, {"project": "domain2"}] }
170+ if ( ! jsonOutput ) {
171+ console . log ( 'Reading layer package.json:' , process . cwd ( ) + '/' + filteredArgs [ 0 ] ) ;
172+ }
173+ const layerPackageJson = JSON . parse ( fs . readFileSync ( process . cwd ( ) + '/' + filteredArgs [ 0 ] , 'utf8' ) ) ;
174+ const domains = JSON . parse ( filteredArgs [ 1 ] ) ; // {"include": [{"project": "domain1"}, {"project": "domain2"}] }
143175
144176 const domainPackageJsons = domains . include . filter ( domain => domain . project != '.' ) . map ( domain => {
145177 try {
@@ -149,18 +181,22 @@ function main() {
149181 packageJson
150182 } ;
151183 } catch ( error ) {
152- console . error ( 'Error parsing package.json for domain:' , domain . project , error ) ;
184+ if ( ! jsonOutput ) {
185+ console . error ( 'Error parsing package.json for domain:' , domain . project , error ) ;
186+ }
153187 return undefined ;
154188 }
155189 } ) . filter ( domain => domain !== undefined ) ;
156190
157191 // print the test plan
158- console . log ( 'Test plan:' ) ;
159- console . log ( 'Layer package.json:' , layerPackageJson ) ;
160- console . log ( 'Domains:' , domains ) ;
161- // console.log('Domain package.json:', JSON.stringify(domainPackageJsons, null, 2));
192+ if ( ! jsonOutput ) {
193+ console . log ( 'Test plan:' ) ;
194+ console . log ( 'Layer package.json:' , layerPackageJson ) ;
195+ console . log ( 'Domains:' , domains ) ;
196+ // console.log('Domain package.json:', JSON.stringify(domainPackageJsons, null, 2));
197+ }
162198
163- layerDependencyAnalysis . run ( layerPackageJson , domainPackageJsons ) ;
199+ layerDependencyAnalysis . run ( layerPackageJson , domainPackageJsons , jsonOutput ? 'json' : 'console' ) ;
164200}
165201
166202main ( ) ;
0 commit comments