fs.js
10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.unlinkIfExists = unlinkIfExists;
exports.statOrNull = statOrNull;
exports.exists = exists;
exports.walk = walk;
exports.copyFile = copyFile;
exports.copyOrLinkFile = copyOrLinkFile;
exports.copyDir = copyDir;
exports.USE_HARD_LINKS = exports.DO_NOT_USE_HARD_LINKS = exports.FileCopier = exports.CopyFileTransformer = exports.CONCURRENCY = exports.MAX_FILE_REQUESTS = void 0;
function _bluebirdLst() {
const data = _interopRequireDefault(require("bluebird-lst"));
_bluebirdLst = function () {
return data;
};
return data;
}
function _fsExtra() {
const data = require("fs-extra");
_fsExtra = function () {
return data;
};
return data;
}
var path = _interopRequireWildcard(require("path"));
function _statMode() {
const data = require("stat-mode");
_statMode = function () {
return data;
};
return data;
}
function _log() {
const data = require("./log");
_log = function () {
return data;
};
return data;
}
function _promise() {
const data = require("./promise");
_promise = function () {
return data;
};
return data;
}
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const MAX_FILE_REQUESTS = 8;
exports.MAX_FILE_REQUESTS = MAX_FILE_REQUESTS;
const CONCURRENCY = {
concurrency: MAX_FILE_REQUESTS
};
exports.CONCURRENCY = CONCURRENCY;
class CopyFileTransformer {
constructor(afterCopyTransformer) {
this.afterCopyTransformer = afterCopyTransformer;
}
}
exports.CopyFileTransformer = CopyFileTransformer;
function unlinkIfExists(file) {
return (0, _fsExtra().unlink)(file).catch(() => {});
}
async function statOrNull(file) {
return (0, _promise().orNullIfFileNotExist)((0, _fsExtra().stat)(file));
}
async function exists(file) {
try {
await (0, _fsExtra().access)(file);
return true;
} catch (e) {
return false;
}
}
/**
* Returns list of file paths (system-dependent file separator)
*/
async function walk(initialDirPath, filter, consumer) {
let result = [];
const queue = [initialDirPath];
let addDirToResult = false;
const isIncludeDir = consumer == null ? false : consumer.isIncludeDir === true;
while (queue.length > 0) {
const dirPath = queue.pop();
if (isIncludeDir) {
if (addDirToResult) {
result.push(dirPath);
} else {
addDirToResult = true;
}
}
const childNames = await (0, _promise().orIfFileNotExist)((0, _fsExtra().readdir)(dirPath), []);
childNames.sort();
let nodeModuleContent = null;
const dirs = []; // our handler is async, but we should add sorted files, so, we add file to result not in the mapper, but after map
const sortedFilePaths = await _bluebirdLst().default.map(childNames, name => {
if (name === ".DS_Store" || name === ".gitkeep") {
return null;
}
const filePath = dirPath + path.sep + name;
return (0, _fsExtra().lstat)(filePath).then(stat => {
if (filter != null && !filter(filePath, stat)) {
return null;
}
const consumerResult = consumer == null ? null : consumer.consume(filePath, stat, dirPath, childNames);
if (consumerResult === false) {
return null;
} else if (consumerResult == null || !("then" in consumerResult)) {
if (stat.isDirectory()) {
dirs.push(name);
return null;
} else {
return filePath;
}
} else {
return consumerResult.then(it => {
if (it != null && Array.isArray(it)) {
nodeModuleContent = it;
return null;
} // asarUtil can return modified stat (symlink handling)
if ((it != null && "isDirectory" in it ? it : stat).isDirectory()) {
dirs.push(name);
return null;
} else {
return filePath;
}
});
}
});
}, CONCURRENCY);
for (const child of sortedFilePaths) {
if (child != null) {
result.push(child);
}
}
dirs.sort();
for (const child of dirs) {
queue.push(dirPath + path.sep + child);
}
if (nodeModuleContent != null) {
result = result.concat(nodeModuleContent);
}
}
return result;
}
const _isUseHardLink = process.platform !== "win32" && process.env.USE_HARD_LINKS !== "false" && (require("is-ci") || process.env.USE_HARD_LINKS === "true");
function copyFile(src, dest, isEnsureDir = true) {
return (isEnsureDir ? (0, _fsExtra().ensureDir)(path.dirname(dest)) : Promise.resolve()).then(() => copyOrLinkFile(src, dest, null, false));
}
/**
* Hard links is used if supported and allowed.
* File permission is fixed — allow execute for all if owner can, allow read for all if owner can.
*
* ensureDir is not called, dest parent dir must exists
*/
function copyOrLinkFile(src, dest, stats, isUseHardLink, exDevErrorHandler) {
if (isUseHardLink === undefined) {
isUseHardLink = _isUseHardLink;
}
if (stats != null) {
const originalModeNumber = stats.mode;
const mode = new (_statMode().Mode)(stats);
if (mode.owner.execute) {
mode.group.execute = true;
mode.others.execute = true;
}
mode.group.read = true;
mode.others.read = true;
mode.setuid = false;
mode.setgid = false;
if (originalModeNumber !== stats.mode) {
if (_log().log.isDebugEnabled) {
const oldMode = new (_statMode().Mode)({
mode: originalModeNumber
});
_log().log.debug({
file: dest,
oldMode,
mode
}, "permissions fixed from");
} // https://helgeklein.com/blog/2009/05/hard-links-and-permissions-acls/
// Permissions on all hard links to the same data on disk are always identical. The same applies to attributes.
// That means if you change the permissions/owner/attributes on one hard link, you will immediately see the changes on all other hard links.
if (isUseHardLink) {
isUseHardLink = false;
_log().log.debug({
dest
}, "copied, but not linked, because file permissions need to be fixed");
}
}
}
if (isUseHardLink) {
return (0, _fsExtra().link)(src, dest).catch(e => {
if (e.code === "EXDEV") {
const isLog = exDevErrorHandler == null ? true : exDevErrorHandler();
if (isLog && _log().log.isDebugEnabled) {
_log().log.debug({
error: e.message
}, "cannot copy using hard link");
}
return doCopyFile(src, dest, stats);
} else {
throw e;
}
});
}
return doCopyFile(src, dest, stats);
}
function doCopyFile(src, dest, stats) {
const promise = (0, _fsExtra().copyFile)(src, dest);
if (stats == null) {
return promise;
}
return promise.then(() => (0, _fsExtra().chmod)(dest, stats.mode));
}
class FileCopier {
constructor(isUseHardLinkFunction, transformer) {
this.isUseHardLinkFunction = isUseHardLinkFunction;
this.transformer = transformer;
if (isUseHardLinkFunction === USE_HARD_LINKS) {
this.isUseHardLink = true;
} else {
this.isUseHardLink = _isUseHardLink && isUseHardLinkFunction !== DO_NOT_USE_HARD_LINKS;
}
}
async copy(src, dest, stat) {
let afterCopyTransformer = null;
if (this.transformer != null && stat != null && stat.isFile()) {
let data = this.transformer(src);
if (data != null) {
if (typeof data === "object" && "then" in data) {
data = await data;
}
if (data != null) {
if (data instanceof CopyFileTransformer) {
afterCopyTransformer = data.afterCopyTransformer;
} else {
await (0, _fsExtra().writeFile)(dest, data);
return;
}
}
}
}
const isUseHardLink = afterCopyTransformer == null && (!this.isUseHardLink || this.isUseHardLinkFunction == null ? this.isUseHardLink : this.isUseHardLinkFunction(dest));
await copyOrLinkFile(src, dest, stat, isUseHardLink, isUseHardLink ? () => {
// 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
if (this.isUseHardLink) {
this.isUseHardLink = false;
return true;
} else {
return false;
}
} : null);
if (afterCopyTransformer != null) {
await afterCopyTransformer(dest);
}
}
}
/**
* Empty directories is never created.
* Hard links is used if supported and allowed.
*/
exports.FileCopier = FileCopier;
function copyDir(src, destination, options = {}) {
const fileCopier = new FileCopier(options.isUseHardLink, options.transformer);
if (_log().log.isDebugEnabled) {
_log().log.debug({
src,
destination
}, `copying${fileCopier.isUseHardLink ? " using hard links" : ""}`);
}
const createdSourceDirs = new Set();
const links = [];
return walk(src, options.filter, {
consume: async (file, stat, parent) => {
if (!stat.isFile() && !stat.isSymbolicLink()) {
return;
}
if (!createdSourceDirs.has(parent)) {
await (0, _fsExtra().ensureDir)(parent.replace(src, destination));
createdSourceDirs.add(parent);
}
const destFile = file.replace(src, destination);
if (stat.isFile()) {
await fileCopier.copy(file, destFile, stat);
} else {
links.push({
file: destFile,
link: await (0, _fsExtra().readlink)(file)
});
}
}
}).then(() => _bluebirdLst().default.map(links, it => (0, _fsExtra().symlink)(it.link, it.file), CONCURRENCY));
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
const DO_NOT_USE_HARD_LINKS = file => false; // eslint-disable-next-line @typescript-eslint/no-unused-vars
exports.DO_NOT_USE_HARD_LINKS = DO_NOT_USE_HARD_LINKS;
const USE_HARD_LINKS = file => true; exports.USE_HARD_LINKS = USE_HARD_LINKS;
// __ts-babel@6.0.4
//# sourceMappingURL=fs.js.map