11import spectral from '@stoplight/spectral-core' ;
22import * as fs from 'node:fs' ;
3+ import { spawnSync } from 'child_process' ;
34import {
45 loadOpenAPIFile ,
56 extractTeamOwnership ,
@@ -9,7 +10,6 @@ import {
910 merge ,
1011} from './utils.js' ;
1112import config from './config.js' ;
12- import collector from './collector.js' ;
1313const { Spectral } = spectral ;
1414
1515async function runMetricCollectionJob ( oasFilePath = config . defaultOasFilePath ) {
@@ -20,22 +20,16 @@ async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) {
2020 console . log ( 'Extracting team ownership data...' ) ;
2121 const ownershipData = extractTeamOwnership ( oasContent ) ;
2222
23- console . log ( 'Initializing Spectral ...' ) ;
23+ console . log ( 'Getting rule severities ...' ) ;
2424 const spectral = new Spectral ( ) ;
2525 const ruleset = await loadRuleset ( config . defaultRulesetFilePath , spectral ) ;
26-
27- console . log ( 'Getting rule severities...' ) ;
2826 const ruleSeverityMap = getSeverityPerRule ( ruleset ) ;
2927
30- console . log ( 'Running Spectral analysis...' ) ;
31- const spectralResults = await spectral . run ( oasContent ) ;
32- collector . flushToFile ( ) ;
33-
3428 console . log ( 'Loading collector results...' ) ;
3529 const collectorResults = loadCollectorResults ( config . defaultCollectorResultsFilePath ) ;
3630
3731 console . log ( 'Merging results...' ) ;
38- const mergedResults = merge ( spectralResults , ownershipData , collectorResults , ruleSeverityMap ) ;
32+ const mergedResults = merge ( ownershipData , collectorResults , ruleSeverityMap ) ;
3933
4034 console . log ( 'Metric collection job complete.' ) ;
4135 return mergedResults ;
@@ -48,6 +42,34 @@ async function runMetricCollectionJob(oasFilePath = config.defaultOasFilePath) {
4842const args = process . argv . slice ( 2 ) ;
4943const customOasFile = args [ 0 ] ;
5044
45+ const result = spawnSync (
46+ 'spectral' ,
47+ [
48+ 'lint' ,
49+ '--ruleset' ,
50+ config . defaultRulesetFilePath ,
51+ '--format' ,
52+ 'stylish' ,
53+ '--verbose' ,
54+ '--format' ,
55+ 'junit' ,
56+ '--output.junit' ,
57+ config . defaultSpectralReportFile ,
58+ config . defaultOasFilePath ,
59+ ] ,
60+ {
61+ encoding : 'utf-8' ,
62+ }
63+ ) ;
64+
65+ if ( result . error ) {
66+ console . error ( 'Error running Spectral lint:' , result . error ) ;
67+ process . exit ( 1 ) ;
68+ }
69+
70+ console . log ( 'Spectral lint completed successfully.' ) ;
71+ fs . writeFileSync ( config . defaultSpectralOutputFile , result . stdout ) ;
72+
5173runMetricCollectionJob ( customOasFile )
5274 . then ( ( results ) => fs . writeFileSync ( config . defaultMetricCollectionResultsFilePath , JSON . stringify ( results ) ) )
5375 . catch ( ( error ) => console . error ( error . message ) ) ;
0 commit comments