@commercetools-frontend/cypress
Version:
Cypress commands and utilities for Custom Applications
109 lines (105 loc) • 5.92 kB
JavaScript
import _Object$keys from '@babel/runtime-corejs3/core-js-stable/object/keys';
import _Object$getOwnPropertySymbols from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols';
import _filterInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/filter';
import _Object$getOwnPropertyDescriptor from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor';
import _forEachInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/for-each';
import _Object$getOwnPropertyDescriptors from '@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptors';
import _Object$defineProperties from '@babel/runtime-corejs3/core-js-stable/object/define-properties';
import _Object$defineProperty from '@babel/runtime-corejs3/core-js-stable/object/define-property';
import _defineProperty from '@babel/runtime-corejs3/helpers/esm/defineProperty';
import _reduceInstanceProperty from '@babel/runtime-corejs3/core-js-stable/instance/reduce';
import _Promise from '@babel/runtime-corejs3/core-js-stable/promise';
import fs from 'fs';
import path from 'path';
import { getPackages } from '@manypkg/get-packages';
import { processConfig, MissingOrInvalidConfigError } from '@commercetools-frontend/application-config';
import { CUSTOM_VIEW_HOST_ENTRY_POINT_URI_PATH } from '@commercetools-frontend/constants';
function ownKeys(e, r) { var t = _Object$keys(e); if (_Object$getOwnPropertySymbols) { var o = _Object$getOwnPropertySymbols(e); r && (o = _filterInstanceProperty(o).call(o, function (r) { return _Object$getOwnPropertyDescriptor(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(_context = ownKeys(Object(t), !0)).call(_context, function (r) { _defineProperty(e, r, t[r]); }) : _Object$getOwnPropertyDescriptors ? _Object$defineProperties(e, _Object$getOwnPropertyDescriptors(t)) : _forEachInstanceProperty(_context2 = ownKeys(Object(t))).call(_context2, function (r) { _Object$defineProperty(e, r, _Object$getOwnPropertyDescriptor(t, r)); }); } return e; }
function doesFileExist(path) {
try {
fs.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(dotfiles).call(dotfiles, (mergedEnvs, dotfile) => {
const envPath = path.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(process.cwd()),
packages = _await$getPackages.packages;
cachedAllCustomEntityConfigs = await _reduceInstanceProperty(packages).call(packages, async (allConfigsPromise, packageInfo) => {
const allConfigs = await allConfigsPromise;
const processEnv = loadEnvironmentVariables(packageInfo.dir, options);
try {
const processedConfig = await 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 MissingOrInvalidConfigError) {
return allConfigs;
}
throw error;
}
}, _Promise.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: CUSTOM_VIEW_HOST_ENTRY_POINT_URI_PATH
}));
const customViewConfig = allCustomEntityConfigs[`${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
};
export { customApplicationConfig, customViewConfig, legacyConfig as default };