UNPKG

4.37 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = void 0;
7
8function _fs() {
9 const data = _interopRequireDefault(require("fs"));
10
11 _fs = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _path() {
19 const data = _interopRequireDefault(require("path"));
20
21 _path = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
30/**
31 * Copyright (c) Facebook, Inc. and its affiliates.
32 *
33 * This source code is licensed under the MIT license found in the
34 * LICENSE file in the root directory of this source tree.
35 *
36 * @format
37 */
38// Binary files, don't process these (avoid decoding as utf8)
39const binaryExtensions = ['.png', '.jar', '.keystore'];
40
41/**
42 * Copy a file to given destination, replacing parts of its contents.
43 * @param srcPath Path to a file to be copied.
44 * @param destPath Destination path.
45 * @param replacements: e.g. {'TextToBeReplaced': 'Replacement'}
46 * @param contentChangedCallback
47 * Used when upgrading projects. Based on if file contents would change
48 * when being replaced, allows the caller to specify whether the file
49 * should be replaced or not.
50 * If null, files will be overwritten.
51 * Function(path, 'identical' | 'changed' | 'new') => 'keep' | 'overwrite'
52 */
53function copyAndReplace(srcPath, destPath, replacements, contentChangedCallback) {
54 if (_fs().default.lstatSync(srcPath).isDirectory()) {
55 if (!_fs().default.existsSync(destPath)) {
56 _fs().default.mkdirSync(destPath);
57 } // Not recursive
58
59
60 return;
61 }
62
63 const extension = _path().default.extname(srcPath);
64
65 if (binaryExtensions.indexOf(extension) !== -1) {
66 // Binary file
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 // Text file
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 // Check if contents changed and ask to overwrite
111 let contentChanged = 'identical';
112
113 try {
114 const origContent = _fs().default.readFileSync(destPath, 'utf8');
115
116 if (content !== origContent) {
117 // logger.info('Content changed: ' + destPath);
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 * Same as 'cp' on Unix. Don't do any replacements.
141 */
142
143
144function 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
175var _default = copyAndReplace;
176exports.default = _default;
\No newline at end of file