JezK
Edit File: execute-lifecycle-script.js
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _typeof3 = require("@babel/runtime/helpers/typeof"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.IGNORE_MANIFEST_KEYS = void 0; exports.execCommand = execCommand; exports.execFromManifest = execFromManifest; exports.executeLifecycleScript = executeLifecycleScript; exports.getWrappersFolder = getWrappersFolder; exports.makeEnv = makeEnv; function _regenerator() { var data = _interopRequireDefault(require("@babel/runtime/regenerator")); _regenerator = function _regenerator() { return data; }; return data; } function _toConsumableArray2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); _toConsumableArray2 = function _toConsumableArray2() { return data; }; return data; } function _slicedToArray2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); _slicedToArray2 = function _slicedToArray2() { return data; }; return data; } function _typeof2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/typeof")); _typeof2 = function _typeof2() { return data; }; return data; } function _defineProperty2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); _defineProperty2 = function _defineProperty2() { return data; }; return data; } function _asyncToGenerator2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); _asyncToGenerator2 = function _asyncToGenerator2() { return data; }; return data; } function _errors() { var data = require("../errors.js"); _errors = function _errors() { return data; }; return data; } function constants() { var data = _interopRequireWildcard(require("../constants.js")); constants = function constants() { return data; }; return data; } function child() { var data = _interopRequireWildcard(require("./child.js")); child = function child() { return data; }; return data; } function fs() { var data = _interopRequireWildcard(require("./fs.js")); fs = function fs() { return data; }; return data; } function _dynamicRequire() { var data = require("./dynamic-require.js"); _dynamicRequire = function _dynamicRequire() { return data; }; return data; } function _portableScript() { var data = require("./portable-script.js"); _portableScript = function _portableScript() { return data; }; return data; } function _fixCmdWinSlashes() { var data = require("./fix-cmd-win-slashes.js"); _fixCmdWinSlashes = function _fixCmdWinSlashes() { return data; }; return data; } function _global() { var data = require("../cli/commands/global.js"); _global = function _global() { 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 || _typeof3(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; } function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2()["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } var path = require('path'); var IGNORE_MANIFEST_KEYS = new Set(['readme', 'notice', 'licenseText', 'activationEvents', 'contributes']); // We treat these configs as internal, thus not expose them to process.env. // This helps us avoid some gyp issues when building native modules. // See https://github.com/yarnpkg/yarn/issues/2286. exports.IGNORE_MANIFEST_KEYS = IGNORE_MANIFEST_KEYS; var IGNORE_CONFIG_KEYS = ['lastUpdateCheck']; var wrappersFolder = null; function getWrappersFolder(_x) { return _getWrappersFolder.apply(this, arguments); } function _getWrappersFolder() { _getWrappersFolder = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee(config) { return _regenerator()["default"].wrap(function _callee$(_context) { while (1) switch (_context.prev = _context.next) { case 0: if (!wrappersFolder) { _context.next = 2; break; } return _context.abrupt("return", wrappersFolder); case 2: _context.next = 4; return fs().makeTempDir(); case 4: wrappersFolder = _context.sent; _context.next = 7; return (0, _portableScript().makePortableProxyScript)(process.execPath, wrappersFolder, { proxyBasename: 'node' }); case 7: _context.next = 9; return (0, _portableScript().makePortableProxyScript)(process.execPath, wrappersFolder, { proxyBasename: 'yarn', prependArguments: [process.argv[1]] }); case 9: return _context.abrupt("return", wrappersFolder); case 10: case "end": return _context.stop(); } }, _callee); })); return _getWrappersFolder.apply(this, arguments); } var INVALID_CHAR_REGEX = /\W/g; function makeEnv(_x2, _x3, _x4) { return _makeEnv.apply(this, arguments); } function _makeEnv() { _makeEnv = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee2(stage, cwd, config) { var env, customEnv, manifest, queue, _queue$pop, _queue$pop2, _key, val, subKey, fullKey, cleanVal, cleanKey, keys, cleaned, _iterator, _step, _step$value, _key3, _val2, _cleanKey2, _envKey, packageConfigPrefix, _iterator2, _step2, _step2$value, _key2, _val, _cleanKey, envKey, envPath, pathParts, globalBin, _iterator3, _step3, registryFolder, _binFolder, pnpFile, candidate, pnpApi, packageLocator, packageInformation, _iterator4, _step4, _step4$value, name, reference, dependencyInformation, binFolder; return _regenerator()["default"].wrap(function _callee2$(_context2) { while (1) switch (_context2.prev = _context2.next) { case 0: env = _objectSpread({ NODE: process.execPath, INIT_CWD: process.cwd() }, process.env); // Merge in the `env` object specified in .yarnrc customEnv = config.getOption('env'); if (customEnv && (0, _typeof2()["default"])(customEnv) === 'object') { Object.assign(env, customEnv); } env.npm_lifecycle_event = stage; env.npm_node_execpath = env.NODE; env.npm_execpath = env.npm_execpath || process.mainModule && process.mainModule.filename; // Set the env to production for npm compat if production mode. // https://github.com/npm/npm/blob/30d75e738b9cb7a6a3f9b50e971adcbe63458ed3/lib/utils/lifecycle.js#L336 if (config.production) { env.NODE_ENV = 'production'; } // Note: npm_config_argv environment variable contains output of nopt - command-line // parser used by npm. Since we use other parser, we just roughly emulate it's output. (See: #684) env.npm_config_argv = JSON.stringify({ remain: [], cooked: config.commandName === 'run' ? [config.commandName, stage] : [config.commandName], original: process.argv.slice(2) }); _context2.next = 10; return config.maybeReadManifest(cwd); case 10: manifest = _context2.sent; if (manifest) { if (manifest.scripts && Object.prototype.hasOwnProperty.call(manifest.scripts, stage)) { env.npm_lifecycle_script = manifest.scripts[stage]; } // add npm_package_* queue = [['', manifest]]; while (queue.length) { _queue$pop = queue.pop(), _queue$pop2 = (0, _slicedToArray2()["default"])(_queue$pop, 2), _key = _queue$pop2[0], val = _queue$pop2[1]; if ((0, _typeof2()["default"])(val) === 'object') { for (subKey in val) { fullKey = [_key, subKey].filter(Boolean).join('_'); if (fullKey && fullKey[0] !== '_' && !IGNORE_MANIFEST_KEYS.has(fullKey)) { queue.push([fullKey, val[subKey]]); } } } else { cleanVal = String(val); if (cleanVal.indexOf('\n') >= 0) { cleanVal = JSON.stringify(cleanVal); } //replacing invalid chars with underscore cleanKey = _key.replace(INVALID_CHAR_REGEX, '_'); env["npm_package_".concat(cleanKey)] = cleanVal; } } } // add npm_config_* and npm_package_config_* from yarn config keys = new Set([].concat((0, _toConsumableArray2()["default"])(Object.keys(config.registries.yarn.config)), (0, _toConsumableArray2()["default"])(Object.keys(config.registries.npm.config)))); cleaned = Array.from(keys).filter(function (key) { return !key.match(/:_/) && IGNORE_CONFIG_KEYS.indexOf(key) === -1; }).map(function (key) { var val = config.getOption(key); if (!val) { val = ''; } else if (typeof val === 'number') { val = '' + val; } else if (typeof val !== 'string') { val = JSON.stringify(val); } if (val.indexOf('\n') >= 0) { val = JSON.stringify(val); } return [key, val]; }); // add npm_config_* _iterator = _createForOfIteratorHelper(cleaned); try { for (_iterator.s(); !(_step = _iterator.n()).done;) { _step$value = (0, _slicedToArray2()["default"])(_step.value, 2), _key3 = _step$value[0], _val2 = _step$value[1]; _cleanKey2 = _key3.replace(/^_+/, ''); _envKey = "npm_config_".concat(_cleanKey2).replace(INVALID_CHAR_REGEX, '_'); env[_envKey] = _val2; } // add npm_package_config_* } catch (err) { _iterator.e(err); } finally { _iterator.f(); } if (!(manifest && manifest.name)) { _context2.next = 38; break; } packageConfigPrefix = "".concat(manifest.name, ":"); _iterator2 = _createForOfIteratorHelper(cleaned); _context2.prev = 19; _iterator2.s(); case 21: if ((_step2 = _iterator2.n()).done) { _context2.next = 30; break; } _step2$value = (0, _slicedToArray2()["default"])(_step2.value, 2), _key2 = _step2$value[0], _val = _step2$value[1]; if (!(_key2.indexOf(packageConfigPrefix) !== 0)) { _context2.next = 25; break; } return _context2.abrupt("continue", 28); case 25: _cleanKey = _key2.replace(/^_+/, '').replace(packageConfigPrefix, ''); envKey = "npm_package_config_".concat(_cleanKey).replace(INVALID_CHAR_REGEX, '_'); env[envKey] = _val; case 28: _context2.next = 21; break; case 30: _context2.next = 35; break; case 32: _context2.prev = 32; _context2.t0 = _context2["catch"](19); _iterator2.e(_context2.t0); case 35: _context2.prev = 35; _iterator2.f(); return _context2.finish(35); case 38: // split up the path envPath = env[constants().ENV_PATH_KEY]; pathParts = envPath ? envPath.split(path.delimiter) : []; // Include node-gyp version that was bundled with the current Node.js version, // if available. pathParts.unshift(path.join(path.dirname(process.execPath), 'node_modules', 'npm', 'bin', 'node-gyp-bin')); pathParts.unshift(path.join(path.dirname(process.execPath), '..', 'lib', 'node_modules', 'npm', 'bin', 'node-gyp-bin')); // Include node-gyp version from homebrew managed npm, if available. pathParts.unshift(path.join(path.dirname(process.execPath), '..', 'libexec', 'lib', 'node_modules', 'npm', 'bin', 'node-gyp-bin')); // Add global bin folder if it is not present already, as some packages depend // on a globally-installed version of node-gyp. _context2.next = 45; return (0, _global().getBinFolder)(config, {}); case 45: globalBin = _context2.sent; if (pathParts.indexOf(globalBin) === -1) { pathParts.unshift(globalBin); } // Add node_modules .bin folders to the PATH _iterator3 = _createForOfIteratorHelper(config.registryFolders); try { for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) { registryFolder = _step3.value; _binFolder = path.join(registryFolder, '.bin'); if (config.workspacesEnabled && config.workspaceRootFolder) { pathParts.unshift(path.join(config.workspaceRootFolder, _binFolder)); } pathParts.unshift(path.join(config.linkFolder, _binFolder)); pathParts.unshift(path.join(cwd, _binFolder)); } } catch (err) { _iterator3.e(err); } finally { _iterator3.f(); } if (!process.versions.pnp) { _context2.next = 53; break; } pnpFile = _dynamicRequire().dynamicRequire.resolve('pnpapi'); _context2.next = 58; break; case 53: candidate = "".concat(config.lockfileFolder, "/").concat(constants().PNP_FILENAME); _context2.next = 56; return fs().exists(candidate); case 56: if (!_context2.sent) { _context2.next = 58; break; } pnpFile = candidate; case 58: if (!pnpFile) { _context2.next = 87; break; } pnpApi = (0, _dynamicRequire().dynamicRequire)(pnpFile); packageLocator = pnpApi.findPackageLocator("".concat(cwd, "/")); packageInformation = pnpApi.getPackageInformation(packageLocator); _iterator4 = _createForOfIteratorHelper(packageInformation.packageDependencies.entries()); _context2.prev = 63; _iterator4.s(); case 65: if ((_step4 = _iterator4.n()).done) { _context2.next = 77; break; } _step4$value = (0, _slicedToArray2()["default"])(_step4.value, 2), name = _step4$value[0], reference = _step4$value[1]; dependencyInformation = pnpApi.getPackageInformation({ name: name, reference: reference }); if (!(!dependencyInformation || !dependencyInformation.packageLocation)) { _context2.next = 70; break; } return _context2.abrupt("continue", 75); case 70: binFolder = "".concat(dependencyInformation.packageLocation, "/.bin"); _context2.next = 73; return fs().exists(binFolder); case 73: if (!_context2.sent) { _context2.next = 75; break; } pathParts.unshift(binFolder); case 75: _context2.next = 65; break; case 77: _context2.next = 82; break; case 79: _context2.prev = 79; _context2.t1 = _context2["catch"](63); _iterator4.e(_context2.t1); case 82: _context2.prev = 82; _iterator4.f(); return _context2.finish(82); case 85: // Note that NODE_OPTIONS doesn't support any style of quoting its arguments at the moment // For this reason, it won't work if the user has a space inside its $PATH env.NODE_OPTIONS = env.NODE_OPTIONS || ''; env.NODE_OPTIONS = "--require ".concat(pnpFile, " ").concat(env.NODE_OPTIONS); case 87: _context2.t2 = pathParts; _context2.next = 90; return getWrappersFolder(config); case 90: _context2.t3 = _context2.sent; _context2.t2.unshift.call(_context2.t2, _context2.t3); // join path back together env[constants().ENV_PATH_KEY] = pathParts.join(path.delimiter); return _context2.abrupt("return", env); case 94: case "end": return _context2.stop(); } }, _callee2, null, [[19, 32, 35, 38], [63, 79, 82, 85]]); })); return _makeEnv.apply(this, arguments); } function executeLifecycleScript(_x5) { return _executeLifecycleScript.apply(this, arguments); } function _executeLifecycleScript() { _executeLifecycleScript = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee3(_ref) { var stage, config, cwd, cmd, isInteractive, onProgress, customShell, env, stdio, detached, shell, stdout; return _regenerator()["default"].wrap(function _callee3$(_context3) { while (1) switch (_context3.prev = _context3.next) { case 0: stage = _ref.stage, config = _ref.config, cwd = _ref.cwd, cmd = _ref.cmd, isInteractive = _ref.isInteractive, onProgress = _ref.onProgress, customShell = _ref.customShell; _context3.next = 3; return makeEnv(stage, cwd, config); case 3: env = _context3.sent; _context3.next = 6; return checkForGypIfNeeded(config, cmd, env[constants().ENV_PATH_KEY].split(path.delimiter)); case 6: if (process.platform === 'win32' && (!customShell || customShell === 'cmd')) { // handle windows run scripts starting with a relative path cmd = (0, _fixCmdWinSlashes().fixCmdWinSlashes)(cmd); } // By default (non-interactive), pipe everything to the terminal and run child process detached // as long as it's not Windows (since windows does not have /dev/tty) stdio = ['ignore', 'pipe', 'pipe']; detached = process.platform !== 'win32'; if (isInteractive) { stdio = 'inherit'; detached = false; } shell = customShell || true; _context3.next = 13; return child().spawn(cmd, [], { cwd: cwd, env: env, stdio: stdio, detached: detached, shell: shell }, onProgress); case 13: stdout = _context3.sent; return _context3.abrupt("return", { cwd: cwd, command: cmd, stdout: stdout }); case 15: case "end": return _context3.stop(); } }, _callee3); })); return _executeLifecycleScript.apply(this, arguments); } var _default = executeLifecycleScript; exports["default"] = _default; var checkGypPromise = null; /** * Special case: Some packages depend on node-gyp, but don't specify this in * their package.json dependencies. They assume that node-gyp is available * globally. We need to detect this case and show an error message. */ function checkForGypIfNeeded(config, cmd, paths) { if (cmd.substr(0, cmd.indexOf(' ')) !== 'node-gyp') { return Promise.resolve(); } // Ensure this only runs once, rather than multiple times in parallel. if (!checkGypPromise) { checkGypPromise = _checkForGyp(config, paths); } return checkGypPromise; } function _checkForGyp(_x6, _x7) { return _checkForGyp2.apply(this, arguments); } function _checkForGyp2() { _checkForGyp2 = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee4(config, paths) { var reporter, allChecks; return _regenerator()["default"].wrap(function _callee4$(_context4) { while (1) switch (_context4.prev = _context4.next) { case 0: reporter = config.reporter; // Check every directory in the PATH _context4.next = 3; return Promise.all(paths.map(function (dir) { return fs().exists(path.join(dir, 'node-gyp')); })); case 3: allChecks = _context4.sent; if (!allChecks.some(Boolean)) { _context4.next = 6; break; } return _context4.abrupt("return"); case 6: reporter.info(reporter.lang('packageRequiresNodeGyp')); _context4.prev = 7; _context4.next = 10; return (0, _global().run)(config, reporter, {}, {}, ['add', 'node-gyp']); case 10: _context4.next = 15; break; case 12: _context4.prev = 12; _context4.t0 = _context4["catch"](7); throw new (_errors().MessageError)(reporter.lang('nodeGypAutoInstallFailed', _context4.t0.message)); case 15: case "end": return _context4.stop(); } }, _callee4, null, [[7, 12]]); })); return _checkForGyp2.apply(this, arguments); } function execFromManifest(_x8, _x9, _x10) { return _execFromManifest.apply(this, arguments); } function _execFromManifest() { _execFromManifest = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee5(config, commandName, cwd) { var pkg, cmd; return _regenerator()["default"].wrap(function _callee5$(_context5) { while (1) switch (_context5.prev = _context5.next) { case 0: _context5.next = 2; return config.maybeReadManifest(cwd); case 2: pkg = _context5.sent; if (!(!pkg || !pkg.scripts)) { _context5.next = 5; break; } return _context5.abrupt("return"); case 5: cmd = pkg.scripts[commandName]; if (!cmd) { _context5.next = 9; break; } _context5.next = 9; return execCommand({ stage: commandName, config: config, cmd: cmd, cwd: cwd, isInteractive: true }); case 9: case "end": return _context5.stop(); } }, _callee5); })); return _execFromManifest.apply(this, arguments); } function execCommand(_x11) { return _execCommand.apply(this, arguments); } function _execCommand() { _execCommand = (0, _asyncToGenerator2()["default"])( /*#__PURE__*/_regenerator()["default"].mark(function _callee6(_ref2) { var stage, config, cmd, cwd, isInteractive, customShell, reporter, formattedError; return _regenerator()["default"].wrap(function _callee6$(_context6) { while (1) switch (_context6.prev = _context6.next) { case 0: stage = _ref2.stage, config = _ref2.config, cmd = _ref2.cmd, cwd = _ref2.cwd, isInteractive = _ref2.isInteractive, customShell = _ref2.customShell; reporter = config.reporter; _context6.prev = 2; reporter.command(cmd); _context6.next = 6; return executeLifecycleScript({ stage: stage, config: config, cwd: cwd, cmd: cmd, isInteractive: isInteractive, customShell: customShell }); case 6: return _context6.abrupt("return", Promise.resolve()); case 9: _context6.prev = 9; _context6.t0 = _context6["catch"](2); if (!(_context6.t0 instanceof _errors().ProcessTermError)) { _context6.next = 18; break; } formattedError = new (_errors().ProcessTermError)(_context6.t0.EXIT_SIGNAL ? reporter.lang('commandFailedWithSignal', _context6.t0.EXIT_SIGNAL) : reporter.lang('commandFailedWithCode', _context6.t0.EXIT_CODE)); formattedError.EXIT_CODE = _context6.t0.EXIT_CODE; formattedError.EXIT_SIGNAL = _context6.t0.EXIT_SIGNAL; throw formattedError; case 18: throw _context6.t0; case 19: case "end": return _context6.stop(); } }, _callee6, null, [[2, 9]]); })); return _execCommand.apply(this, arguments); } //# sourceMappingURL=execute-lifecycle-script.js.map