JezK
Edit File: pack.js
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports.hasWrapper = hasWrapper; exports.pack = pack; exports.packTarball = packTarball; exports.packWithIgnoreAndHeaders = packWithIgnoreAndHeaders; exports.run = run; exports.setFlags = setFlags; 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 = _interopRequireWildcard(require("../../util/fs.js")); fs = function fs() { return data; }; return data; } function _filter() { var data = require("../../util/filter.js"); _filter = function _filter() { return data; }; return data; } function _errors() { var data = require("../../errors.js"); _errors = function _errors() { return data; }; return data; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } var zlib = require('zlib'); var path = require('path'); var tar = require('tar-fs'); var fs2 = require('fs'); var depsFor = require('hash-for-dep/lib/deps-for'); var FOLDERS_IGNORE = [ // never allow version control folders '.git', 'CVS', '.svn', '.hg', 'node_modules']; var DEFAULT_IGNORE = (0, _filter().ignoreLinesToRegex)([].concat(FOLDERS_IGNORE, [ // ignore cruft 'yarn.lock', '.lock-wscript', '.wafpickle-{0..9}', '*.swp', '._*', 'npm-debug.log', 'yarn-error.log', '.npmrc', '.yarnrc', '.yarnrc.yml', '.npmignore', '.gitignore', '.DS_Store'])); var NEVER_IGNORE = (0, _filter().ignoreLinesToRegex)([ // never ignore these files '!/package.json', '!/readme*', '!/+(license|licence)*', '!/+(changes|changelog|history)*']); function packTarball(_x) { return _packTarball.apply(this, arguments); } function _packTarball() { _packTarball = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee(config) { var _ref, mapHeader, pkg, bundleDependencies, main, onlyFiles, filters, bundleDependenciesFiles, _iterator, _step, dependency, dependencyList, _iterator2, _step2, dep, filesForBundledDep, lines, regexes, files, dotIgnoreFiles, _iterator3, _step3, file, raw, _lines, _regexes, keepFiles, ignoredFiles, possibleKeepFiles, _iterator4, _step4, _file, realPath, _args = arguments; return _regenerator()["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: _ref = _args.length > 1 && _args[1] !== undefined ? _args[1] : {}, mapHeader = _ref.mapHeader; _context.next = 3; return config.readRootManifest(); case 3: pkg = _context.sent; bundleDependencies = pkg.bundleDependencies, main = pkg.main, onlyFiles = pkg.files; // include required files filters = NEVER_IGNORE.slice(); // include default filters unless `files` is used if (!onlyFiles) { filters = filters.concat(DEFAULT_IGNORE); } if (main) { filters = filters.concat((0, _filter().ignoreLinesToRegex)(['!/' + main])); } // include bundleDependencies bundleDependenciesFiles = []; if (!bundleDependencies) { _context.next = 45; break; } _iterator = _createForOfIteratorHelper(bundleDependencies); _context.prev = 11; _iterator.s(); case 13: if ((_step = _iterator.n()).done) { _context.next = 37; break; } dependency = _step.value; dependencyList = depsFor(dependency, config.cwd); _iterator2 = _createForOfIteratorHelper(dependencyList); _context.prev = 17; _iterator2.s(); case 19: if ((_step2 = _iterator2.n()).done) { _context.next = 27; break; } dep = _step2.value; _context.next = 23; return fs().walk(dep.baseDir, null, new Set(FOLDERS_IGNORE)); case 23: filesForBundledDep = _context.sent; bundleDependenciesFiles = bundleDependenciesFiles.concat(filesForBundledDep); case 25: _context.next = 19; break; case 27: _context.next = 32; break; case 29: _context.prev = 29; _context.t0 = _context["catch"](17); _iterator2.e(_context.t0); case 32: _context.prev = 32; _iterator2.f(); return _context.finish(32); case 35: _context.next = 13; break; case 37: _context.next = 42; break; case 39: _context.prev = 39; _context.t1 = _context["catch"](11); _iterator.e(_context.t1); case 42: _context.prev = 42; _iterator.f(); return _context.finish(42); case 45: // `files` field if (onlyFiles) { lines = ['*' // ignore all files except those that are explicitly included with a negation filter ]; lines = lines.concat(onlyFiles.map(function (filename) { return "!".concat(filename); }), onlyFiles.map(function (filename) { return "!".concat(path.join(filename, '**')); })); regexes = (0, _filter().ignoreLinesToRegex)(lines, './'); filters = filters.concat(regexes); } _context.next = 48; return fs().walk(config.cwd, null, new Set(FOLDERS_IGNORE)); case 48: files = _context.sent; dotIgnoreFiles = (0, _filter().filterOverridenGitignores)(files); // create ignores _iterator3 = _createForOfIteratorHelper(dotIgnoreFiles); _context.prev = 51; _iterator3.s(); case 53: if ((_step3 = _iterator3.n()).done) { _context.next = 63; break; } file = _step3.value; _context.next = 57; return fs().readFile(file.absolute); case 57: raw = _context.sent; _lines = raw.split('\n'); _regexes = (0, _filter().ignoreLinesToRegex)(_lines, path.dirname(file.relative)); filters = filters.concat(_regexes); case 61: _context.next = 53; break; case 63: _context.next = 68; break; case 65: _context.prev = 65; _context.t2 = _context["catch"](51); _iterator3.e(_context.t2); case 68: _context.prev = 68; _iterator3.f(); return _context.finish(68); case 71: // files to definitely keep, takes precedence over ignore filter keepFiles = new Set(); // files to definitely ignore ignoredFiles = new Set(); // list of files that didn't match any of our patterns, if a directory in the chain above was matched // then we should inherit it possibleKeepFiles = new Set(); // apply filters (0, _filter().sortFilter)(files, filters, keepFiles, possibleKeepFiles, ignoredFiles); // add the files for the bundled dependencies to the set of files to keep _iterator4 = _createForOfIteratorHelper(bundleDependenciesFiles); _context.prev = 76; _iterator4.s(); case 78: if ((_step4 = _iterator4.n()).done) { _context.next = 86; break; } _file = _step4.value; _context.next = 82; return fs().realpath(config.cwd); case 82: realPath = _context.sent; keepFiles.add(path.relative(realPath, _file.absolute)); case 84: _context.next = 78; break; case 86: _context.next = 91; break; case 88: _context.prev = 88; _context.t3 = _context["catch"](76); _iterator4.e(_context.t3); case 91: _context.prev = 91; _iterator4.f(); return _context.finish(91); case 94: return _context.abrupt("return", packWithIgnoreAndHeaders(config.cwd, function (name) { var relative = path.relative(config.cwd, name); // Don't ignore directories, since we need to recurse inside them to check for unignored files. if (fs2.lstatSync(name).isDirectory()) { var isParentOfKeptFile = Array.from(keepFiles).some(function (name) { return !path.relative(relative, name).startsWith('..'); }); return !isParentOfKeptFile; } // Otherwise, ignore a file if we're not supposed to keep it. return !keepFiles.has(relative); }, { mapHeader: mapHeader })); case 95: case "end": return _context.stop(); } }, _callee, null, [[11, 39, 42, 45], [17, 29, 32, 35], [51, 65, 68, 71], [76, 88, 91, 94]]); })); return _packTarball.apply(this, arguments); } function packWithIgnoreAndHeaders(cwd, ignoreFunction) { var _ref2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}, mapHeader = _ref2.mapHeader; return tar.pack(cwd, { ignore: ignoreFunction, sort: true, map: function map(header) { var suffix = header.name === '.' ? '' : "/".concat(header.name); header.name = "package".concat(suffix); delete header.uid; delete header.gid; return mapHeader ? mapHeader(header) : header; } }); } function pack(_x2) { return _pack.apply(this, arguments); } function _pack() { _pack = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee2(config) { var packer, compressor; return _regenerator()["default"].wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: _context2.next = 2; return packTarball(config); case 2: packer = _context2.sent; compressor = packer.pipe(new zlib.Gzip()); return _context2.abrupt("return", compressor); case 5: case "end": return _context2.stop(); } }, _callee2); })); return _pack.apply(this, arguments); } function setFlags(commander) { commander.description('Creates a compressed gzip archive of package dependencies.'); commander.option('-f, --filename <filename>', 'filename'); } function hasWrapper(commander, args) { return true; } function run(_x3, _x4, _x5, _x6, _x7) { return _run.apply(this, arguments); } function _run() { _run = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee3(config, reporter, commander, flags, args) { var pkg, normaliseScope, filename, stream; return _regenerator()["default"].wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return config.readRootManifest(); case 2: pkg = _context3.sent; if (pkg.name) { _context3.next = 5; break; } throw new (_errors().MessageError)(reporter.lang('noName')); case 5: if (pkg.version) { _context3.next = 7; break; } throw new (_errors().MessageError)(reporter.lang('noVersion')); case 7: normaliseScope = function normaliseScope(name) { return name[0] === '@' ? name.substr(1).replace('/', '-') : name; }; filename = flags.filename || path.join(config.cwd, "".concat(normaliseScope(pkg.name), "-v").concat(pkg.version, ".tgz")); _context3.next = 11; return config.executeLifecycleScript('prepack'); case 11: _context3.next = 13; return pack(config); case 13: stream = _context3.sent; _context3.next = 16; return new Promise(function (resolve, reject) { stream.pipe(fs2.createWriteStream(filename)); stream.on('error', reject); stream.on('close', resolve); }); case 16: _context3.next = 18; return config.executeLifecycleScript('postpack'); case 18: reporter.success(reporter.lang('packWroteTarball', filename)); case 19: case "end": return _context3.stop(); } }, _callee3); })); return _run.apply(this, arguments); } //# sourceMappingURL=pack.js.map