UNPKG

2.44 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
28var _walk = _interopRequireDefault(require("./walk"));
29
30function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
31
32/**
33 * Copyright (c) Facebook, Inc. and its affiliates.
34 *
35 * This source code is licensed under the MIT license found in the
36 * LICENSE file in the root directory of this source tree.
37 */
38
39/**
40 * Copy files (binary included) recursively.
41 */
42async function copyFiles(srcPath, destPath, options = {}) {
43 return Promise.all((0, _walk.default)(srcPath).map(async absoluteSrcFilePath => {
44 const exclude = options.exclude;
45
46 if (exclude && exclude.some(p => p.test(absoluteSrcFilePath))) {
47 return;
48 }
49
50 const relativeFilePath = _path().default.relative(srcPath, absoluteSrcFilePath);
51
52 await copyFile(absoluteSrcFilePath, _path().default.resolve(destPath, relativeFilePath));
53 }));
54}
55/**
56 * Copy a file to given destination.
57 */
58
59
60function copyFile(srcPath, destPath) {
61 if (_fs().default.lstatSync(srcPath).isDirectory()) {
62 if (!_fs().default.existsSync(destPath)) {
63 _fs().default.mkdirSync(destPath);
64 } // Not recursive
65
66
67 return;
68 }
69
70 return new Promise((resolve, reject) => {
71 copyBinaryFile(srcPath, destPath, err => {
72 if (err) {
73 reject(err);
74 }
75
76 resolve(destPath);
77 });
78 });
79}
80/**
81 * Same as 'cp' on Unix. Don't do any replacements.
82 */
83
84
85function copyBinaryFile(srcPath, destPath, cb) {
86 let cbCalled = false;
87
88 const {
89 mode
90 } = _fs().default.statSync(srcPath);
91
92 const readStream = _fs().default.createReadStream(srcPath);
93
94 const writeStream = _fs().default.createWriteStream(destPath);
95
96 readStream.on('error', err => {
97 done(err);
98 });
99 writeStream.on('error', err => {
100 done(err);
101 });
102 readStream.on('close', () => {
103 done();
104
105 _fs().default.chmodSync(destPath, mode);
106 });
107 readStream.pipe(writeStream);
108
109 function done(err) {
110 if (!cbCalled) {
111 cb(err);
112 cbCalled = true;
113 }
114 }
115}
116
117var _default = copyFiles;
118exports.default = _default;
119
120//# sourceMappingURL=copyFiles.js.map
\No newline at end of file