1
- /* eslint-disable */
2
- const { readdirSync, existsSync, readFileSync } = require ( 'fs' ) ;
1
+ #!/usr/bin/env node
2
+ const { readdirSync, existsSync, readFileSync, writeFileSync } = require ( 'fs' ) ;
3
3
4
4
const getDirectories = source =>
5
5
readdirSync ( source , { withFileTypes : true } )
@@ -32,11 +32,11 @@ function readPackageJsonNameVersion(filePath) {
32
32
}
33
33
34
34
function compareVersions ( versionsA , versionsB ) {
35
- let output = '' ;
35
+ let output = { } ;
36
36
const newVersions = { ...versionsA } ;
37
37
Object . keys ( versionsB ) . forEach ( dep => {
38
38
if ( versionsA [ dep ] && versionsB [ dep ] && versionsA [ dep ] !== versionsB [ dep ] ) {
39
- output += ` - " ${ dep } " should be " ${ versionsA [ dep ] } " but is " ${ versionsB [ dep ] } "\n` ;
39
+ output [ dep ] = [ versionsA [ dep ] , versionsB [ dep ] ] ;
40
40
}
41
41
if ( ! newVersions [ dep ] ) {
42
42
newVersions [ dep ] = versionsB [ dep ] ;
@@ -53,28 +53,48 @@ let currentVersions = readPackageJsonDeps('./package.json');
53
53
let endReturn = 0 ;
54
54
55
55
// find all versions in the monorepo
56
- [ './packages' , './demo/projects' ] . forEach ( rootDir => {
57
- getDirectories ( rootDir ) . forEach ( subPackage => {
58
- const filePath = ` ${ rootDir } / ${ subPackage } /package.json` ;
59
- currentVersions = { ... currentVersions , ... readPackageJsonNameVersion ( filePath ) } ;
60
- } ) ;
61
- } ) ;
56
+ for ( const subPackage of getDirectories ( './packages' ) ) {
57
+ const filePath = `./packages/ ${ subPackage } /package.json` ;
58
+ currentVersions = { ... currentVersions , ... readPackageJsonNameVersion ( filePath ) } ;
59
+ }
60
+
61
+ const fixes = new Map ( ) ;
62
62
63
63
// lint all versions in packages
64
- [ './packages' , './demo/projects' ] . forEach ( rootDir => {
65
- getDirectories ( rootDir ) . forEach ( subPackage => {
66
- const filePath = `${ rootDir } /${ subPackage } /package.json` ;
67
- const subPackageVersions = readPackageJsonDeps ( filePath ) ;
68
- const { output, newVersions } = compareVersions ( currentVersions , subPackageVersions ) ;
69
- currentVersions = { ...newVersions } ;
70
- if ( output ) {
71
- console . log ( `Version mismatches found in "${ filePath } ":` ) ;
72
- console . log ( output ) ;
73
- console . log ( ) ;
74
- endReturn = 1 ;
75
- }
76
- } ) ;
77
- } ) ;
64
+ for ( const subPackage of getDirectories ( './packages' ) ) {
65
+ const filePath = `./packages/${ subPackage } /package.json` ;
66
+ const subPackageVersions = readPackageJsonDeps ( filePath ) ;
67
+ const { output, newVersions } = compareVersions ( currentVersions , subPackageVersions ) ;
68
+ currentVersions = { ...newVersions } ;
69
+ const entries = Object . entries ( output ) ;
70
+ if ( entries . length ) {
71
+ fixes . set ( filePath , output ) ;
72
+ console . log ( `Version mismatches found in "${ filePath } ":` ) ;
73
+ console . log (
74
+ entries . reduce (
75
+ ( acc , [ dep , [ should , is ] ] ) => `${ acc } - "${ dep } " should be "${ should } " but is "${ is } "\n` ,
76
+ '' ,
77
+ ) ,
78
+ ) ;
79
+ console . log ( ) ;
80
+ endReturn = 1 ;
81
+ }
82
+ }
83
+
84
+ function fixJSON ( filePath , changes ) {
85
+ const json = JSON . parse ( readFileSync ( filePath , 'utf8' ) ) ;
86
+ for ( const [ dep , [ should ] ] of Object . entries ( changes ) ) {
87
+ json . dependencies [ dep ] = should ;
88
+ }
89
+ writeFileSync ( filePath , JSON . stringify ( json , null , 2 ) ) ;
90
+ }
91
+
92
+ if ( fixes . size && process . argv . includes ( '--fix' ) ) {
93
+ for ( const [ filePath , changes ] of fixes ) {
94
+ fixJSON ( filePath , changes ) ;
95
+ }
96
+ console . log ( 'package.json files updated, run `yarn`' ) ;
97
+ }
78
98
79
99
if ( endReturn === 0 ) {
80
100
console . log ( 'All versions are aligned 💪' ) ;
0 commit comments