UNPKG

12.3 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.DO_NOT_USE_HARD_LINKS = exports.FileCopier = exports.walk = exports.exists = exports.statOrNull = exports.CONCURRENCY = exports.MAX_FILE_REQUESTS = undefined;
7
8var _bluebirdLst;
9
10function _load_bluebirdLst() {
11 return _bluebirdLst = require("bluebird-lst");
12}
13
14var _bluebirdLst2;
15
16function _load_bluebirdLst2() {
17 return _bluebirdLst2 = _interopRequireDefault(require("bluebird-lst"));
18}
19
20let statOrNull = exports.statOrNull = (() => {
21 var _ref = (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* (file) {
22 return (0, (_promise || _load_promise()).orNullIfFileNotExist)((0, (_fsExtraP || _load_fsExtraP()).stat)(file));
23 });
24
25 return function statOrNull(_x) {
26 return _ref.apply(this, arguments);
27 };
28})();
29
30let exists = exports.exists = (() => {
31 var _ref2 = (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* (file) {
32 try {
33 yield (0, (_fsExtraP || _load_fsExtraP()).access)(file);
34 return true;
35 } catch (e) {
36 return false;
37 }
38 });
39
40 return function exists(_x2) {
41 return _ref2.apply(this, arguments);
42 };
43})();
44
45let walk = exports.walk = (() => {
46 var _ref3 = (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* (initialDirPath, filter, consumer) {
47 let result = [];
48 const queue = [initialDirPath];
49 let addDirToResult = false;
50 const isIncludeDir = consumer == null ? false : consumer.isIncludeDir === true;
51 while (queue.length > 0) {
52 const dirPath = queue.pop();
53 if (isIncludeDir) {
54 if (addDirToResult) {
55 result.push(dirPath);
56 } else {
57 addDirToResult = true;
58 }
59 }
60 const childNames = yield (0, (_fsExtraP || _load_fsExtraP()).readdir)(dirPath);
61 childNames.sort();
62 let nodeModuleContent = null;
63 const dirs = [];
64 // our handler is async, but we should add sorted files, so, we add file to result not in the mapper, but after map
65 const sortedFilePaths = yield (_bluebirdLst2 || _load_bluebirdLst2()).default.map(childNames, function (name) {
66 if (name === ".DS_Store" || name === ".gitkeep") {
67 return null;
68 }
69 const filePath = dirPath + _path.sep + name;
70 return (0, (_fsExtraP || _load_fsExtraP()).lstat)(filePath).then(function (stat) {
71 if (filter != null && !filter(filePath, stat)) {
72 return null;
73 }
74 const consumerResult = consumer == null ? null : consumer.consume(filePath, stat, dirPath, childNames);
75 if (consumerResult == null || !("then" in consumerResult)) {
76 if (stat.isDirectory()) {
77 dirs.push(name);
78 return null;
79 } else {
80 return filePath;
81 }
82 } else {
83 return consumerResult.then(function (it) {
84 if (it != null && Array.isArray(it)) {
85 nodeModuleContent = it;
86 return null;
87 }
88 // asarUtil can return modified stat (symlink handling)
89 if ((it != null && "isDirectory" in it ? it : stat).isDirectory()) {
90 dirs.push(name);
91 return null;
92 } else {
93 return filePath;
94 }
95 });
96 }
97 });
98 }, CONCURRENCY);
99 for (const child of sortedFilePaths) {
100 if (child != null) {
101 result.push(child);
102 }
103 }
104 dirs.sort();
105 for (const child of dirs) {
106 queue.push(dirPath + _path.sep + child);
107 }
108 if (nodeModuleContent != null) {
109 result = result.concat(nodeModuleContent);
110 }
111 }
112 return result;
113 });
114
115 return function walk(_x3, _x4, _x5) {
116 return _ref3.apply(this, arguments);
117 };
118})();
119
120exports.unlinkIfExists = unlinkIfExists;
121exports.copyFile = copyFile;
122exports.copyOrLinkFile = copyOrLinkFile;
123exports.copyDir = copyDir;
124
125var _fcopyPreBundled;
126
127function _load_fcopyPreBundled() {
128 return _fcopyPreBundled = _interopRequireDefault(require("fcopy-pre-bundled"));
129}
130
131var _fsExtraP;
132
133function _load_fsExtraP() {
134 return _fsExtraP = require("fs-extra-p");
135}
136
137var _isCi;
138
139function _load_isCi() {
140 return _isCi = _interopRequireDefault(require("is-ci"));
141}
142
143var _path = _interopRequireWildcard(require("path"));
144
145var _statMode;
146
147function _load_statMode() {
148 return _statMode = _interopRequireDefault(require("stat-mode"));
149}
150
151var _promise;
152
153function _load_promise() {
154 return _promise = require("./promise");
155}
156
157var _util;
158
159function _load_util() {
160 return _util = require("./util");
161}
162
163function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
164
165function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
166
167const MAX_FILE_REQUESTS = exports.MAX_FILE_REQUESTS = 8;
168const CONCURRENCY = exports.CONCURRENCY = { concurrency: MAX_FILE_REQUESTS };
169function unlinkIfExists(file) {
170 return (0, (_fsExtraP || _load_fsExtraP()).unlink)(file).catch(() => {});
171}
172
173const _isUseHardLink = process.platform !== "win32" && process.env.USE_HARD_LINKS !== "false" && ((_isCi || _load_isCi()).default || process.env.USE_HARD_LINKS === "true");
174function copyFile(src, dest) {
175 let isEnsureDir = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
176
177 return (isEnsureDir ? (0, (_fsExtraP || _load_fsExtraP()).ensureDir)(_path.dirname(dest)) : (_bluebirdLst2 || _load_bluebirdLst2()).default.resolve()).then(() => copyOrLinkFile(src, dest, null, false));
178}
179/**
180 * Hard links is used if supported and allowed.
181 * File permission is fixed — allow execute for all if owner can, allow read for all if owner can.
182 *
183 * ensureDir is not called, dest parent dir must exists
184 */
185function copyOrLinkFile(src, dest, stats) {
186 let isUseHardLink = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : _isUseHardLink;
187
188 if (stats != null) {
189 const originalModeNumber = stats.mode;
190 const mode = new (_statMode || _load_statMode()).default(stats);
191 if (mode.owner.execute) {
192 mode.group.execute = true;
193 mode.others.execute = true;
194 }
195 mode.group.read = true;
196 mode.others.read = true;
197 if (originalModeNumber !== stats.mode) {
198 if ((_util || _load_util()).debug.enabled) {
199 const oldMode = new (_statMode || _load_statMode()).default(Object.assign({}, stats, { mode: originalModeNumber }));
200 (0, (_util || _load_util()).debug)(`${dest} permissions fixed from ${oldMode.toOctal()} (${oldMode.toString()}) to ${mode.toOctal()} (${mode.toString()})`);
201 }
202 // https://helgeklein.com/blog/2009/05/hard-links-and-permissions-acls/
203 // Permissions on all hard links to the same data on disk are always identical. The same applies to attributes.
204 // That means if you change the permissions/owner/attributes on one hard link, you will immediately see the changes on all other hard links.
205 if (isUseHardLink) {
206 isUseHardLink = false;
207 (0, (_util || _load_util()).debug)(`${dest} will be copied, but not linked, because file permissions need to be fixed`);
208 }
209 }
210 }
211 if (isUseHardLink) {
212 return (0, (_fsExtraP || _load_fsExtraP()).link)(src, dest);
213 }
214 return new (_bluebirdLst2 || _load_bluebirdLst2()).default((resolve, reject) => {
215 (0, (_fcopyPreBundled || _load_fcopyPreBundled()).default)(src, dest, stats == null ? undefined : { mode: stats.mode }, error => error == null ? resolve() : reject(error));
216 });
217}
218class FileCopier {
219 constructor(isUseHardLinkFunction, transformer) {
220 this.isUseHardLinkFunction = isUseHardLinkFunction;
221 this.transformer = transformer;
222 this.isUseHardLink = _isUseHardLink && isUseHardLinkFunction !== DO_NOT_USE_HARD_LINKS;
223 }
224 copy(src, dest, stat) {
225 var _this = this;
226
227 return (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* () {
228 try {
229 if (_this.transformer != null && stat != null && stat.isFile()) {
230 let data = _this.transformer(src);
231 if (data != null) {
232 if (typeof data.then === "function") {
233 data = yield data;
234 }
235 if (data != null) {
236 yield (0, (_fsExtraP || _load_fsExtraP()).writeFile)(dest, data);
237 return;
238 }
239 }
240 }
241 yield copyOrLinkFile(src, dest, stat, !_this.isUseHardLink || _this.isUseHardLinkFunction == null ? _this.isUseHardLink : _this.isUseHardLinkFunction(dest));
242 } catch (e) {
243 // files are copied concurrently, so, we must not check here currentIsUseHardLink — our code can be executed after that other handler will set currentIsUseHardLink to false
244 if (e.code === "EXDEV") {
245 // ...but here we want to avoid excess debug log message
246 if (_this.isUseHardLink) {
247 (0, (_util || _load_util()).debug)(`Cannot copy using hard link: ${e}`);
248 _this.isUseHardLink = false;
249 }
250 yield copyOrLinkFile(src, dest, stat, false);
251 } else {
252 throw e;
253 }
254 }
255 })();
256 }
257}
258exports.FileCopier = FileCopier; /**
259 * Empty directories is never created.
260 * Hard links is used if supported and allowed.
261 */
262
263function copyDir(src, destination, filter, transformer, isUseHardLink) {
264 const fileCopier = new FileCopier(isUseHardLink, transformer);
265 if ((_util || _load_util()).debug.enabled) {
266 (0, (_util || _load_util()).debug)(`Copying ${src} to ${destination}${fileCopier.isUseHardLink ? " using hard links" : ""}`);
267 }
268 const createdSourceDirs = new Set();
269 const links = [];
270 return walk(src, filter, {
271 consume: (() => {
272 var _ref4 = (0, (_bluebirdLst || _load_bluebirdLst()).coroutine)(function* (file, stat, parent) {
273 if (!stat.isFile() && !stat.isSymbolicLink()) {
274 return;
275 }
276 if (!createdSourceDirs.has(parent)) {
277 yield (0, (_fsExtraP || _load_fsExtraP()).ensureDir)(parent.replace(src, destination));
278 createdSourceDirs.add(parent);
279 }
280 const destFile = file.replace(src, destination);
281 if (stat.isFile()) {
282 yield fileCopier.copy(file, destFile, stat);
283 } else {
284 links.push({ file: destFile, link: yield (0, (_fsExtraP || _load_fsExtraP()).readlink)(file) });
285 }
286 });
287
288 return function consume(_x8, _x9, _x10) {
289 return _ref4.apply(this, arguments);
290 };
291 })()
292 }).then(() => (_bluebirdLst2 || _load_bluebirdLst2()).default.map(links, it => (0, (_fsExtraP || _load_fsExtraP()).symlink)(it.link, it.file), CONCURRENCY));
293}
294const DO_NOT_USE_HARD_LINKS = exports.DO_NOT_USE_HARD_LINKS = file => false;
295//# sourceMappingURL=fs.js.map
\No newline at end of file