1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | function _fs() {
|
9 | const data = _interopRequireDefault(require("fs"));
|
10 |
|
11 | _fs = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | function _path() {
|
19 | const data = _interopRequireDefault(require("path"));
|
20 |
|
21 | _path = function () {
|
22 | return data;
|
23 | };
|
24 |
|
25 | return data;
|
26 | }
|
27 |
|
28 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | const binaryExtensions = ['.png', '.jar', '.keystore'];
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function copyAndReplace(srcPath, destPath, replacements, contentChangedCallback) {
|
54 | if (_fs().default.lstatSync(srcPath).isDirectory()) {
|
55 | if (!_fs().default.existsSync(destPath)) {
|
56 | _fs().default.mkdirSync(destPath);
|
57 | }
|
58 |
|
59 |
|
60 | return;
|
61 | }
|
62 |
|
63 | const extension = _path().default.extname(srcPath);
|
64 |
|
65 | if (binaryExtensions.indexOf(extension) !== -1) {
|
66 |
|
67 | let shouldOverwrite = 'overwrite';
|
68 |
|
69 | if (contentChangedCallback) {
|
70 | const newContentBuffer = _fs().default.readFileSync(srcPath);
|
71 |
|
72 | let contentChanged = 'identical';
|
73 |
|
74 | try {
|
75 | const origContentBuffer = _fs().default.readFileSync(destPath);
|
76 |
|
77 | if (Buffer.compare(origContentBuffer, newContentBuffer) !== 0) {
|
78 | contentChanged = 'changed';
|
79 | }
|
80 | } catch (err) {
|
81 | if (err.code === 'ENOENT') {
|
82 | contentChanged = 'new';
|
83 | } else {
|
84 | throw err;
|
85 | }
|
86 | }
|
87 |
|
88 | shouldOverwrite = contentChangedCallback(destPath, contentChanged);
|
89 | }
|
90 |
|
91 | if (shouldOverwrite === 'overwrite') {
|
92 | copyBinaryFile(srcPath, destPath, err => {
|
93 | if (err) {
|
94 | throw err;
|
95 | }
|
96 | });
|
97 | }
|
98 | } else {
|
99 |
|
100 | const srcPermissions = _fs().default.statSync(srcPath).mode;
|
101 |
|
102 | let content = _fs().default.readFileSync(srcPath, 'utf8');
|
103 |
|
104 | Object.keys(replacements).forEach(regex => {
|
105 | content = content.replace(new RegExp(regex, 'g'), replacements[regex]);
|
106 | });
|
107 | let shouldOverwrite = 'overwrite';
|
108 |
|
109 | if (contentChangedCallback) {
|
110 |
|
111 | let contentChanged = 'identical';
|
112 |
|
113 | try {
|
114 | const origContent = _fs().default.readFileSync(destPath, 'utf8');
|
115 |
|
116 | if (content !== origContent) {
|
117 |
|
118 | contentChanged = 'changed';
|
119 | }
|
120 | } catch (err) {
|
121 | if (err.code === 'ENOENT') {
|
122 | contentChanged = 'new';
|
123 | } else {
|
124 | throw err;
|
125 | }
|
126 | }
|
127 |
|
128 | shouldOverwrite = contentChangedCallback(destPath, contentChanged);
|
129 | }
|
130 |
|
131 | if (shouldOverwrite === 'overwrite') {
|
132 | _fs().default.writeFileSync(destPath, content, {
|
133 | encoding: 'utf8',
|
134 | mode: srcPermissions
|
135 | });
|
136 | }
|
137 | }
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 | function copyBinaryFile(srcPath, destPath, cb) {
|
145 | let cbCalled = false;
|
146 |
|
147 | const srcPermissions = _fs().default.statSync(srcPath).mode;
|
148 |
|
149 | const readStream = _fs().default.createReadStream(srcPath);
|
150 |
|
151 | readStream.on('error', err => {
|
152 | done(err);
|
153 | });
|
154 |
|
155 | const writeStream = _fs().default.createWriteStream(destPath, {
|
156 | mode: srcPermissions
|
157 | });
|
158 |
|
159 | writeStream.on('error', err => {
|
160 | done(err);
|
161 | });
|
162 | writeStream.on('close', () => {
|
163 | done();
|
164 | });
|
165 | readStream.pipe(writeStream);
|
166 |
|
167 | function done(err) {
|
168 | if (!cbCalled) {
|
169 | cb(err);
|
170 | cbCalled = true;
|
171 | }
|
172 | }
|
173 | }
|
174 |
|
175 | var _default = copyAndReplace;
|
176 | exports.default = _default; |
\ | No newline at end of file |