@reactunity/scripts 0.9.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ // @remove-on-eject-begin
2
+ /**
3
+ * Copyright (c) 2015-present, Facebook, Inc.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ // @remove-on-eject-end
9
+ 'use strict';
10
+
11
+ const path = require('path');
12
+ const fs = require('fs-extra');
13
+ const paths = require('../../config/paths');
14
+
15
+ function cleanBuildDirectory() {
16
+ try {
17
+ if (fs.existsSync(paths.appManifest)) {
18
+ // Clean only the files inside the asset-manifest so that we avoid cleaning files belonging to user
19
+ const manifestContent = fs.readFileSync(paths.appManifest, 'utf8');
20
+ const manifest = JSON.parse(manifestContent);
21
+
22
+ const keys = Object.keys(manifest.files);
23
+
24
+ for (const key of keys) {
25
+ const file = manifest.files[key];
26
+ const filePath = path.join(paths.appBuild, file);
27
+ const metaFile = filePath + '.meta';
28
+
29
+ fs.removeSync(filePath);
30
+ fs.removeSync(metaFile);
31
+ }
32
+
33
+ const manifestMeta = paths.appManifest + '.meta';
34
+ fs.removeSync(paths.appManifest);
35
+ fs.removeSync(manifestMeta);
36
+
37
+ cleanEmptyFoldersRecursively(null, paths.appBuild, true);
38
+
39
+ return;
40
+ }
41
+ } catch (err) { }
42
+
43
+ console.log('Skipped clearing output directory because asset manifest was not found. Please clean the output directory manually if necessary.');
44
+ }
45
+
46
+
47
+ function cleanEmptyFoldersRecursively(parent, folder, skipFirst) {
48
+ if (!fs.existsSync(folder)) return;
49
+ var isDir = fs.statSync(folder).isDirectory();
50
+ if (!isDir) return;
51
+
52
+ var files = fs.readdirSync(folder);
53
+ if (files.length > 0) {
54
+ files.forEach(function (file) {
55
+ var fullPath = path.join(folder, file);
56
+ cleanEmptyFoldersRecursively(folder, fullPath, false);
57
+ });
58
+
59
+ // re-evaluate files; after deleting subfolder
60
+ // we may have parent folder empty now
61
+ files = fs.readdirSync(folder);
62
+ }
63
+
64
+ if (!skipFirst && files.length == 0) {
65
+ fs.rmdirSync(folder);
66
+
67
+ if (parent) {
68
+ const folderName = path.basename(folder);
69
+ const dirMeta = path.join(parent, folderName + '.meta');
70
+ fs.removeSync(dirMeta);
71
+ }
72
+ return;
73
+ }
74
+ }
75
+
76
+ module.exports.cleanBuildDirectory = cleanBuildDirectory;