JezK
Edit File: fs-normalized.js
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.unlink = exports.fileDatesEqual = exports.copyFile = void 0; function _regenerator() { var data = _interopRequireDefault(require("@babel/runtime/regenerator")); _regenerator = function _regenerator() { return data; }; return data; } function _asyncToGenerator2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); _asyncToGenerator2 = function _asyncToGenerator2() { return data; }; return data; } function _fs() { var data = _interopRequireDefault(require("fs")); _fs = function _fs() { return data; }; return data; } function _promise() { var data = require("./promise.js"); _promise = function _promise() { return data; }; return data; } function _fs2() { var data = require("./fs"); _fs2 = function _fs2() { return data; }; return data; } // This module serves as a wrapper for file operations that are inconsistant across node and OS versions. var disableTimestampCorrection = undefined; // OS dependent. will be detected on first file copy. var readFileBuffer = (0, _promise().promisify)(_fs()["default"].readFile); var close = (0, _promise().promisify)(_fs()["default"].close); var lstat = (0, _promise().promisify)(_fs()["default"].lstat); var open = (0, _promise().promisify)(_fs()["default"].open); var futimes = (0, _promise().promisify)(_fs()["default"].futimes); var write = (0, _promise().promisify)(_fs()["default"].write); var unlink = (0, _promise().promisify)(require('rimraf')); /** * Unlinks the destination to force a recreation. This is needed on case-insensitive file systems * to force the correct naming when the filename has changed only in character-casing. (Jest -> jest). */ exports.unlink = unlink; var copyFile = /*#__PURE__*/function () { var _ref = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee(data, cleanup) { var ficloneFlag; return _regenerator()["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: // $FlowFixMe: Flow doesn't currently support COPYFILE_FICLONE ficloneFlag = _fs2().constants.COPYFILE_FICLONE || 0; _context.prev = 1; _context.next = 4; return unlink(data.dest); case 4: _context.next = 6; return copyFilePoly(data.src, data.dest, ficloneFlag, data); case 6: _context.prev = 6; if (cleanup) { cleanup(); } return _context.finish(6); case 9: case "end": return _context.stop(); } }, _callee, null, [[1,, 6, 9]]); })); return function copyFile(_x, _x2) { return _ref.apply(this, arguments); }; }(); // Node 8.5.0 introduced `fs.copyFile` which is much faster, so use that when available. // Otherwise we fall back to reading and writing files as buffers. exports.copyFile = copyFile; var copyFilePoly = function copyFilePoly(src, dest, flags, data) { if (_fs()["default"].copyFile) { return new Promise(function (resolve, reject) { return _fs()["default"].copyFile(src, dest, flags, function (err) { if (err) { reject(err); } else { fixTimes(undefined, dest, data).then(function () { return resolve(); })["catch"](function (ex) { return reject(ex); }); } }); }); } else { return copyWithBuffer(src, dest, flags, data); } }; var copyWithBuffer = /*#__PURE__*/function () { var _ref2 = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee2(src, dest, flags, data) { var fd, _buffer; return _regenerator()["default"].wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return open(dest, 'w', data.mode); case 2: fd = _context2.sent; _context2.prev = 3; _context2.next = 6; return readFileBuffer(src); case 6: _buffer = _context2.sent; _context2.next = 9; return write(fd, _buffer, 0, _buffer.length); case 9: _context2.next = 11; return fixTimes(fd, dest, data); case 11: _context2.prev = 11; _context2.next = 14; return close(fd); case 14: return _context2.finish(11); case 15: case "end": return _context2.stop(); } }, _callee2, null, [[3,, 11, 15]]); })); return function copyWithBuffer(_x3, _x4, _x5, _x6) { return _ref2.apply(this, arguments); }; }(); // We want to preserve file timestamps when copying a file, since yarn uses them to decide if a file has // changed compared to the cache. // There are some OS specific cases here: // * On linux, fs.copyFile does not preserve timestamps, but does on OSX and Win. // * On windows, you must open a file with write permissions to call `fs.futimes`. // * On OSX you can open with read permissions and still call `fs.futimes`. function fixTimes(_x7, _x8, _x9) { return _fixTimes.apply(this, arguments); } // Compare file timestamps. // Some versions of Node on windows zero the milliseconds when utime is used. function _fixTimes() { _fixTimes = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee3(fd, dest, data) { var doOpen, openfd, destStat; return _regenerator()["default"].wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: doOpen = fd === undefined; openfd = fd ? fd : -1; if (!(disableTimestampCorrection === undefined)) { _context3.next = 7; break; } _context3.next = 5; return lstat(dest); case 5: destStat = _context3.sent; disableTimestampCorrection = fileDatesEqual(destStat.mtime, data.mtime); case 7: if (!disableTimestampCorrection) { _context3.next = 9; break; } return _context3.abrupt("return"); case 9: if (!doOpen) { _context3.next = 27; break; } _context3.prev = 10; _context3.next = 13; return open(dest, 'a', data.mode); case 13: openfd = _context3.sent; _context3.next = 27; break; case 16: _context3.prev = 16; _context3.t0 = _context3["catch"](10); _context3.prev = 18; _context3.next = 21; return open(dest, 'r', data.mode); case 21: openfd = _context3.sent; _context3.next = 27; break; case 24: _context3.prev = 24; _context3.t1 = _context3["catch"](18); return _context3.abrupt("return"); case 27: _context3.prev = 27; if (!openfd) { _context3.next = 31; break; } _context3.next = 31; return futimes(openfd, data.atime, data.mtime); case 31: _context3.next = 35; break; case 33: _context3.prev = 33; _context3.t2 = _context3["catch"](27); case 35: _context3.prev = 35; if (!(doOpen && openfd)) { _context3.next = 39; break; } _context3.next = 39; return close(openfd); case 39: return _context3.finish(35); case 40: case "end": return _context3.stop(); } }, _callee3, null, [[10, 16], [18, 24], [27, 33, 35, 40]]); })); return _fixTimes.apply(this, arguments); } var fileDatesEqual = function fileDatesEqual(a, b) { var aTime = a.getTime(); var bTime = b.getTime(); if (process.platform !== 'win32') { return aTime === bTime; } // See https://github.com/nodejs/node/pull/12607 // Submillisecond times from stat and utimes are truncated on Windows, // causing a file with mtime 8.0079998 and 8.0081144 to become 8.007 and 8.008 // and making it impossible to update these files to their correct timestamps. if (Math.abs(aTime - bTime) <= 1) { return true; } var aTimeSec = Math.floor(aTime / 1000); var bTimeSec = Math.floor(bTime / 1000); // See https://github.com/nodejs/node/issues/2069 // Some versions of Node on windows zero the milliseconds when utime is used // So if any of the time has a milliseconds part of zero we suspect that the // bug is present and compare only seconds. if (aTime - aTimeSec * 1000 === 0 || bTime - bTimeSec * 1000 === 0) { return aTimeSec === bTimeSec; } return aTime === bTime; }; exports.fileDatesEqual = fileDatesEqual; //# sourceMappingURL=fs-normalized.js.map