UNPKG

@commercetools-frontend/cypress

Version:
130 lines (122 loc) • 7.58 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var _Object$keys = require('@babel/runtime-corejs3/core-js-stable/object/keys'); var _Object$getOwnPropertySymbols = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols'); var _filterInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/filter'); var _Object$getOwnPropertyDescriptor = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor'); var _forEachInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/for-each'); var _Object$getOwnPropertyDescriptors = require('@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors'); var _Object$defineProperties = require('@babel/runtime-corejs3/core-js-stable/object/define-properties'); var _Object$defineProperty = require('@babel/runtime-corejs3/core-js-stable/object/define-property'); var _defineProperty = require('@babel/runtime-corejs3/helpers/defineProperty'); var _reduceInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/reduce'); var _Promise = require('@babel/runtime-corejs3/core-js-stable/promise'); var fs = require('fs'); var path = require('path'); var getPackages = require('@manypkg/get-packages'); var applicationConfig = require('@commercetools-frontend/application-config'); var constants = require('@commercetools-frontend/constants'); function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; } var _Object$keys__default = /*#__PURE__*/_interopDefault(_Object$keys); var _Object$getOwnPropertySymbols__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertySymbols); var _filterInstanceProperty__default = /*#__PURE__*/_interopDefault(_filterInstanceProperty); var _Object$getOwnPropertyDescriptor__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptor); var _forEachInstanceProperty__default = /*#__PURE__*/_interopDefault(_forEachInstanceProperty); var _Object$getOwnPropertyDescriptors__default = /*#__PURE__*/_interopDefault(_Object$getOwnPropertyDescriptors); var _Object$defineProperties__default = /*#__PURE__*/_interopDefault(_Object$defineProperties); var _Object$defineProperty__default = /*#__PURE__*/_interopDefault(_Object$defineProperty); var _reduceInstanceProperty__default = /*#__PURE__*/_interopDefault(_reduceInstanceProperty); var _Promise__default = /*#__PURE__*/_interopDefault(_Promise); var fs__default = /*#__PURE__*/_interopDefault(fs); var path__default = /*#__PURE__*/_interopDefault(path); function ownKeys(e, r) { var t = _Object$keys__default["default"](e); if (_Object$getOwnPropertySymbols__default["default"]) { var o = _Object$getOwnPropertySymbols__default["default"](e); r && (o = _filterInstanceProperty__default["default"](o).call(o, function (r) { return _Object$getOwnPropertyDescriptor__default["default"](e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var _context, _context2; var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? _forEachInstanceProperty__default["default"](_context = ownKeys(Object(t), !0)).call(_context, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors__default["default"] ? _Object$defineProperties__default["default"](e, _Object$getOwnPropertyDescriptors__default["default"](t)) : _forEachInstanceProperty__default["default"](_context2 = ownKeys(Object(t))).call(_context2, function (r) { _Object$defineProperty__default["default"](e, r, _Object$getOwnPropertyDescriptor__default["default"](t, r)); }); } return e; } function doesFileExist(path) { try { fs__default["default"].accessSync(path); return true; } catch (error) { return false; } } let cachedAllCustomEntityConfigs; const defaultDotfiles = ['.env', '.env.local']; const loadEnvironmentVariables = (packageDirPath, options) => { const dotfiles = options.dotfiles ?? defaultDotfiles; return _reduceInstanceProperty__default["default"](dotfiles).call(dotfiles, (mergedEnvs, dotfile) => { const envPath = path__default["default"].join(packageDirPath, dotfile); if (!doesFileExist(envPath)) { return mergedEnvs; } const env = require('dotenv').config({ path: envPath }); if (env.error) { console.error(`Failed to load environment variables from ${envPath}`); return mergedEnvs; } console.log(`Loading environment variables from ${envPath}`); return _objectSpread(_objectSpread({}, mergedEnvs), env.parsed); // Merge it with the environment variables defined in the current process. }, process.env); }; const loadAllCustomEntityConfigs = async options => { if (cachedAllCustomEntityConfigs) { return cachedAllCustomEntityConfigs; } const _await$getPackages = await getPackages.getPackages(process.cwd()), packages = _await$getPackages.packages; cachedAllCustomEntityConfigs = await _reduceInstanceProperty__default["default"](packages).call(packages, async (allConfigsPromise, packageInfo) => { const allConfigs = await allConfigsPromise; const processEnv = loadEnvironmentVariables(packageInfo.dir, options); try { const processedConfig = await applicationConfig.processConfig({ disableCache: true, processEnv, applicationPath: packageInfo.dir }); const isCustomViewConfig = Boolean(processedConfig.env.customViewId); console.log(`Found Custom ${isCustomViewConfig ? 'View' : 'Application'} config for ${packageInfo.packageJson.name}`); const customEntityConfigCacheKey = isCustomViewConfig ? `${processedConfig.env.entryPointUriPath}-${packageInfo.packageJson.name}` : processedConfig.env.entryPointUriPath; return _objectSpread(_objectSpread({}, allConfigs), {}, { [customEntityConfigCacheKey]: processedConfig.env }); } catch (error) { // Ignore packages that do not have a valid config file, either because // the package is not a Custom Entity or because the config file // is invalid. if (error instanceof applicationConfig.MissingOrInvalidConfigError) { return allConfigs; } throw error; } }, _Promise__default["default"].resolve({})); return cachedAllCustomEntityConfigs; }; const customApplicationConfig = async options => { const allCustomEntityConfigs = await loadAllCustomEntityConfigs(options); const customApplicationConfig = allCustomEntityConfigs[options.entryPointUriPath]; if (!customApplicationConfig) { throw new Error(`Could not find Custom Application config for entry point "${options.entryPointUriPath}"`); } console.log(`Using Custom Application config for "${options.entryPointUriPath}"`); return customApplicationConfig; }; const customViewConfig = async options => { const allCustomEntityConfigs = await loadAllCustomEntityConfigs(_objectSpread(_objectSpread({}, options), {}, { entryPointUriPath: constants.CUSTOM_VIEW_HOST_ENTRY_POINT_URI_PATH })); const customViewConfig = allCustomEntityConfigs[`${constants.CUSTOM_VIEW_HOST_ENTRY_POINT_URI_PATH}-${options.packageName}`]; if (!customViewConfig) { throw new Error(`Could not find Custom View config`); } console.log(`Using Custom View config for "${options.packageName}"`); return customViewConfig; }; // for backwards compatibility const legacyConfig = { customApplicationConfig }; exports.customApplicationConfig = customApplicationConfig; exports.customViewConfig = customViewConfig; exports["default"] = legacyConfig;