@@ -2,26 +2,30 @@ import path from 'path';
22import chalk from 'chalk' ;
33import { get , set } from 'lodash' ;
44import getFiles from './lib/get-files' ;
5+ import writeJson from './lib/write-json' ;
56
67const getPackage = location => ( { location, json : require ( location ) } ) ;
7- const formatValue = value => JSON . stringify ( value ) ;
8+ const toJson = value => JSON . stringify ( value ) ;
9+
10+ const reportChange = ( key , previous , next ) => {
11+ if ( toJson ( previous ) === toJson ( next ) ) {
12+ console . log ( `${ key } : ${ chalk . green ( '✓ unchanged' ) } ` ) ;
13+ } else {
14+ console . log ( `${ key } : ${ chalk . red ( toJson ( previous ) ) } → ${ chalk . green ( toJson ( next ) ) } ` ) ;
15+ }
16+ } ;
817
918export default async ( { keys, packagesPattern, sourcePattern } ) => {
1019 const [ source ] = ( await getFiles ( sourcePattern ) ) . map ( getPackage ) ;
1120 const packages = ( await getFiles ( packagesPattern ) ) . map ( getPackage ) ;
12-
1321 packages . forEach ( pkg => {
1422 console . log ( chalk . grey . underline ( path . relative ( process . cwd ( ) , pkg . location ) ) ) ;
1523 keys . forEach ( key => {
1624 const value = get ( source . json , key ) ;
17- const previousValue = formatValue ( get ( pkg . json , key ) ) ;
25+ const previousValue = get ( pkg . json , key ) ;
1826 set ( pkg . json , key , value ) ;
19- const nextValue = formatValue ( value ) ;
20- if ( previousValue === nextValue ) {
21- console . log ( `${ key } : ${ chalk . green ( '✓ unchanged' ) } ` ) ;
22- } else {
23- console . log ( `${ key } : ${ chalk . red ( previousValue ) } → ${ chalk . green ( nextValue ) } ` ) ;
24- }
27+ writeJson ( pkg . location , pkg . json ) ;
28+ reportChange ( key , previousValue , value ) ;
2529 } ) ;
2630 } ) ;
2731} ;
0 commit comments