Skip to content

Commit f514607

Browse files
committed
chore: initial commit
1 parent 947b771 commit f514607

18 files changed

+445
-0
lines changed

.eslintignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dist
2+
package.json
3+
./node_modules/
4+
.vscode
5+
.idea

.eslintrc.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"root": true,
3+
"parserOptions": {
4+
"ecmaVersion": "latest"
5+
},
6+
"env": {
7+
"es6": true
8+
},
9+
"plugins": ["simple-import-sort"],
10+
"overrides": [
11+
{
12+
"parserOptions": {
13+
"project": "./tsconfig.json"
14+
},
15+
"files": [
16+
"lib/src/**/*.ts"
17+
],
18+
"extends": [
19+
"eslint:recommended",
20+
"plugin:@typescript-eslint/recommended",
21+
"plugin:prettier/recommended"
22+
],
23+
"rules": {
24+
"simple-import-sort/imports": "error",
25+
"simple-import-sort/exports": "error",
26+
"@typescript-eslint/ban-ts-comment": 0,
27+
"max-lines-per-function": [
28+
1,
29+
{
30+
"max": 40
31+
}
32+
],
33+
"max-lines": [
34+
1,
35+
{
36+
"max": 150
37+
}
38+
]
39+
}
40+
},
41+
{
42+
"parserOptions": {
43+
"project": "./tsconfig.json"
44+
},
45+
"files": [
46+
"lib/src/**/*.ts"
47+
]
48+
}
49+
]
50+
}

.github/workflows/release.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Release
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
release:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout repository
14+
uses: actions/checkout@v2
15+
16+
- name: Set up Node.js
17+
uses: actions/setup-node@v2
18+
with:
19+
node-version: '20'
20+
21+
- name: Setup Corepack
22+
run: |
23+
corepack enable
24+
corepack prepare npm@10.8.1 --activate
25+
26+
- name: Install dependencies
27+
run: npm install
28+
29+
- name: Build project
30+
run: npm run build
31+
32+
- name: Run Semantic Release
33+
env:
34+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
35+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
36+
run: npm run release

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

.npmignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.idea/
2+
.github
3+
.gitignore
4+
.prettierrc.json
5+
.DS_Store
6+
CONTRIBUTE.md
7+
8+
# configuration
9+
renovate.json
10+
tsconfig.json
11+
/.vscode
12+
.eslintrc.json
13+
.eslintignore

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all",
4+
"endOfLine": "auto",
5+
"printWidth": 100
6+
}

