1+ import * as fs from 'fs' ;
2+ import * as Contracts from './contracts' ;
3+ import * as Helpers from './helpers' ;
4+
5+ export default class Cleanup {
6+
7+ constructor ( private config : Contracts . Config ) { }
8+
9+ public Clean ( ) {
10+ let packageJSON = this . readPackageJSON ( ) ;
11+ let modifiedPackageJSON = Helpers . Clone ( packageJSON ) ;
12+ let excluded = false ;
13+
14+ if ( this . config . exclude != null ) {
15+ modifiedPackageJSON = this . exclude ( modifiedPackageJSON ) ;
16+ excluded = true ;
17+ }
18+ if ( this . config . include != null ) {
19+ modifiedPackageJSON = this . include ( packageJSON , modifiedPackageJSON , excluded ) ;
20+ }
21+
22+ if ( this . config . backup != null && this . config . backup ) {
23+ this . writePackageJSON ( packageJSON , 'package.bak.json' ) ;
24+ }
25+
26+ if ( this . config . writeChanges != null && this . config . writeChanges || this . config . writeChanges == null ) {
27+ this . writePackageJSON ( modifiedPackageJSON , 'package.json' ) ;
28+ }
29+
30+ return modifiedPackageJSON ;
31+ }
32+
33+ private exclude ( packageJSON : Contracts . PackageJSONSkeleton ) {
34+ let excludeConfig = this . config . exclude ! ;
35+ for ( let key in excludeConfig ) {
36+ if ( this . configItemAll ( excludeConfig [ key ] ) ) {
37+ delete packageJSON [ key ] ;
38+ } else {
39+ excludeConfig [ key ] . forEach ( ( value ) => {
40+ let item = packageJSON [ key ] ;
41+
42+ if ( Array . isArray ( item ) ) {
43+ item = item as Array < string > ;
44+ packageJSON [ key ] = item . filter ( x => x !== value ) ;
45+ } else if ( typeof item === 'object' ) {
46+ item = item as Contracts . PackageJSONSkeletonDictionary ;
47+ for ( let pkey in item ) {
48+ if ( pkey === value ) {
49+ delete item [ pkey ] ;
50+ }
51+ }
52+ } else {
53+ console . warn ( `[Warning] ${ key } is not found in package.json.` ) ;
54+ }
55+ } ) ;
56+ }
57+ }
58+
59+ return packageJSON ;
60+ }
61+
62+ private include ( packageJSON : Contracts . PackageJSONSkeleton , modifiedPackageJSON : Contracts . PackageJSONSkeleton , excluded : boolean ) {
63+ let includeConfig = this . config . include ! ;
64+ // Include only listed in Config.
65+ if ( ! excluded ) {
66+ modifiedPackageJSON = { }
67+ }
68+ for ( let key in includeConfig ) {
69+
70+ if ( this . configItemAll ( includeConfig [ key ] ) ) {
71+ modifiedPackageJSON [ key ] = packageJSON [ key ] ;
72+ } else {
73+ includeConfig [ key ] . forEach ( ( value ) => {
74+ let item = packageJSON [ key ] ;
75+ if ( Array . isArray ( item ) ) {
76+ item = item as Array < string > ;
77+ let modifiedItem = modifiedPackageJSON [ key ] as Array < string > ;
78+ if ( modifiedItem == null ) {
79+ modifiedItem = new Array < string > ( ) ;
80+ }
81+ if ( modifiedItem . indexOf ( value ) === - 1 ) {
82+ modifiedItem . push ( value ) ;
83+ }
84+
85+ modifiedPackageJSON [ key ] = modifiedItem ;
86+ } else if ( typeof item === 'object' ) {
87+ item = item as Contracts . PackageJSONSkeletonDictionary ;
88+
89+ if ( packageJSON [ key ] [ value ] != null ) {
90+ modifiedPackageJSON [ key ] [ value ] = packageJSON [ key ] [ value ] ;
91+ }
92+ } else {
93+ console . warn ( `[Warning] ${ key } is not found in package.json.` ) ;
94+ }
95+ } ) ;
96+ }
97+ }
98+
99+ return modifiedPackageJSON ;
100+ }
101+
102+ private configItemAll ( itemSettings : Array < string > ) : boolean {
103+ return ( itemSettings . length > 0 && itemSettings [ 0 ] === '*' ) ;
104+ }
105+
106+ private readPackageJSON ( fileName : string = 'package.json' ) : Contracts . PackageJSONSkeleton {
107+ return JSON . parse ( fs . readFileSync ( fileName , 'utf8' ) ) ;
108+ }
109+
110+ private writePackageJSON ( packageJSON : Contracts . PackageJSONSkeleton , file : string ) {
111+ let data = JSON . stringify ( packageJSON , null , 4 ) ;
112+ fs . writeFileSync ( file , data ) ;
113+ }
114+ }
0 commit comments