@commercetools-frontend/cypress-environments 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +97 -0
- package/dist/commercetools-frontend-cypress-environments.cjs.dev.js +108 -0
- package/dist/commercetools-frontend-cypress-environments.cjs.js +7 -0
- package/dist/commercetools-frontend-cypress-environments.cjs.prod.js +108 -0
- package/dist/commercetools-frontend-cypress-environments.esm.js +96 -0
- package/dist/config/.env.ctp-aws-production-fra.config +4 -0
- package/dist/config/.env.ctp-gcp-production-au.config +5 -0
- package/dist/config/.env.ctp-gcp-production-eu.config +5 -0
- package/dist/config/.env.ctp-gcp-production-us.config +5 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) commercetools GmbH
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# @commercetools-frontend/cypress-environments
|
|
2
|
+
|
|
3
|
+
> This package allows to load environment variables into a Cypress environment from [dotenv](https://github.com/motdotla/dotenv) files.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
$ npm install --save @commercetools-frontend/cypress-environments
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Inside of your `cypress.config.js` import the package as follows:
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
const {
|
|
17
|
+
getConfigurationForEnvironment,
|
|
18
|
+
} = require('@commercetools-frontend/cypress-environments');
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Then inside your `e2e.setupNodeEvents` in Cypress v10.x load the configuration by calling `await getConfigurationForEnvironment(environment)`.
|
|
22
|
+
|
|
23
|
+
This will return an object with the merged configuration based on the requested environment.
|
|
24
|
+
|
|
25
|
+
## Adding a configuration file
|
|
26
|
+
|
|
27
|
+
This module uses [cosmiconfig](https://github.com/davidtheclark/cosmiconfig) to load a configuration file named `cypress-environments`. As a result it supports many formats such as `cypress-environments.config.cjs` or `cypress-environments.rc.json` depending on your preference.
|
|
28
|
+
|
|
29
|
+
Within the configuration file an array `environments` property should be defined containing a set of environments with `name`, `secrets` and `config` globs.
|
|
30
|
+
|
|
31
|
+
The contents of the configuration file could look like:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
const projectKeys = {
|
|
35
|
+
gcpEu: 'ctp-gcp-production-eu',
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
module.exports = {
|
|
39
|
+
environments: [
|
|
40
|
+
{
|
|
41
|
+
name: projectKeys.gcpEu,
|
|
42
|
+
secrets: `cypress/config/${projectKeys.gcpEu}/.env.secrets*`,
|
|
43
|
+
config: `cypress/config/${projectKeys.gcpEu}/.env.config*`,
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
};
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
From here the documentation assumes the above format and naming for further examples.
|
|
50
|
+
|
|
51
|
+
## Adding environment configurations
|
|
52
|
+
|
|
53
|
+
The package expects configurations for environments to be stored in `cypress/config/<environment_name>`. For example this structure:
|
|
54
|
+
|
|
55
|
+
```txt
|
|
56
|
+
cypress/config
|
|
57
|
+
├── ctp-gcp-production-eu
|
|
58
|
+
|──── .env.config
|
|
59
|
+
|──── .env.secrets.ci (Decrypred from `*.enc`)
|
|
60
|
+
|──── .env.config.local.template
|
|
61
|
+
|──── .env.secrets.local.template
|
|
62
|
+
├── ctp-aws-production-fra
|
|
63
|
+
└── ctp-vw-production-eu
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Would load the values from `.env.config` anytime the environment is `ctp-gcp-production-eu`. Additionally on CI (when `CI=true`) it would load the `.env.secrets.ci` file, otherwise it would attempt to load values from `.env.config.local` and `.env.secrets.local` (assuming they have been properly configured - see `*.template` files for reference).
|
|
67
|
+
|
|
68
|
+
Given multiple files can be loaded some values can be overwritten by a subsequently loaded file. First the `.env.config` file will be loaded. After the secrets from `.env.secrets` and then `.env.secrets.ci` or `.env.secrets.local` respectively. Any file loaded later can overwrite values of a file loaded before.
|
|
69
|
+
|
|
70
|
+
## Provided configuration values
|
|
71
|
+
|
|
72
|
+
The module provides default values for `ctp-aws-production-fra`, `ctp-gcp-production-eu`, `ctp-gcp-production-us` and `ctp-gcp-production-au`. These values will be loaded first before any custom configuration. These default configurations contain the `API_URL`, `MC_API_URL`, `MC_URL` and `AUTH_URL` for the given environment.
|
|
73
|
+
|
|
74
|
+
## Troubleshooting
|
|
75
|
+
|
|
76
|
+
### Configuration does not load as expected
|
|
77
|
+
|
|
78
|
+
Inspect the logs of the module to inspect which files match the globs. This output could be:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
ℹ️ 'CI' environment variables are defined. Assuming operating from a CI system.
|
|
82
|
+
ℹ️ Found 1 secret file(s) and 1 config file(s) matching the defined globs.
|
|
83
|
+
✅ Found and loading environment variables from: 'cypress/config/ctp-gcp-production-eu/.env.config'
|
|
84
|
+
ℹ️ No environment variables at: 'cypress/config/ctp-gcp-production-eu/.env.config.ci'. If needed create it or duplicate the template file.
|
|
85
|
+
✅ Found and loading environment variables from: 'cypress/config/ctp-gcp-production-eu/.env.secrets.ci'
|
|
86
|
+
✅ Found and loading environment variables from: '/Users/<username>/merchant-center-frontend/packages-cypress/environments/src/config/.env.ctp-gcp-production-eu.config'
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Verify that the files are loaded as expected and make sure they exist.
|
|
90
|
+
|
|
91
|
+
### Secrets are not loaded
|
|
92
|
+
|
|
93
|
+
Make sure the secrets are decrypted on CI from the `*.enc` or locally a `*.template` file for the secrets has been duplicated and filled.
|
|
94
|
+
|
|
95
|
+
### Local configuration and secrets are loaded on CI
|
|
96
|
+
|
|
97
|
+
Ensure that the `CI` environment variable is set. Most CI providers set it by default but you can also pass it to the command when running Cypress.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var path = require('path');
|
|
6
|
+
var fs = require('fs');
|
|
7
|
+
var url = require('url');
|
|
8
|
+
var util = require('util');
|
|
9
|
+
var glob = require('glob');
|
|
10
|
+
var dotenv = require('dotenv');
|
|
11
|
+
var cosmiconfig = require('cosmiconfig');
|
|
12
|
+
|
|
13
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
14
|
+
|
|
15
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
16
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
17
|
+
var url__default = /*#__PURE__*/_interopDefault(url);
|
|
18
|
+
var glob__default = /*#__PURE__*/_interopDefault(glob);
|
|
19
|
+
var dotenv__default = /*#__PURE__*/_interopDefault(dotenv);
|
|
20
|
+
|
|
21
|
+
const cypressEnvironmentsConfigExplorer = cosmiconfig.cosmiconfig('cypress-environments');
|
|
22
|
+
const isCI = process.env.CI === true || process.env.CI === 'true';
|
|
23
|
+
const promisifiedGlob = util.promisify(glob__default["default"]);
|
|
24
|
+
|
|
25
|
+
const __dirname$1 = path__default["default"].dirname(url__default["default"].fileURLToPath((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('dist/commercetools-frontend-cypress-environments.cjs.dev.js', document.baseURI).href))));
|
|
26
|
+
|
|
27
|
+
function getParsedEnvFileContents(envPath) {
|
|
28
|
+
if (fs__default["default"].existsSync(envPath)) {
|
|
29
|
+
console.log(`✅ Found and loading environment variables from: '${envPath}'`);
|
|
30
|
+
const envFileContents = fs__default["default"].readFileSync(envPath, {
|
|
31
|
+
encoding: 'utf8'
|
|
32
|
+
});
|
|
33
|
+
const configuration = dotenv__default["default"].parse(envFileContents);
|
|
34
|
+
return configuration;
|
|
35
|
+
} else {
|
|
36
|
+
console.log(`ℹ️ No environment variables at: '${envPath}'. If needed create it or duplicate the template file.`);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function loadConfig() {
|
|
42
|
+
try {
|
|
43
|
+
return await cypressEnvironmentsConfigExplorer.search();
|
|
44
|
+
} catch (e) {
|
|
45
|
+
throw new Error(`Failed loading a Cypress environments configuration. Create a cosmiconfig for 'cypress-environments' for example 'cypress-environments.config.cjs'.`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function getConfigurationForEnvironment(environmentName) {
|
|
50
|
+
if (isCI) {
|
|
51
|
+
console.log(`ℹ️ 'CI' environment variables are defined. Assuming operating from a CI system.`);
|
|
52
|
+
} else {
|
|
53
|
+
console.log(`ℹ️ 'CI' environment variables are not defined. Assuming you are running locally.`);
|
|
54
|
+
} // NOTE: On CI the *.ci secrests are not excluded. Locally the are in case
|
|
55
|
+
// decrypted files are available on a developer's machine.
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
const ignoredGlobs = ['**/*.enc', '**/*.template', !isCI && '**/*.ci'].filter(Boolean);
|
|
59
|
+
const {
|
|
60
|
+
config: allEnvironmentConfigs
|
|
61
|
+
} = await loadConfig();
|
|
62
|
+
const configForEnvironment = allEnvironmentConfigs.environments.find(environment => environment.name === environmentName);
|
|
63
|
+
|
|
64
|
+
if (!configForEnvironment) {
|
|
65
|
+
throw new Error(`No configuration for ${environmentName} defined in cosmiconfig for 'cypress-environments'. Please make sure it exists.`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const secretsFiles = await promisifiedGlob(configForEnvironment.secrets.glob, {
|
|
69
|
+
ignore: ignoredGlobs
|
|
70
|
+
});
|
|
71
|
+
const configFiles = await promisifiedGlob(configForEnvironment.config.glob, {
|
|
72
|
+
ignore: ignoredGlobs
|
|
73
|
+
});
|
|
74
|
+
console.log(`ℹ️ Found ${secretsFiles.length} secret file(s) and ${configFiles.length} config file(s) matching the defined globs.`);
|
|
75
|
+
|
|
76
|
+
if (secretsFiles.length === 0 || configFiles.length === 0) {
|
|
77
|
+
console.log(`ℹ️ Secrets files glob is: ${configForEnvironment.secrets.glob}`);
|
|
78
|
+
console.log(`ℹ️ Config files glob is: ${configForEnvironment.config.glob}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const mergedConfigurationAndSecrests = [...configFiles, ...secretsFiles].reduce((previousParsedEnvFileContents, nextEnvFilePath) => {
|
|
82
|
+
const parsedEnvFileContents = getParsedEnvFileContents(nextEnvFilePath);
|
|
83
|
+
let matchingParsedEnvFileContentsForCIOrLocal = {};
|
|
84
|
+
|
|
85
|
+
if (!nextEnvFilePath.endsWith('.local') && !nextEnvFilePath.endsWith('.ci')) {
|
|
86
|
+
matchingParsedEnvFileContentsForCIOrLocal = isCI ? getParsedEnvFileContents(`${nextEnvFilePath}.ci`) : getParsedEnvFileContents(`${nextEnvFilePath}.local`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return { ...previousParsedEnvFileContents,
|
|
90
|
+
...parsedEnvFileContents,
|
|
91
|
+
...matchingParsedEnvFileContentsForCIOrLocal
|
|
92
|
+
};
|
|
93
|
+
}, {});
|
|
94
|
+
const pathToSharedEnvironmentConfiguration = path__default["default"].join(__dirname$1, 'config', `.env.${environmentName}.config`);
|
|
95
|
+
let sharedEnvironmentConfiguration = null;
|
|
96
|
+
|
|
97
|
+
if (fs__default["default"].existsSync(pathToSharedEnvironmentConfiguration)) {
|
|
98
|
+
sharedEnvironmentConfiguration = getParsedEnvFileContents(pathToSharedEnvironmentConfiguration);
|
|
99
|
+
} else {
|
|
100
|
+
console.log(`ℹ️ Not shared configuration for ${environmentName} defined.`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return { ...sharedEnvironmentConfiguration,
|
|
104
|
+
...mergedConfigurationAndSecrests
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
exports.getConfigurationForEnvironment = getConfigurationForEnvironment;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var path = require('path');
|
|
6
|
+
var fs = require('fs');
|
|
7
|
+
var url = require('url');
|
|
8
|
+
var util = require('util');
|
|
9
|
+
var glob = require('glob');
|
|
10
|
+
var dotenv = require('dotenv');
|
|
11
|
+
var cosmiconfig = require('cosmiconfig');
|
|
12
|
+
|
|
13
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
|
|
14
|
+
|
|
15
|
+
var path__default = /*#__PURE__*/_interopDefault(path);
|
|
16
|
+
var fs__default = /*#__PURE__*/_interopDefault(fs);
|
|
17
|
+
var url__default = /*#__PURE__*/_interopDefault(url);
|
|
18
|
+
var glob__default = /*#__PURE__*/_interopDefault(glob);
|
|
19
|
+
var dotenv__default = /*#__PURE__*/_interopDefault(dotenv);
|
|
20
|
+
|
|
21
|
+
const cypressEnvironmentsConfigExplorer = cosmiconfig.cosmiconfig('cypress-environments');
|
|
22
|
+
const isCI = process.env.CI === true || process.env.CI === 'true';
|
|
23
|
+
const promisifiedGlob = util.promisify(glob__default["default"]);
|
|
24
|
+
|
|
25
|
+
const __dirname$1 = path__default["default"].dirname(url__default["default"].fileURLToPath((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('dist/commercetools-frontend-cypress-environments.cjs.prod.js', document.baseURI).href))));
|
|
26
|
+
|
|
27
|
+
function getParsedEnvFileContents(envPath) {
|
|
28
|
+
if (fs__default["default"].existsSync(envPath)) {
|
|
29
|
+
console.log(`✅ Found and loading environment variables from: '${envPath}'`);
|
|
30
|
+
const envFileContents = fs__default["default"].readFileSync(envPath, {
|
|
31
|
+
encoding: 'utf8'
|
|
32
|
+
});
|
|
33
|
+
const configuration = dotenv__default["default"].parse(envFileContents);
|
|
34
|
+
return configuration;
|
|
35
|
+
} else {
|
|
36
|
+
console.log(`ℹ️ No environment variables at: '${envPath}'. If needed create it or duplicate the template file.`);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async function loadConfig() {
|
|
42
|
+
try {
|
|
43
|
+
return await cypressEnvironmentsConfigExplorer.search();
|
|
44
|
+
} catch (e) {
|
|
45
|
+
throw new Error(`Failed loading a Cypress environments configuration. Create a cosmiconfig for 'cypress-environments' for example 'cypress-environments.config.cjs'.`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function getConfigurationForEnvironment(environmentName) {
|
|
50
|
+
if (isCI) {
|
|
51
|
+
console.log(`ℹ️ 'CI' environment variables are defined. Assuming operating from a CI system.`);
|
|
52
|
+
} else {
|
|
53
|
+
console.log(`ℹ️ 'CI' environment variables are not defined. Assuming you are running locally.`);
|
|
54
|
+
} // NOTE: On CI the *.ci secrests are not excluded. Locally the are in case
|
|
55
|
+
// decrypted files are available on a developer's machine.
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
const ignoredGlobs = ['**/*.enc', '**/*.template', !isCI && '**/*.ci'].filter(Boolean);
|
|
59
|
+
const {
|
|
60
|
+
config: allEnvironmentConfigs
|
|
61
|
+
} = await loadConfig();
|
|
62
|
+
const configForEnvironment = allEnvironmentConfigs.environments.find(environment => environment.name === environmentName);
|
|
63
|
+
|
|
64
|
+
if (!configForEnvironment) {
|
|
65
|
+
throw new Error(`No configuration for ${environmentName} defined in cosmiconfig for 'cypress-environments'. Please make sure it exists.`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const secretsFiles = await promisifiedGlob(configForEnvironment.secrets.glob, {
|
|
69
|
+
ignore: ignoredGlobs
|
|
70
|
+
});
|
|
71
|
+
const configFiles = await promisifiedGlob(configForEnvironment.config.glob, {
|
|
72
|
+
ignore: ignoredGlobs
|
|
73
|
+
});
|
|
74
|
+
console.log(`ℹ️ Found ${secretsFiles.length} secret file(s) and ${configFiles.length} config file(s) matching the defined globs.`);
|
|
75
|
+
|
|
76
|
+
if (secretsFiles.length === 0 || configFiles.length === 0) {
|
|
77
|
+
console.log(`ℹ️ Secrets files glob is: ${configForEnvironment.secrets.glob}`);
|
|
78
|
+
console.log(`ℹ️ Config files glob is: ${configForEnvironment.config.glob}`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const mergedConfigurationAndSecrests = [...configFiles, ...secretsFiles].reduce((previousParsedEnvFileContents, nextEnvFilePath) => {
|
|
82
|
+
const parsedEnvFileContents = getParsedEnvFileContents(nextEnvFilePath);
|
|
83
|
+
let matchingParsedEnvFileContentsForCIOrLocal = {};
|
|
84
|
+
|
|
85
|
+
if (!nextEnvFilePath.endsWith('.local') && !nextEnvFilePath.endsWith('.ci')) {
|
|
86
|
+
matchingParsedEnvFileContentsForCIOrLocal = isCI ? getParsedEnvFileContents(`${nextEnvFilePath}.ci`) : getParsedEnvFileContents(`${nextEnvFilePath}.local`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return { ...previousParsedEnvFileContents,
|
|
90
|
+
...parsedEnvFileContents,
|
|
91
|
+
...matchingParsedEnvFileContentsForCIOrLocal
|
|
92
|
+
};
|
|
93
|
+
}, {});
|
|
94
|
+
const pathToSharedEnvironmentConfiguration = path__default["default"].join(__dirname$1, 'config', `.env.${environmentName}.config`);
|
|
95
|
+
let sharedEnvironmentConfiguration = null;
|
|
96
|
+
|
|
97
|
+
if (fs__default["default"].existsSync(pathToSharedEnvironmentConfiguration)) {
|
|
98
|
+
sharedEnvironmentConfiguration = getParsedEnvFileContents(pathToSharedEnvironmentConfiguration);
|
|
99
|
+
} else {
|
|
100
|
+
console.log(`ℹ️ Not shared configuration for ${environmentName} defined.`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return { ...sharedEnvironmentConfiguration,
|
|
104
|
+
...mergedConfigurationAndSecrests
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
exports.getConfigurationForEnvironment = getConfigurationForEnvironment;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import url from 'url';
|
|
4
|
+
import { promisify } from 'util';
|
|
5
|
+
import glob from 'glob';
|
|
6
|
+
import dotenv from 'dotenv';
|
|
7
|
+
import { cosmiconfig } from 'cosmiconfig';
|
|
8
|
+
|
|
9
|
+
const cypressEnvironmentsConfigExplorer = cosmiconfig('cypress-environments');
|
|
10
|
+
const isCI = process.env.CI === true || process.env.CI === 'true';
|
|
11
|
+
const promisifiedGlob = promisify(glob);
|
|
12
|
+
|
|
13
|
+
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
|
|
14
|
+
|
|
15
|
+
function getParsedEnvFileContents(envPath) {
|
|
16
|
+
if (fs.existsSync(envPath)) {
|
|
17
|
+
console.log(`✅ Found and loading environment variables from: '${envPath}'`);
|
|
18
|
+
const envFileContents = fs.readFileSync(envPath, {
|
|
19
|
+
encoding: 'utf8'
|
|
20
|
+
});
|
|
21
|
+
const configuration = dotenv.parse(envFileContents);
|
|
22
|
+
return configuration;
|
|
23
|
+
} else {
|
|
24
|
+
console.log(`ℹ️ No environment variables at: '${envPath}'. If needed create it or duplicate the template file.`);
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function loadConfig() {
|
|
30
|
+
try {
|
|
31
|
+
return await cypressEnvironmentsConfigExplorer.search();
|
|
32
|
+
} catch (e) {
|
|
33
|
+
throw new Error(`Failed loading a Cypress environments configuration. Create a cosmiconfig for 'cypress-environments' for example 'cypress-environments.config.cjs'.`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function getConfigurationForEnvironment(environmentName) {
|
|
38
|
+
if (isCI) {
|
|
39
|
+
console.log(`ℹ️ 'CI' environment variables are defined. Assuming operating from a CI system.`);
|
|
40
|
+
} else {
|
|
41
|
+
console.log(`ℹ️ 'CI' environment variables are not defined. Assuming you are running locally.`);
|
|
42
|
+
} // NOTE: On CI the *.ci secrests are not excluded. Locally the are in case
|
|
43
|
+
// decrypted files are available on a developer's machine.
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
const ignoredGlobs = ['**/*.enc', '**/*.template', !isCI && '**/*.ci'].filter(Boolean);
|
|
47
|
+
const {
|
|
48
|
+
config: allEnvironmentConfigs
|
|
49
|
+
} = await loadConfig();
|
|
50
|
+
const configForEnvironment = allEnvironmentConfigs.environments.find(environment => environment.name === environmentName);
|
|
51
|
+
|
|
52
|
+
if (!configForEnvironment) {
|
|
53
|
+
throw new Error(`No configuration for ${environmentName} defined in cosmiconfig for 'cypress-environments'. Please make sure it exists.`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const secretsFiles = await promisifiedGlob(configForEnvironment.secrets.glob, {
|
|
57
|
+
ignore: ignoredGlobs
|
|
58
|
+
});
|
|
59
|
+
const configFiles = await promisifiedGlob(configForEnvironment.config.glob, {
|
|
60
|
+
ignore: ignoredGlobs
|
|
61
|
+
});
|
|
62
|
+
console.log(`ℹ️ Found ${secretsFiles.length} secret file(s) and ${configFiles.length} config file(s) matching the defined globs.`);
|
|
63
|
+
|
|
64
|
+
if (secretsFiles.length === 0 || configFiles.length === 0) {
|
|
65
|
+
console.log(`ℹ️ Secrets files glob is: ${configForEnvironment.secrets.glob}`);
|
|
66
|
+
console.log(`ℹ️ Config files glob is: ${configForEnvironment.config.glob}`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const mergedConfigurationAndSecrests = [...configFiles, ...secretsFiles].reduce((previousParsedEnvFileContents, nextEnvFilePath) => {
|
|
70
|
+
const parsedEnvFileContents = getParsedEnvFileContents(nextEnvFilePath);
|
|
71
|
+
let matchingParsedEnvFileContentsForCIOrLocal = {};
|
|
72
|
+
|
|
73
|
+
if (!nextEnvFilePath.endsWith('.local') && !nextEnvFilePath.endsWith('.ci')) {
|
|
74
|
+
matchingParsedEnvFileContentsForCIOrLocal = isCI ? getParsedEnvFileContents(`${nextEnvFilePath}.ci`) : getParsedEnvFileContents(`${nextEnvFilePath}.local`);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return { ...previousParsedEnvFileContents,
|
|
78
|
+
...parsedEnvFileContents,
|
|
79
|
+
...matchingParsedEnvFileContentsForCIOrLocal
|
|
80
|
+
};
|
|
81
|
+
}, {});
|
|
82
|
+
const pathToSharedEnvironmentConfiguration = path.join(__dirname, 'config', `.env.${environmentName}.config`);
|
|
83
|
+
let sharedEnvironmentConfiguration = null;
|
|
84
|
+
|
|
85
|
+
if (fs.existsSync(pathToSharedEnvironmentConfiguration)) {
|
|
86
|
+
sharedEnvironmentConfiguration = getParsedEnvFileContents(pathToSharedEnvironmentConfiguration);
|
|
87
|
+
} else {
|
|
88
|
+
console.log(`ℹ️ Not shared configuration for ${environmentName} defined.`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return { ...sharedEnvironmentConfiguration,
|
|
92
|
+
...mergedConfigurationAndSecrests
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { getConfigurationForEnvironment };
|
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@commercetools-frontend/cypress-environments",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "Cypress package to setup environment configuration using dotenv files",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"scripts": {
|
|
10
|
+
"prepublish": "cp -R src/config dist/config"
|
|
11
|
+
},
|
|
12
|
+
"main": "dist/commercetools-frontend-cypress-environments.cjs.js",
|
|
13
|
+
"module": "dist/commercetools-frontend-cypress-environments.esm.js",
|
|
14
|
+
"preconstruct": {
|
|
15
|
+
"entrypoints": [
|
|
16
|
+
"./index.js"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"package.json",
|
|
22
|
+
"LICENSE",
|
|
23
|
+
"README.md"
|
|
24
|
+
],
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"cosmiconfig": "7.0.1",
|
|
27
|
+
"dotenv": "16.0.2",
|
|
28
|
+
"glob": "8.0.3"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=14",
|
|
32
|
+
"npm": ">=5"
|
|
33
|
+
}
|
|
34
|
+
}
|