.releaserc.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @type {import('semantic-release').GlobalConfig}
3+
*/
4+
const releaseConfig = {
5+
branches: ["main"],
6+
"plugins": [
7+
"@semantic-release/commit-analyzer",
8+
"@semantic-release/release-notes-generator",
9+
"@semantic-release/changelog",
10+
"@semantic-release/npm",
11+
"@semantic-release/github",
12+
[
13+
"@semantic-release/git",
14+
{
15+
"assets": ["package.json", "CHANGELOG.md"],
16+
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
17+
}
18+
]
19+
]
20+
};
21+
22+
module.exports = releaseConfig;

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# @devmy/cypress-dotenv
2+
3+
`@devmy/cypress-dotenv` is a TypeScript library designed to integrate `dotenv` configuration with `@dotenv-run/core` into your Cypress setup, simplifying the management of environment variables for both component and end-to-end (E2E) tests.
4+
5+
- ✅ Load environment variables from .env files
6+
- ✅ Load environment variables from .env.vault files
7+
- ✅ Expand environment variables API_URL=$API_BASE/users
8+
- ✅ Define environment variables for a specific environment (e.g. .env.production)
9+
- ✅ Load priorities of .env.* files (e.g. .env.production > .env)
10+
- ✅ Hierarchical cascading configuration in monorepo projects (Nx, Turbo, etc.) apps/next-app/.env > apps/.env > .env
11+
12+
## Installation
13+
14+
Install the package via npm:
15+
16+
```bash
17+
npm install @devmy/cypress-dotenv
18+
```
19+
20+
or via yarn:
21+
22+
```bash
23+
yarn add @devmy/cypress-dotenv
24+
```
25+
26+
or via pnpm:
27+
28+
```bash
29+
pnpm add @devmy/cypress-dotenv
30+
```
31+
32+
## Usage
33+
34+
### Dotenv Configuration
35+
The `dotenv` configuration options in this library use `DotenvRunOptions` of [dotenv-run/core](https://www.npmjs.com/package/@dotenv-run/core).
36+
37+
### `defineConfigWithDotenv`
38+
39+
The `defineConfigWithDotenv` function invokes Cypress's `defineConfig`, loading environment variables from `dotenv`.
40+
41+
#### Example:
42+
43+
```typescript
44+
import { defineConfigWithDotenv } from '@devmy/cypress-dotenv';
45+
46+
export default defineConfigWithDotenv({
47+
dotenv: {
48+
prefix: 'FRONTEND_E2E_',
49+
root: '../../',
50+
},
51+
component: {
52+
specPattern: 'src/**/*.cy.ts',
53+
},
54+
e2e: {
55+
baseUrl: 'http://localhost:3000',
56+
},
57+
});
58+
```
59+
60+
### `dotenvComponentPreset`
61+
62+
The `dotenvComponentPreset` function loads `dotenv` for component tests in Cypress.
63+
64+
#### Example:
65+
66+
```typescript
67+
import { dotenvComponentPreset } from '@devmy/cypress-dotenv';
68+
69+
export default dotenvComponentPreset({
70+
dotenv: {
71+
prefix: 'FRONTEND_E2E_',
72+
root: '../../',
73+
},
74+
devServer: {
75+
framework: 'react',
76+
bundler: 'webpack',
77+
},
78+
});
79+
```
80+
81+
### `dotenvE2EPreset`
82+
83+
The `dotenvE2EPreset` function loads `dotenv` for E2E tests in Cypress.
84+
85+
#### Example:
86+
87+
```typescript
88+
import { dotenvE2EPreset } from '@devmy/cypress-dotenv';
89+
90+
export default dotenvE2EPreset({
91+
dotenv: {
92+
prefix: 'FRONTEND_E2E_',
93+
root: '../../',
94+
},
95+
baseUrl: 'http://localhost:3000',
96+
});
97+
```
98+
99+
100+
## License
101+
102+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
103+
104+
## Contributing
105+
106+
Contributions are welcome! Please open an issue or submit a pull request for any changes.

lib/src/define-config.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { DotenvRunOptions } from '@dotenv-run/core';
2+
import { defineConfig } from 'cypress';
3+
4+
import { parseEnvVariables } from './utilities';
5+
6+
export type CyDefineConfig = Parameters<typeof defineConfig>[0];
7+
8+
export type DotenvConfigOptions = CyDefineConfig & {
9+
dotenv: DotenvRunOptions;
10+
};
11+
12+
export const defineConfigWithDotenv = (
13+
dotenvConfigOptions: DotenvConfigOptions,
14+
): CyDefineConfig => {
15+
const { dotenv, ...config } = dotenvConfigOptions;
16+
const oldSetupNodeEvents = config.setupNodeEvents;
17+
18+
config.setupNodeEvents = (onPluginEvent, pluginConfigOptions) => {
19+
const nextPluginConfigOptions = parseEnvVariables(dotenv, pluginConfigOptions);
20+
21+
return oldSetupNodeEvents?.(onPluginEvent, nextPluginConfigOptions);
22+
};
23+
24+
return defineConfig(config);
25+
};

lib/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './define-config';
2+
export * from './presets';
3+
export * from './utilities';

lib/src/presets/component-preset.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { DotenvRunOptions } from '@dotenv-run/core';
2+
import { defineConfig } from 'cypress';
3+
4+
import { parseEnvVariables } from '../utilities';
5+
6+
export type CyDefineComponentConfig = Parameters<typeof defineConfig>[0]['component'];
7+
8+
export type DotenvComponentPresetConfigOptions = CyDefineComponentConfig & {
9+
dotenv?: DotenvRunOptions;
10+
};
11+
12+
export const dotenvComponentPreset = (
13+
dotenvComponentPresetConfigOptions: DotenvComponentPresetConfigOptions,
14+
): CyDefineComponentConfig => {
15+
const { dotenv, ...config } = dotenvComponentPresetConfigOptions;
16+
17+
const oldSetupNodeEvents = config.setupNodeEvents;
18+
19+
config.setupNodeEvents = (onPluginEvent, pluginConfigOptions) => {
20+
const nextPluginConfigOptions = parseEnvVariables(dotenv, pluginConfigOptions);
21+
22+
return oldSetupNodeEvents?.(onPluginEvent, nextPluginConfigOptions);
23+
};
24+
25+
return config;
26+
};

lib/src/presets/e2e-preset.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import type { DotenvRunOptions } from '@dotenv-run/core';
2+
import { defineConfig } from 'cypress';
3+
4+
import { parseEnvVariables } from '../utilities';
5+
6+
export type CyDefineE2EConfig = Parameters<typeof defineConfig>[0]['e2e'];
7+
8+
export type DotenvE2EPresetConfigOptions = CyDefineE2EConfig & {
9+
dotenv?: DotenvRunOptions;
10+
};
11+
12+
export const dotenvE2EPreset = (
13+
dotenvE2EPresetConfigOptions: DotenvE2EPresetConfigOptions,
14+
): CyDefineE2EConfig => {
15+
const { dotenv, ...config } = dotenvE2EPresetConfigOptions;
16+
17+
const oldSetupNodeEvents = config.setupNodeEvents;
18+
19+
config.setupNodeEvents = (onPluginEvent, pluginConfigOptions) => {
20+
const nextPluginConfigOptions = parseEnvVariables(dotenv, pluginConfigOptions);
21+
22+
return oldSetupNodeEvents?.(onPluginEvent, nextPluginConfigOptions);
23+
};
24+
25+
return config;
26+
};

lib/src/presets/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './component-preset';
2+
export * from './e2e-preset';

lib/src/utilities/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './parse-env-variables';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { DotenvRunOptions, env } from '@dotenv-run/core';
2+
import { cloneDeep, entries } from 'lodash';
3+
4+
export const parseEnvVariables = (
5+
options: DotenvRunOptions | undefined,
6+
config: Cypress.PluginConfigOptions,
7+
) => {
8+
const dotenvRun = env(options);
9+
10+
const enhancedConfig = cloneDeep(config);
11+
enhancedConfig.env = enhancedConfig.env ?? {};
12+
13+
entries(dotenvRun.raw).forEach(([key, value]) => (enhancedConfig.env[key] = value));
14+
15+
return enhancedConfig;
16+
};

0 commit comments

Comments
 (0)