2018-08-19 00:44:17 +01:00
#!/usr/bin/env node
const path = require ( "path" ) ;
2018-12-07 14:13:59 +00:00
const fs = require ( "fs-extra" ) ;
2018-08-19 00:44:17 +01:00
const should = require ( "should" ) ;
const rootPackage = require ( path . join ( ".." , "package.json" ) ) ;
const packages = [
"node-red" ,
"@node-red/editor-api" ,
"@node-red/editor-client" ,
"@node-red/nodes" ,
"@node-red/registry" ,
"@node-red/runtime" ,
"@node-red/util"
] ;
2018-12-07 14:13:59 +00:00
const fixFlag = process . argv [ 2 ] === '--fix' ;
2024-05-31 15:16:32 +01:00
async function verifyDependencies ( depType = 'dependencies' ) {
const rootDependencies = rootPackage [ depType ] ;
2018-08-19 00:44:17 +01:00
let failures = [ ] ;
2018-12-07 14:13:59 +00:00
let packageUpdates = { } ;
2018-08-19 00:44:17 +01:00
packages . forEach ( package => {
let modulePackage = require ( path . join ( "../packages/node_modules" , package , "package.json" ) ) ;
2024-05-31 15:16:32 +01:00
let dependencies = Object . keys ( modulePackage [ depType ] || { } ) ;
2018-08-19 00:44:17 +01:00
dependencies . forEach ( module => {
try {
if ( ! /^@node-red\// . test ( module ) ) {
2024-05-31 15:16:32 +01:00
should . exist ( rootDependencies [ module ] , ` [ ${ package } ] ' ${ module } ' missing from root package.json ${ depType } ` ) ;
2018-12-07 14:13:59 +00:00
try {
2024-05-31 15:16:32 +01:00
rootDependencies [ module ] . should . eql ( modulePackage [ depType ] [ module ] , ` [ ${ package } ] ' ${ module } ' version mismatch. Expected ' ${ modulePackage . dependencies [ module ] } ' (got ' ${ rootDependencies [ module ] } ') in ${ depType } ` ) ;
2018-12-07 14:13:59 +00:00
} catch ( err ) {
if ( fixFlag ) {
2024-05-31 15:16:32 +01:00
modulePackage [ depType ] [ module ] = rootDependencies [ module ] ;
2018-12-07 14:13:59 +00:00
packageUpdates [ package ] = modulePackage ;
} else {
failures . push ( err . toString ( ) ) ;
}
}
2018-08-19 00:44:17 +01:00
}
} catch ( err ) {
failures . push ( err . toString ( ) ) ;
}
} ) ;
} )
2018-12-07 14:13:59 +00:00
if ( failures . length === 0 && fixFlag ) {
var promises = [ ] ;
packages . forEach ( package => {
if ( packageUpdates . hasOwnProperty ( package ) ) {
promises . push ( fs . writeJSON ( path . join ( _ _dirname , "../packages/node_modules" , package , "package.json" ) , packageUpdates [ package ] , { spaces : 4 } ) ) ;
}
} ) ;
return Promise . all ( promises ) . then ( r => [ ] ) . catch ( e => {
console . log ( e ) ;
process . exit ( 1 ) ;
} )
} else {
2024-05-31 15:16:32 +01:00
return failures ;
2018-12-07 14:13:59 +00:00
}
2018-08-19 00:44:17 +01:00
}
if ( require . main === module ) {
2024-05-31 15:16:32 +01:00
let failures = [ ]
verifyDependencies ( 'dependencies' ) . then ( depFailures => {
failures = failures . concat ( depFailures )
return verifyDependencies ( 'optionalDependencies' )
} ) . then ( optDepFailures => {
failures = failures . concat ( optDepFailures )
2018-12-07 14:13:59 +00:00
if ( failures . length > 0 ) {
failures . forEach ( f => console . log ( ` - ${ f } ` ) ) ;
2019-01-07 17:03:32 +00:00
console . log ( "Run with --fix option to fix up versions" )
2018-12-07 14:13:59 +00:00
process . exit ( 1 ) ;
}
} ) . catch ( e => {
console . log ( e ) ;
2018-08-19 00:44:17 +01:00
process . exit ( 1 ) ;
2018-12-07 14:13:59 +00:00
} ) ;
2018-08-19 00:44:17 +01:00
} else {
module . exports = verifyDependencies ;
}