@@ -12,7 +12,7 @@ const path = require('path');
12
12
const fs = require ( 'fs-extra' ) ;
13
13
const paths = require ( '../../config/paths' ) ;
14
14
15
- function cleanBuildDirectory ( ) {
15
+ function cleanBuildDirectory ( cleanMetaFiles = false ) {
16
16
try {
17
17
if ( fs . existsSync ( paths . appManifest ) ) {
18
18
// Clean only the files inside the asset-manifest so that we avoid cleaning files belonging to user
@@ -21,26 +21,55 @@ function cleanBuildDirectory() {
21
21
22
22
const keys = Object . keys ( manifest . files ) ;
23
23
24
+ const metaFiles = [ ] ;
25
+
24
26
for ( const key of keys ) {
25
27
const file = manifest . files [ key ] ;
26
28
const filePath = path . join ( paths . appBuild , file ) ;
27
29
const metaFile = filePath + '.meta' ;
28
30
29
31
fs . removeSync ( filePath ) ;
30
- fs . removeSync ( metaFile ) ;
32
+
33
+ if ( fs . existsSync ( metaFile ) ) {
34
+ if ( cleanMetaFiles ) fs . removeSync ( metaFile ) ;
35
+ else metaFiles . push ( metaFile ) ;
36
+ }
31
37
}
32
38
33
39
const manifestMeta = paths . appManifest + '.meta' ;
34
40
fs . removeSync ( paths . appManifest ) ;
35
- fs . removeSync ( manifestMeta ) ;
41
+
42
+ if ( fs . existsSync ( manifestMeta ) ) {
43
+ if ( cleanMetaFiles ) fs . removeSync ( manifestMeta ) ;
44
+ else metaFiles . push ( manifestMeta ) ;
45
+ }
36
46
37
47
cleanEmptyFoldersRecursively ( null , paths . appBuild , true ) ;
38
48
39
- return ;
49
+ return metaFiles ;
40
50
}
41
- } catch ( err ) { }
51
+ } catch ( err ) {
52
+ console . log ( 'Skipped clearing output directory because asset manifest was not found. Please clean the output directory manually if necessary.' ) ;
53
+ return [ ] ;
54
+ }
55
+ }
42
56
43
- console . log ( 'Skipped clearing output directory because asset manifest was not found. Please clean the output directory manually if necessary.' ) ;
57
+ function cleanUnusedMetaFiles ( metaFiles ) {
58
+ try {
59
+ for ( const metaFile of metaFiles ) {
60
+ try {
61
+ if ( ! fs . existsSync ( metaFile ) ) continue ;
62
+
63
+ const nonMetaFile = metaFile . replace ( / \. m e t a $ / , '' ) ;
64
+ if ( fs . existsSync ( nonMetaFile ) ) continue ;
65
+
66
+ fs . removeSync ( metaFile ) ;
67
+ } catch ( err ) {
68
+ console . log ( 'Error while cleaning meta file: ' + metaFile ) ;
69
+ }
70
+ }
71
+ } catch ( err ) {
72
+ }
44
73
}
45
74
46
75
@@ -74,3 +103,4 @@ function cleanEmptyFoldersRecursively(parent, folder, skipFirst) {
74
103
}
75
104
76
105
module . exports . cleanBuildDirectory = cleanBuildDirectory ;
106
+ module . exports . cleanUnusedMetaFiles = cleanUnusedMetaFiles ;
0 commit comments