@cogs/config 0.2.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/README.md +101 -0
- package/dist/correct-env.d.ts +20 -0
- package/dist/correct-env.d.ts.map +1 -0
- package/dist/correct-env.js +41 -0
- package/dist/correct-env.js.map +1 -0
- package/dist/dotenv-checkers.d.ts +53 -0
- package/dist/dotenv-checkers.d.ts.map +1 -0
- package/dist/dotenv-checkers.js +106 -0
- package/dist/dotenv-checkers.js.map +1 -0
- package/dist/dotenv-fixers.d.ts +17 -0
- package/dist/dotenv-fixers.d.ts.map +1 -0
- package/dist/dotenv-fixers.js +80 -0
- package/dist/dotenv-fixers.js.map +1 -0
- package/dist/dotenv-with-expand.d.ts +53 -0
- package/dist/dotenv-with-expand.d.ts.map +1 -0
- package/dist/dotenv-with-expand.js +115 -0
- package/dist/dotenv-with-expand.js.map +1 -0
- package/dist/envs.d.ts +24 -0
- package/dist/envs.d.ts.map +1 -0
- package/dist/envs.js +63 -0
- package/dist/envs.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/preload.d.ts +2 -0
- package/dist/preload.d.ts.map +1 -0
- package/dist/preload.js +17 -0
- package/dist/preload.js.map +1 -0
- package/dist/process-env.d.ts +12 -0
- package/dist/process-env.d.ts.map +1 -0
- package/dist/process-env.js +36 -0
- package/dist/process-env.js.map +1 -0
- package/dist/utils.d.ts +20 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +55 -0
- package/dist/utils.js.map +1 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# @cogs/config
|
|
2
|
+
|
|
3
|
+
The framework-agnostic loader behind the canonical environment schema. It
|
|
4
|
+
resolves the layered `.env` file chain, expands `${VAR}` references, and folds
|
|
5
|
+
the result into `process.env` — with deploy-injected variables staying
|
|
6
|
+
authoritative by default. One loader, one precedence, no per-repo forks.
|
|
7
|
+
|
|
8
|
+
Runtime dependencies: `dotenv`, `dotenv-expand`. Node 20+ (the `preload` entry
|
|
9
|
+
needs Node ≥ 20.6 for `--import`).
|
|
10
|
+
|
|
11
|
+
See [`docs/ENVIRONMENT-CONFIG.md`](../../../next-starters/docs/ENVIRONMENT-CONFIG.md)
|
|
12
|
+
for the full spec and [ADR 0001](../../../next-starters/docs/adr/0001-canonical-env-schema.md)
|
|
13
|
+
for the rationale.
|
|
14
|
+
|
|
15
|
+
## Load order
|
|
16
|
+
|
|
17
|
+
Files are merged low → high precedence (later overrides earlier):
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
.env base defaults committed, non-secret
|
|
21
|
+
.env.local personal overrides gitignored (skipped when NODE_ENV=test)
|
|
22
|
+
.env.${ENV} stage config committed, non-secret
|
|
23
|
+
secretsPath injected secrets e.g. secrets.json (optional, parsed as JSON when *.json)
|
|
24
|
+
.env.${ENV}.local stage + personal gitignored
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
- `${VAR}` interpolation works across every file (`dotenv-expand`).
|
|
28
|
+
- **Default `override: false`** — a value already present in `process.env`
|
|
29
|
+
(K8s ConfigMap, Vercel, CI) beats every file; dotenv files only fill gaps.
|
|
30
|
+
Pass `override: true` for tooling that must force file values over the ambient
|
|
31
|
+
environment.
|
|
32
|
+
- **`.env.local` is skipped when `NODE_ENV === 'test'`** so personal-machine
|
|
33
|
+
overrides never leak into a deterministic test run.
|
|
34
|
+
- `${ENV}` is the stage selector (`dev` | `test` | `staging` | `production`),
|
|
35
|
+
orthogonal to `NODE_ENV`.
|
|
36
|
+
|
|
37
|
+
## API
|
|
38
|
+
|
|
39
|
+
### `correctEnv(opts?)`
|
|
40
|
+
|
|
41
|
+
Normalizes `NODE_ENV`, `ENV`, and `BROWSERSLIST_ENV` (reading `BABEL_ENV`),
|
|
42
|
+
keeping them mutually consistent. Existing `process.env` values always win over
|
|
43
|
+
the provided options. Run it **first**, before `envs()`.
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
correctEnv({ nodeEnv: "development", env: "dev" });
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### `envs({ appDirectory, secretsPath?, override?, lint? })`
|
|
50
|
+
|
|
51
|
+
Resolves the file chain above relative to `appDirectory`, parses + expands each
|
|
52
|
+
existing file, merges in precedence order, folds the result into `process.env`,
|
|
53
|
+
and returns it. `.json` files (e.g. the `secretsPath`) are parsed as JSON. `lint`
|
|
54
|
+
(default `true`) emits `.env` hygiene warnings.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
envs({ appDirectory: __dirname });
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Lower-level helpers
|
|
61
|
+
|
|
62
|
+
`parse`, `parseJSON`, `config` (from `dotenv-with-expand`), the `dotenvCheckers`
|
|
63
|
+
/ `dotenvKeyFixers` / `dotenvLineFixers` hygiene rules, `processEnv`, and the
|
|
64
|
+
merge utilities (`mergeDefaults`, `mergeAssign`, `resolveApp`) are all exported
|
|
65
|
+
from the package root.
|
|
66
|
+
|
|
67
|
+
## Usage
|
|
68
|
+
|
|
69
|
+
### Preload for `dev` / `start` scripts
|
|
70
|
+
|
|
71
|
+
Populate `process.env` before any application module is evaluated:
|
|
72
|
+
|
|
73
|
+
```jsonc
|
|
74
|
+
// package.json
|
|
75
|
+
{
|
|
76
|
+
"scripts": {
|
|
77
|
+
"dev": "NODE_OPTIONS=\"--import @cogs/config/preload\" next dev",
|
|
78
|
+
"start": "NODE_OPTIONS=\"--import @cogs/config/preload\" next start"
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The `./preload` entry side-effect-calls `correctEnv()` then
|
|
84
|
+
`envs({ appDirectory: process.cwd(), secretsPath })`, deriving `secretsPath` from
|
|
85
|
+
`SECRETS_PATH` (`${SECRETS_PATH}secrets.json`, or `''` when unset). Requires
|
|
86
|
+
Node ≥ 20.6.
|
|
87
|
+
|
|
88
|
+
### `next.config.ts` (build time)
|
|
89
|
+
|
|
90
|
+
Load env at the top of the config so the build sees resolved values:
|
|
91
|
+
|
|
92
|
+
```ts
|
|
93
|
+
import { correctEnv, envs } from "@cogs/config";
|
|
94
|
+
|
|
95
|
+
correctEnv();
|
|
96
|
+
envs({ appDirectory: __dirname });
|
|
97
|
+
|
|
98
|
+
export default {
|
|
99
|
+
/* next config */
|
|
100
|
+
};
|
|
101
|
+
```
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Corrects and sets various environment variables based on provided values or
|
|
3
|
+
* defaults. Do this as the first thing so that any code reading it knows the
|
|
4
|
+
* right env. Existing `process.env` values always win over the provided options.
|
|
5
|
+
*
|
|
6
|
+
* @param options - An optional object containing environment-related options.
|
|
7
|
+
* @param options.nodeEnv - The desired Node.js environment (NODE_ENV).
|
|
8
|
+
* @param options.babelEnv - The desired Babel environment (BABEL_ENV).
|
|
9
|
+
* @param options.browserslistEnv - The desired Browserslist environment (BROWSERSLIST_ENV).
|
|
10
|
+
* @param options.env - The desired environment (ENV).
|
|
11
|
+
* @param options.browser - The desired browser environment (__BROWSER__).
|
|
12
|
+
*/
|
|
13
|
+
export declare const correctEnv: ({ nodeEnv, babelEnv, browserslistEnv, env, browser, }?: {
|
|
14
|
+
nodeEnv?: "development" | "production" | "test";
|
|
15
|
+
babelEnv?: "esm" | "cjs";
|
|
16
|
+
browserslistEnv?: string;
|
|
17
|
+
env?: string;
|
|
18
|
+
browser?: string;
|
|
19
|
+
}) => void;
|
|
20
|
+
//# sourceMappingURL=correct-env.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"correct-env.d.ts","sourceRoot":"","sources":["../src/correct-env.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,UAAU,GAAI,wDAMxB;IACD,OAAO,CAAC,EAAE,aAAa,GAAG,YAAY,GAAG,MAAM,CAAC;IAChD,QAAQ,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC;IACzB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC;CACb,SA4BL,CAAC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// please note that `process.env` is not exported so only this process has the
|
|
2
|
+
// values for anything set in `process.env`
|
|
3
|
+
function existy(x) {
|
|
4
|
+
return x != null;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Corrects and sets various environment variables based on provided values or
|
|
8
|
+
* defaults. Do this as the first thing so that any code reading it knows the
|
|
9
|
+
* right env. Existing `process.env` values always win over the provided options.
|
|
10
|
+
*
|
|
11
|
+
* @param options - An optional object containing environment-related options.
|
|
12
|
+
* @param options.nodeEnv - The desired Node.js environment (NODE_ENV).
|
|
13
|
+
* @param options.babelEnv - The desired Babel environment (BABEL_ENV).
|
|
14
|
+
* @param options.browserslistEnv - The desired Browserslist environment (BROWSERSLIST_ENV).
|
|
15
|
+
* @param options.env - The desired environment (ENV).
|
|
16
|
+
* @param options.browser - The desired browser environment (__BROWSER__).
|
|
17
|
+
*/
|
|
18
|
+
export const correctEnv = ({ nodeEnv, babelEnv, browserslistEnv, env, browser, } = {}) => {
|
|
19
|
+
const nEnv = process.env.NODE_ENV || nodeEnv;
|
|
20
|
+
const bEnv = process.env.BABEL_ENV || babelEnv;
|
|
21
|
+
const blEnv = process.env.BROWSERSLIST_ENV || browserslistEnv || nEnv;
|
|
22
|
+
const aEnv = process.env.ENV || env;
|
|
23
|
+
let browserEnv = process.env.__BROWSER__;
|
|
24
|
+
if (!existy(browserEnv) && existy(browser)) {
|
|
25
|
+
browserEnv = browser;
|
|
26
|
+
}
|
|
27
|
+
if (existy(bEnv)) {
|
|
28
|
+
process.env.BABEL_ENV = bEnv;
|
|
29
|
+
}
|
|
30
|
+
if (existy(aEnv)) {
|
|
31
|
+
process.env.ENV = aEnv;
|
|
32
|
+
}
|
|
33
|
+
if (existy(blEnv)) {
|
|
34
|
+
process.env.BROWSERSLIST_ENV = blEnv;
|
|
35
|
+
}
|
|
36
|
+
if (existy(browserEnv)) {
|
|
37
|
+
process.env.__BROWSER__ = browserEnv;
|
|
38
|
+
}
|
|
39
|
+
process.env = Object.assign(process.env, { NODE_ENV: nEnv });
|
|
40
|
+
};
|
|
41
|
+
//# sourceMappingURL=correct-env.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"correct-env.js","sourceRoot":"","sources":["../src/correct-env.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,2CAA2C;AAC3C,SAAS,MAAM,CAAC,CAAU;IACxB,OAAO,CAAC,IAAI,IAAI,CAAC;AACnB,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,EACzB,OAAO,EACP,QAAQ,EACR,eAAe,EACf,GAAG,EACH,OAAO,MAOL,EAAE,EAAE,EAAE;IACR,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,IAAI,OAAO,CAAC;IAC7C,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,SAAS,IAAI,QAAQ,CAAC;IAC/C,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,eAAe,IAAI,IAAI,CAAC;IACtE,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC;IAEpC,IAAI,UAAU,GAAG,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;IACzC,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3C,UAAU,GAAG,OAAO,CAAC;IACvB,CAAC;IAED,IAAI,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QACjB,OAAO,CAAC,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC;IAC/B,CAAC;IAED,IAAI,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;QACjB,OAAO,CAAC,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC;IACzB,CAAC;IAED,IAAI,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;QAClB,OAAO,CAAC,GAAG,CAAC,gBAAgB,GAAG,KAAK,CAAC;IACvC,CAAC;IAED,IAAI,MAAM,CAAC,UAAU,CAAC,EAAE,CAAC;QACvB,OAAO,CAAC,GAAG,CAAC,WAAW,GAAG,UAAU,CAAC;IACvC,CAAC;IAED,OAAO,CAAC,GAAG,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC;AAC/D,CAAC,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks if a given key has incorrect delimiters. Delimiters should occur
|
|
3
|
+
* between characters, not as the initial character. This function removes
|
|
4
|
+
* any invalid leading characters before checking for incorrect delimiters.
|
|
5
|
+
*
|
|
6
|
+
* @param key - The key to be checked for incorrect delimiters.
|
|
7
|
+
* @param line - The line of text where the key is found (for logging purposes).
|
|
8
|
+
* @returns True if incorrect delimiters are found, undefined otherwise.
|
|
9
|
+
*/
|
|
10
|
+
export declare const incorrectDelimiterChecker: (key: string, line: string) => true | undefined;
|
|
11
|
+
/**
|
|
12
|
+
* Checks if a key starts with an unallowed character. Only characters from A to
|
|
13
|
+
* Z and _(underscore) are allowed as the initial character.
|
|
14
|
+
*
|
|
15
|
+
* @param key - The key to be checked for an unallowed leading character.
|
|
16
|
+
* @returns True if an unallowed leading character is found, undefined otherwise.
|
|
17
|
+
*/
|
|
18
|
+
export declare const leadingCharacterChecker: (key: string) => true | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Checks if a key contains lowercase characters.
|
|
21
|
+
*
|
|
22
|
+
* @param key - The key to be checked for lowercase characters.
|
|
23
|
+
* @returns True if lowercase characters are found in the key, undefined otherwise.
|
|
24
|
+
*/
|
|
25
|
+
export declare const lowercaseKeyChecker: (key: string) => true | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Checks if a value contains quote characters (`'` or `"`), but skips if the
|
|
28
|
+
* value is multiline, contains whitespaces, newline characters, or hash signs.
|
|
29
|
+
*
|
|
30
|
+
* @param line - The value to be checked for quote characters.
|
|
31
|
+
* @returns True if quote characters are found in the value, undefined otherwise.
|
|
32
|
+
*/
|
|
33
|
+
export declare const quoteCharacterChecker: (line: string) => true | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Checks if a line has trailing whitespace characters (spaces or tabs).
|
|
36
|
+
*
|
|
37
|
+
* @param line - The line to be checked for trailing whitespace.
|
|
38
|
+
* @returns True if trailing whitespace is found in the line, undefined otherwise.
|
|
39
|
+
*/
|
|
40
|
+
export declare const trailingWhitespaceChecker: (line: string) => true | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Checks if a value contains whitespaces and doesn't contain quote characters
|
|
43
|
+
* (`'` / `"`).
|
|
44
|
+
*
|
|
45
|
+
* @param line - The value to be checked for whitespaces and lack of quotes.
|
|
46
|
+
* @returns True if the value has whitespaces without quotes, undefined otherwise.
|
|
47
|
+
*/
|
|
48
|
+
export declare const valueWithoutQuotesChecker: (line: string) => true | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* An array of functions for checking various aspects of .env file entries.
|
|
51
|
+
*/
|
|
52
|
+
export declare const dotenvCheckers: ((key: string, line: string) => true | undefined)[];
|
|
53
|
+
//# sourceMappingURL=dotenv-checkers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dotenv-checkers.d.ts","sourceRoot":"","sources":["../src/dotenv-checkers.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,eAAO,MAAM,yBAAyB,GAAI,KAAK,MAAM,EAAE,MAAM,MAAM,qBAUlE,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,GAAI,KAAK,MAAM,qBASlD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,GAAI,KAAK,MAAM,qBAO9C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,qBAAqB,GAAI,MAAM,MAAM,qBAcjD,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,yBAAyB,GAAI,MAAM,MAAM,qBAKrD,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,GAAI,MAAM,MAAM,qBASrD,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,cAAc,SAtGoB,MAAM,QAAQ,MAAM,wBA6GlE,CAAC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { removeInvalidLeadingChars } from "./utils.js";
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a given key has incorrect delimiters. Delimiters should occur
|
|
4
|
+
* between characters, not as the initial character. This function removes
|
|
5
|
+
* any invalid leading characters before checking for incorrect delimiters.
|
|
6
|
+
*
|
|
7
|
+
* @param key - The key to be checked for incorrect delimiters.
|
|
8
|
+
* @param line - The line of text where the key is found (for logging purposes).
|
|
9
|
+
* @returns True if incorrect delimiters are found, undefined otherwise.
|
|
10
|
+
*/
|
|
11
|
+
export const incorrectDelimiterChecker = (key, line) => {
|
|
12
|
+
// delimiters occur /between/ characters, not as the initial character, so
|
|
13
|
+
// we should remove all invalid leading characters before checking for
|
|
14
|
+
// incorrect delimiters
|
|
15
|
+
const cleanedKey = removeInvalidLeadingChars(key);
|
|
16
|
+
const chars = cleanedKey.trim().split("");
|
|
17
|
+
if (chars.some((char) => !/[a-z0-9_]/i.test(char))) {
|
|
18
|
+
console.log(`warning: '${key}' incorrect delimiter`);
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Checks if a key starts with an unallowed character. Only characters from A to
|
|
24
|
+
* Z and _(underscore) are allowed as the initial character.
|
|
25
|
+
*
|
|
26
|
+
* @param key - The key to be checked for an unallowed leading character.
|
|
27
|
+
* @returns True if an unallowed leading character is found, undefined otherwise.
|
|
28
|
+
*/
|
|
29
|
+
export const leadingCharacterChecker = (key) => {
|
|
30
|
+
if (/^[a-z_]/i.test(key)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
console.log(`warning: '${key}' starts with an unallowed character. (characters from A to Z and _(underscore) are allowed)`);
|
|
34
|
+
return true;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Checks if a key contains lowercase characters.
|
|
38
|
+
*
|
|
39
|
+
* @param key - The key to be checked for lowercase characters.
|
|
40
|
+
* @returns True if lowercase characters are found in the key, undefined otherwise.
|
|
41
|
+
*/
|
|
42
|
+
export const lowercaseKeyChecker = (key) => {
|
|
43
|
+
if (key.toUpperCase() === key) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
console.log(`warning: '${key}' has lowercase characters.`);
|
|
47
|
+
return true;
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Checks if a value contains quote characters (`'` or `"`), but skips if the
|
|
51
|
+
* value is multiline, contains whitespaces, newline characters, or hash signs.
|
|
52
|
+
*
|
|
53
|
+
* @param line - The value to be checked for quote characters.
|
|
54
|
+
* @returns True if quote characters are found in the value, undefined otherwise.
|
|
55
|
+
*/
|
|
56
|
+
export const quoteCharacterChecker = (line) => {
|
|
57
|
+
if (line.includes("\n") ||
|
|
58
|
+
/[ \t\r\n\f]/.test(line) ||
|
|
59
|
+
line.includes("$") ||
|
|
60
|
+
line.includes("#")) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
if (line.includes('"') || line.includes("'")) {
|
|
64
|
+
console.log(`warning: '${line}' contains quote characters.`);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Checks if a line has trailing whitespace characters (spaces or tabs).
|
|
70
|
+
*
|
|
71
|
+
* @param line - The line to be checked for trailing whitespace.
|
|
72
|
+
* @returns True if trailing whitespace is found in the line, undefined otherwise.
|
|
73
|
+
*/
|
|
74
|
+
export const trailingWhitespaceChecker = (line) => {
|
|
75
|
+
if (line.endsWith(" ")) {
|
|
76
|
+
console.log(`warning: '${line}' has a trailing whitespace.`);
|
|
77
|
+
return true;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Checks if a value contains whitespaces and doesn't contain quote characters
|
|
82
|
+
* (`'` / `"`).
|
|
83
|
+
*
|
|
84
|
+
* @param line - The value to be checked for whitespaces and lack of quotes.
|
|
85
|
+
* @returns True if the value has whitespaces without quotes, undefined otherwise.
|
|
86
|
+
*/
|
|
87
|
+
export const valueWithoutQuotesChecker = (line) => {
|
|
88
|
+
line = line.trim();
|
|
89
|
+
if (/[ \t\r\n\f]/.test(line) &&
|
|
90
|
+
!(line.startsWith("'") && line.endsWith("'")) &&
|
|
91
|
+
!(line.startsWith('"') && line.endsWith('"'))) {
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* An array of functions for checking various aspects of .env file entries.
|
|
97
|
+
*/
|
|
98
|
+
export const dotenvCheckers = [
|
|
99
|
+
incorrectDelimiterChecker,
|
|
100
|
+
leadingCharacterChecker,
|
|
101
|
+
lowercaseKeyChecker,
|
|
102
|
+
quoteCharacterChecker,
|
|
103
|
+
trailingWhitespaceChecker,
|
|
104
|
+
valueWithoutQuotesChecker,
|
|
105
|
+
];
|
|
106
|
+
//# sourceMappingURL=dotenv-checkers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dotenv-checkers.js","sourceRoot":"","sources":["../src/dotenv-checkers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAC;AAEvD;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,GAAW,EAAE,IA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B,CAAC;QACD,OAAO;IACT,CAAC;IAED,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QAC7C,OAAO,CAAC,GAAG,CAAC,aAAa,IAAI,8BAA8B,CAAC,CAAC;QAC7D,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,IAAY,EAAE,EAAE;IACxD,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QACvB,OAAO,CAAC,GAAG,CAAC,aAAa,IAAI,8BAA8B,CAAC,CAAC;QAC7D,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,IAAY,EAAE,EAAE;IACxD,IAAI,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC;IACnB,IACE,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC;QACxB,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;QAC7C,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,EAC7C,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG;IAC5B,yBAAyB;IACzB,uBAAuB;IACvB,mBAAmB;IACnB,qBAAqB;IACrB,yBAAyB;IACzB,yBAAyB;CAC1B,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fixes incorrect delimiters by replacing non-alphanumeric characters with
|
|
3
|
+
* underscores ('_'). Also ensures that an underscore separates words in the key.
|
|
4
|
+
*
|
|
5
|
+
* @param key - The key to fix.
|
|
6
|
+
* @param line - The corresponding line from the .env file.
|
|
7
|
+
* @returns An array containing the fixed key and the original line.
|
|
8
|
+
*/
|
|
9
|
+
export declare const incorrectDelimiterFixer: (key: string, line: string) => string[];
|
|
10
|
+
export declare const leadingCharacterFixer: (key: string, line: string) => string[];
|
|
11
|
+
export declare const lowercaseKeyFixer: (key: string, line: string) => string[];
|
|
12
|
+
export declare const quoteCharacterFixer: (key: string, line: string) => string[];
|
|
13
|
+
export declare const trailingWhitespaceFixer: (key: string, line: string) => string[];
|
|
14
|
+
export declare const valueWithoutQuotesFixer: (key: string, line: string) => string[];
|
|
15
|
+
export declare const dotenvKeyFixers: ((key: string, line: string) => string[])[];
|
|
16
|
+
export declare const dotenvLineFixers: ((key: string, line: string) => string[])[];
|
|
17
|
+
//# sourceMappingURL=dotenv-fixers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dotenv-fixers.d.ts","sourceRoot":"","sources":["../src/dotenv-fixers.ts"],"names":[],"mappings":"AAUA;;;;;;;GAOG;AACH,eAAO,MAAM,uBAAuB,GAAI,KAAK,MAAM,EAAE,MAAM,MAAM,aAUhE,CAAC;AAGF,eAAO,MAAM,qBAAqB,GAAI,KAAK,MAAM,EAAE,MAAM,MAAM,aAO9D,CAAC;AAGF,eAAO,MAAM,iBAAiB,GAAI,KAAK,MAAM,EAAE,MAAM,MAAM,aAM1D,CAAC;AAKF,eAAO,MAAM,mBAAmB,GAAI,KAAK,MAAM,EAAE,MAAM,MAAM,aAO5D,CAAC;AAGF,eAAO,MAAM,uBAAuB,GAAI,KAAK,MAAM,EAAE,MAAM,MAAM,aAMhE,CAAC;AAIF,eAAO,MAAM,uBAAuB,GAAI,KAAK,MAAM,EAAE,MAAM,MAAM,aAehE,CAAC;AAEF,eAAO,MAAM,eAAe,SAvEiB,MAAM,QAAQ,MAAM,gBA2EhE,CAAC;AAEF,eAAO,MAAM,gBAAgB,SA3CY,MAAM,QAAQ,MAAM,gBA+C5D,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { incorrectDelimiterChecker, leadingCharacterChecker, lowercaseKeyChecker, quoteCharacterChecker, trailingWhitespaceChecker, valueWithoutQuotesChecker, } from "./dotenv-checkers.js";
|
|
2
|
+
import { removeInvalidLeadingChars } from "./utils.js";
|
|
3
|
+
/**
|
|
4
|
+
* Fixes incorrect delimiters by replacing non-alphanumeric characters with
|
|
5
|
+
* underscores ('_'). Also ensures that an underscore separates words in the key.
|
|
6
|
+
*
|
|
7
|
+
* @param key - The key to fix.
|
|
8
|
+
* @param line - The corresponding line from the .env file.
|
|
9
|
+
* @returns An array containing the fixed key and the original line.
|
|
10
|
+
*/
|
|
11
|
+
export const incorrectDelimiterFixer = (key, line) => {
|
|
12
|
+
if (!incorrectDelimiterChecker(key, line)) {
|
|
13
|
+
return [key, line];
|
|
14
|
+
}
|
|
15
|
+
let cleanedKey = removeInvalidLeadingChars(key);
|
|
16
|
+
const startIdx = key.length - cleanedKey.length;
|
|
17
|
+
cleanedKey = key.slice(startIdx).replace(/[^a-z0-9]/gi, "_");
|
|
18
|
+
return [`${key.substring(0, startIdx)}${cleanedKey}`, line];
|
|
19
|
+
};
|
|
20
|
+
// Fixes key that starts with an unallowed character.
|
|
21
|
+
export const leadingCharacterFixer = (key, line) => {
|
|
22
|
+
if (!leadingCharacterChecker(key)) {
|
|
23
|
+
return [key, line];
|
|
24
|
+
}
|
|
25
|
+
const cleanedKey = removeInvalidLeadingChars(key);
|
|
26
|
+
return [cleanedKey, line];
|
|
27
|
+
};
|
|
28
|
+
// Fixes lowercase key by upper casing it.
|
|
29
|
+
export const lowercaseKeyFixer = (key, line) => {
|
|
30
|
+
if (!lowercaseKeyChecker(key)) {
|
|
31
|
+
return [key, line];
|
|
32
|
+
}
|
|
33
|
+
return [key.toUpperCase(), line];
|
|
34
|
+
};
|
|
35
|
+
// Quote character: Fixes when a value contains quote characters(`'` / `"`) but
|
|
36
|
+
// skips if the value is multiline, contains whitespaces, newline characters or
|
|
37
|
+
// hash signs:
|
|
38
|
+
export const quoteCharacterFixer = (key, line) => {
|
|
39
|
+
if (!quoteCharacterChecker(line)) {
|
|
40
|
+
return [key, line];
|
|
41
|
+
}
|
|
42
|
+
const pureVal = line.replace(/"|'/g, "");
|
|
43
|
+
return [key, pureVal];
|
|
44
|
+
};
|
|
45
|
+
// Fixes trailing whitespace.
|
|
46
|
+
export const trailingWhitespaceFixer = (key, line) => {
|
|
47
|
+
if (!trailingWhitespaceChecker(line)) {
|
|
48
|
+
return [key, line];
|
|
49
|
+
}
|
|
50
|
+
return [key, line.trimEnd()];
|
|
51
|
+
};
|
|
52
|
+
// Value without quotes: Detects if a value contains whitespaces and at the
|
|
53
|
+
// same time doesn't contain quote characters (`'` / `"`):
|
|
54
|
+
export const valueWithoutQuotesFixer = (key, line) => {
|
|
55
|
+
if (!valueWithoutQuotesChecker(line)) {
|
|
56
|
+
return [key, line];
|
|
57
|
+
}
|
|
58
|
+
let pureVal = "";
|
|
59
|
+
if (line.includes("'")) {
|
|
60
|
+
pureVal = `"${line}"`;
|
|
61
|
+
}
|
|
62
|
+
else if (line.includes('"')) {
|
|
63
|
+
pureVal = `'${line}'`;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
pureVal = `'${line}'`;
|
|
67
|
+
}
|
|
68
|
+
return [key, pureVal];
|
|
69
|
+
};
|
|
70
|
+
export const dotenvKeyFixers = [
|
|
71
|
+
incorrectDelimiterFixer,
|
|
72
|
+
leadingCharacterFixer,
|
|
73
|
+
lowercaseKeyFixer,
|
|
74
|
+
];
|
|
75
|
+
export const dotenvLineFixers = [
|
|
76
|
+
quoteCharacterFixer,
|
|
77
|
+
trailingWhitespaceFixer,
|
|
78
|
+
valueWithoutQuotesFixer,
|
|
79
|
+
];
|
|
80
|
+
//# sourceMappingURL=dotenv-fixers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
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AAO,GAAG,IAAI,IAAI,GAAG,CAAC;IACxB,CAAC;SAAM,IAAI,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;QAC9B,OAAO,GAAG,IAAI,IAAI,GAAG,CAAC;IACxB,CAAC;SAAM,CAAC;QACN,OAAO,GAAG,IAAI,IAAI,GAAG,CAAC;IACxB,CAAC;IAED,OAAO,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;AACxB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,uBAAuB;IACvB,qBAAqB;IACrB,iBAAiB;CAClB,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,mBAAmB;IACnB,uBAAuB;IACvB,uBAAuB;CACxB,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { DotenvParseOutput, DotenvPopulateInput } from "dotenv";
|
|
2
|
+
export interface ConfigOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Default: `path.resolve(process.cwd(), '.env')`
|
|
5
|
+
*
|
|
6
|
+
* Specify a custom path if your file containing environment variables is
|
|
7
|
+
* located elsewhere. Can also be an array of strings, specifying multiple
|
|
8
|
+
* paths.
|
|
9
|
+
*/
|
|
10
|
+
path?: string | string[];
|
|
11
|
+
/**
|
|
12
|
+
* Default: `false`
|
|
13
|
+
*
|
|
14
|
+
* Override any environment variables that have already been set on your
|
|
15
|
+
* machine with values from your .env file.
|
|
16
|
+
*/
|
|
17
|
+
override?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Default: `process.env`
|
|
20
|
+
*
|
|
21
|
+
* Specify an object to write your secrets to. Defaults to process.env
|
|
22
|
+
* environment variables.
|
|
23
|
+
*/
|
|
24
|
+
processEnv?: DotenvPopulateInput;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Parse and expand environment variables from a .env file.
|
|
28
|
+
*
|
|
29
|
+
* @param src - The raw .env contents.
|
|
30
|
+
* @param override - Whether file values override existing process.env values.
|
|
31
|
+
* @param processEnv - The object expansion resolves references against.
|
|
32
|
+
* @returns An object containing the parsed and expanded environment variables.
|
|
33
|
+
*/
|
|
34
|
+
export declare const parse: (src: string | Buffer, override?: boolean, processEnv?: DotenvPopulateInput) => DotenvParseOutput;
|
|
35
|
+
/**
|
|
36
|
+
* Parse and expand environment variables from a JSON secrets file.
|
|
37
|
+
*
|
|
38
|
+
* @param src - The raw JSON contents.
|
|
39
|
+
* @param override - Whether file values override existing process.env values.
|
|
40
|
+
* @param processEnv - The object expansion resolves references against.
|
|
41
|
+
* @returns An object containing the parsed and expanded environment variables.
|
|
42
|
+
*/
|
|
43
|
+
export declare const parseJSON: (src: string | Buffer, override?: boolean, processEnv?: DotenvPopulateInput) => DotenvParseOutput;
|
|
44
|
+
/**
|
|
45
|
+
* Configure and expand environment variables from a .env file.
|
|
46
|
+
*
|
|
47
|
+
* @param args - Configuration options passed to the loader.
|
|
48
|
+
* @returns An object containing the parsed and expanded environment variables.
|
|
49
|
+
*/
|
|
50
|
+
export declare const config: (args?: ConfigOptions) => {
|
|
51
|
+
parsed: DotenvParseOutput | undefined;
|
|
52
|
+
};
|
|
53
|
+
//# sourceMappingURL=dotenv-with-expand.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dotenv-with-expand.d.ts","sourceRoot":"","sources":["../src/dotenv-with-expand.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AAIrE,MAAM,WAAW,aAAa;IAC5B;;;;;;OAMG;IACH,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;IAEzB;;;;;OAKG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;;;;OAKG;IACH,UAAU,CAAC,EAAE,mBAAmB,CAAC;CAClC;AA0BD;;;;;;;GAOG;AACH,eAAO,MAAM,KAAK,GAChB,KAAK,MAAM,GAAG,MAAM,EACpB,WAAW,OAAO,EAClB,aAAa,mBAAmB,KAC/B,iBAUF,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,SAAS,GACpB,KAAK,MAAM,GAAG,MAAM,EACpB,WAAW,OAAO,EAClB,aAAa,mBAAmB,KAC/B,iBAWF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,MAAM,GACjB,OAAO,aAAa,KACnB;IACD,MAAM,EAAE,iBAAiB,GAAG,SAAS,CAAC;CAIvC,CAAC"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import dotenv from "dotenv";
|
|
3
|
+
import dotenvExpand from "dotenv-expand";
|
|
4
|
+
const dotenvExpandOverrideFix = (parsed) => {
|
|
5
|
+
const obj = {};
|
|
6
|
+
dotenvExpand.expand({ parsed, processEnv: obj });
|
|
7
|
+
const source = Object.keys(obj).length > 0 ? obj : parsed;
|
|
8
|
+
// Set process.env
|
|
9
|
+
for (const key of Object.keys(source)) {
|
|
10
|
+
if (Object.hasOwn(process.env, key)) {
|
|
11
|
+
const upcoming = source[key];
|
|
12
|
+
const prior = process.env[key];
|
|
13
|
+
if (prior !== upcoming) {
|
|
14
|
+
console.log(`"${key}" is already defined as ${prior}, and WAS overwritten with ${upcoming}`);
|
|
15
|
+
}
|
|
16
|
+
process.env[key] = source[key];
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
process.env[key] = source[key];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Parse and expand environment variables from a .env file.
|
|
25
|
+
*
|
|
26
|
+
* @param src - The raw .env contents.
|
|
27
|
+
* @param override - Whether file values override existing process.env values.
|
|
28
|
+
* @param processEnv - The object expansion resolves references against.
|
|
29
|
+
* @returns An object containing the parsed and expanded environment variables.
|
|
30
|
+
*/
|
|
31
|
+
export const parse = (src, override, processEnv) => {
|
|
32
|
+
const parsed = dotenv.parse(src.toString());
|
|
33
|
+
if (override) {
|
|
34
|
+
dotenvExpandOverrideFix(parsed);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
dotenvExpand.expand({ parsed, processEnv });
|
|
38
|
+
}
|
|
39
|
+
return parsed;
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Parse and expand environment variables from a JSON secrets file.
|
|
43
|
+
*
|
|
44
|
+
* @param src - The raw JSON contents.
|
|
45
|
+
* @param override - Whether file values override existing process.env values.
|
|
46
|
+
* @param processEnv - The object expansion resolves references against.
|
|
47
|
+
* @returns An object containing the parsed and expanded environment variables.
|
|
48
|
+
*/
|
|
49
|
+
export const parseJSON = (src, override, processEnv) => {
|
|
50
|
+
// Convert buffer to string
|
|
51
|
+
const parsed = JSON.parse(src.toString());
|
|
52
|
+
if (override) {
|
|
53
|
+
dotenvExpandOverrideFix(parsed);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
dotenvExpand.expand({ parsed, processEnv });
|
|
57
|
+
}
|
|
58
|
+
return parsed;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Configure and expand environment variables from a .env file.
|
|
62
|
+
*
|
|
63
|
+
* @param args - Configuration options passed to the loader.
|
|
64
|
+
* @returns An object containing the parsed and expanded environment variables.
|
|
65
|
+
*/
|
|
66
|
+
export const config = (args) => {
|
|
67
|
+
const { parsed: envs } = configHelper(args);
|
|
68
|
+
return { parsed: envs };
|
|
69
|
+
};
|
|
70
|
+
const configHelper = (options) => {
|
|
71
|
+
let optionPaths = [];
|
|
72
|
+
if (options?.path) {
|
|
73
|
+
if (!Array.isArray(options.path)) {
|
|
74
|
+
optionPaths = [options.path];
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
optionPaths = []; // reset default
|
|
78
|
+
for (const filepath of options.path) {
|
|
79
|
+
optionPaths.push(filepath);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Build the parsed data in a temporary object (because we need to return it).
|
|
84
|
+
// Once we have the final parsed data, we will combine it with process.env (or
|
|
85
|
+
// options.processEnv if provided).
|
|
86
|
+
let lastError;
|
|
87
|
+
const parsedAll = {};
|
|
88
|
+
for (const optPath of optionPaths) {
|
|
89
|
+
try {
|
|
90
|
+
let parsed;
|
|
91
|
+
const contents = readFileSync(optPath, { encoding: "utf8" });
|
|
92
|
+
if (optPath?.endsWith(".json")) {
|
|
93
|
+
parsed = parseJSON(contents, options?.override);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
// Specifying an encoding returns a string instead of a buffer
|
|
97
|
+
parsed = parse(contents, options?.override);
|
|
98
|
+
}
|
|
99
|
+
dotenv.populate(parsedAll, parsed, options);
|
|
100
|
+
}
|
|
101
|
+
catch (e) {
|
|
102
|
+
lastError = e;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
let processEnv = process.env;
|
|
106
|
+
if (options?.processEnv != null) {
|
|
107
|
+
processEnv = options.processEnv;
|
|
108
|
+
}
|
|
109
|
+
dotenv.populate(processEnv, parsedAll, options);
|
|
110
|
+
if (lastError) {
|
|
111
|
+
return { parsed: parsedAll, error: lastError };
|
|
112
|
+
}
|
|
113
|
+
return { parsed: parsedAll };
|
|
114
|
+
};
|
|
115
|
+
//# sourceMappingURL=dotenv-with-expand.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"dotenv-with-expand.js","sourceRoot":"","sources":["../src/dotenv-with-expand.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEvC,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,YAAY,MAAM,eAAe,CAAC;AA6BzC,MAAM,uBAAuB,GAAG,CAAC,MAAkC,EAAQ,EAAE;IAC3E,MAAM,GAAG,GAAG,EAAgC,CAAC;IAC7C,YAAY,CAAC,MAAM,CAAC,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC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,MAAM,OAAO,IAAI,WAAW,EAAE,CAAC;QAClC,IAAI,CAAC;YACH,IAAI,MAAyB,CAAC;YAC9B,MAAM,QAAQ,GAAG,YAAY,CAAC,OAAO,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC;YAC7D,IAAI,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC/B,MAAM,GAAG,SAAS,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;YAClD,CAAC;iBAAM,CAAC;gBACN,8DAA8D;gBAC9D,MAAM,GAAG,KAAK,CAAC,QAAQ,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;YAC9C,CAAC;YAED,MAAM,CAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,CAAC;QAC9C,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,SAAS,GAAG,CAAC,CAAC;QAChB,CAAC;IACH,CAAC;IAED,IAAI,UAAU,GAAG,OAAO,CAAC,GAA0B,CAAC;IACpD,IAAI,OAAO,EAAE,UAAU,IAAI,IAAI,EAAE,CAAC;QAChC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;IAClC,CAAC;IAED,MAAM,CAAC,QAAQ,CAAC,UAAU,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC;IAEhD,IAAI,SAAS,EAAE,CAAC;QACd,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;IACjD,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC;AAC/B,CAAC,CAAC"}
|
package/dist/envs.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Load environment variables from .env* files and optionally perform linting.
|
|
3
|
+
* Suppress warnings using silent if this file is missing. Dotenv files are
|
|
4
|
+
* merged in precedence order before filling process.env, so `.env.<env>.local`
|
|
5
|
+
* wins over lower-priority dotenv files while inherited runtime variables remain
|
|
6
|
+
* authoritative unless `override` is enabled. Variable expansion is supported in
|
|
7
|
+
* .env files.
|
|
8
|
+
*
|
|
9
|
+
* @param options - Configuration options.
|
|
10
|
+
* @param options.appDirectory - The directory of the app.
|
|
11
|
+
* @param options.secretsPath - The path to an injected secrets file.
|
|
12
|
+
* @param options.lint - Whether to perform linting on the loaded variables.
|
|
13
|
+
* @param options.override - Whether file values override existing process.env values.
|
|
14
|
+
* @returns An object containing the loaded environment variables.
|
|
15
|
+
*/
|
|
16
|
+
export declare const envs: ({ appDirectory, lint, secretsPath, override, }?: {
|
|
17
|
+
appDirectory?: string;
|
|
18
|
+
secretsPath?: string;
|
|
19
|
+
lint?: boolean;
|
|
20
|
+
override?: boolean;
|
|
21
|
+
}) => {
|
|
22
|
+
[name: string]: string | undefined;
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=envs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envs.d.ts","sourceRoot":"","sources":["../src/envs.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,IAAI,GAAI,iDAKlB;IACD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,QAAQ,CAAC,EAAE,OAAO,CAAC;CACf,KAAG;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;CAmD5C,CAAC"}
|
package/dist/envs.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { isAbsolute } from "node:path";
|
|
3
|
+
import { dotenvCheckers } from "./dotenv-checkers.js";
|
|
4
|
+
import { parse, parseJSON } from "./dotenv-with-expand.js";
|
|
5
|
+
import { mergeAssign, mergeDefaults, resolveApp } from "./utils.js";
|
|
6
|
+
/**
|
|
7
|
+
* Load environment variables from .env* files and optionally perform linting.
|
|
8
|
+
* Suppress warnings using silent if this file is missing. Dotenv files are
|
|
9
|
+
* merged in precedence order before filling process.env, so `.env.<env>.local`
|
|
10
|
+
* wins over lower-priority dotenv files while inherited runtime variables remain
|
|
11
|
+
* authoritative unless `override` is enabled. Variable expansion is supported in
|
|
12
|
+
* .env files.
|
|
13
|
+
*
|
|
14
|
+
* @param options - Configuration options.
|
|
15
|
+
* @param options.appDirectory - The directory of the app.
|
|
16
|
+
* @param options.secretsPath - The path to an injected secrets file.
|
|
17
|
+
* @param options.lint - Whether to perform linting on the loaded variables.
|
|
18
|
+
* @param options.override - Whether file values override existing process.env values.
|
|
19
|
+
* @returns An object containing the loaded environment variables.
|
|
20
|
+
*/
|
|
21
|
+
export const envs = ({ appDirectory = "", lint = true, secretsPath = "", override = false, } = {}) => {
|
|
22
|
+
let absDotenvFiles = [];
|
|
23
|
+
const NODE_ENV = process.env.NODE_ENV;
|
|
24
|
+
const ENV = process.env.ENV;
|
|
25
|
+
// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
|
|
26
|
+
const dotenvFiles = [
|
|
27
|
+
".env",
|
|
28
|
+
// Don't include `.env.local` for `test` environment since normally you
|
|
29
|
+
// expect tests to produce the same results for everyone
|
|
30
|
+
NODE_ENV !== "test" && ".env.local",
|
|
31
|
+
ENV && `.env.${ENV}`,
|
|
32
|
+
secretsPath,
|
|
33
|
+
ENV && `.env.${ENV}.local`,
|
|
34
|
+
].filter(Boolean);
|
|
35
|
+
if (appDirectory) {
|
|
36
|
+
absDotenvFiles = dotenvFiles.reduce((acc, dotenvFile) => {
|
|
37
|
+
return isAbsolute(dotenvFile)
|
|
38
|
+
? acc.concat(dotenvFile)
|
|
39
|
+
: acc.concat(resolveApp(dotenvFile, appDirectory));
|
|
40
|
+
}, []);
|
|
41
|
+
}
|
|
42
|
+
const resolvedEnv = absDotenvFiles.reduce((acc, dotenvFile) => {
|
|
43
|
+
if (!existsSync(dotenvFile)) {
|
|
44
|
+
return acc;
|
|
45
|
+
}
|
|
46
|
+
const contents = readFileSync(dotenvFile, { encoding: "utf8" });
|
|
47
|
+
const envs = dotenvFile.endsWith(".json")
|
|
48
|
+
? parseJSON(contents, false, {})
|
|
49
|
+
: parse(contents, false, {});
|
|
50
|
+
if (lint && envs) {
|
|
51
|
+
Object.entries(envs).forEach(([key, value]) => {
|
|
52
|
+
dotenvCheckers.forEach((checker) => {
|
|
53
|
+
checker(key, value);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return mergeAssign(acc, envs);
|
|
58
|
+
}, {});
|
|
59
|
+
return override
|
|
60
|
+
? mergeAssign(process.env, resolvedEnv)
|
|
61
|
+
: mergeDefaults(process.env, resolvedEnv);
|
|
62
|
+
};
|
|
63
|
+
//# sourceMappingURL=envs.js.map
|
package/dist/envs.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envs.js","sourceRoot":"","sources":["../src/envs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEpE;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAC,EACnB,YAAY,GAAG,EAAE,EACjB,IAAI,GAAG,IAAI,EACX,WAAW,GAAG,EAAE,EAChB,QAAQ,GAAG,KAAK,MAMd,EAAE,EAA0C,EAAE;IAChD,IAAI,cAAc,GAAa,EAAE,CAAC;IAClC,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAA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GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;gBAC5C,cAAc,CAAC,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;oBACjC,OAAO,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;gBACtB,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;QAED,OAAO,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;IAChC,CAAC,EACD,EAA4C,CAC7C,CAAC;IAEF,OAAO,QAAQ;QACb,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,GAAG,EAAE,WAAW,CAAC;QACvC,CAAC,CAAC,aAAa,CAAC,OAAO,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;AAC9C,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./correct-env.js";
|
|
2
|
+
export * from "./dotenv-checkers.js";
|
|
3
|
+
export * from "./dotenv-fixers.js";
|
|
4
|
+
export * from "./dotenv-with-expand.js";
|
|
5
|
+
export * from "./envs.js";
|
|
6
|
+
export * from "./process-env.js";
|
|
7
|
+
export * from "./utils.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from "./correct-env.js";
|
|
2
|
+
export * from "./dotenv-checkers.js";
|
|
3
|
+
export * from "./dotenv-fixers.js";
|
|
4
|
+
export * from "./dotenv-with-expand.js";
|
|
5
|
+
export * from "./envs.js";
|
|
6
|
+
export * from "./process-env.js";
|
|
7
|
+
export * from "./utils.js";
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC;AACjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,oBAAoB,CAAC;AACnC,cAAc,yBAAyB,CAAC;AACxC,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preload.d.ts","sourceRoot":"","sources":["../src/preload.ts"],"names":[],"mappings":""}
|
package/dist/preload.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Preload entry for `dev` / `start` scripts and any process that must have the
|
|
2
|
+
// dotenv chain folded into `process.env` before a single application module is
|
|
3
|
+
// evaluated. Wire it via `NODE_OPTIONS="--import @cogs/config/preload"` (Node
|
|
4
|
+
// >= 20.6, ESM). Importing this module runs `correctEnv()` and then `envs()` as
|
|
5
|
+
// a side effect.
|
|
6
|
+
import { correctEnv } from "./correct-env.js";
|
|
7
|
+
import { envs } from "./envs.js";
|
|
8
|
+
correctEnv();
|
|
9
|
+
envs({
|
|
10
|
+
appDirectory: process.cwd(),
|
|
11
|
+
// Guard the concat: an unset SECRETS_PATH must yield '' rather than the
|
|
12
|
+
// literal string 'undefinedsecrets.json'.
|
|
13
|
+
secretsPath: process.env.SECRETS_PATH
|
|
14
|
+
? `${process.env.SECRETS_PATH}secrets.json`
|
|
15
|
+
: "",
|
|
16
|
+
});
|
|
17
|
+
//# sourceMappingURL=preload.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"preload.js","sourceRoot":"","sources":["../src/preload.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,+EAA+E;AAC/E,8EAA8E;AAC9E,gFAAgF;AAChF,iBAAiB;AACjB,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAEjC,UAAU,EAAE,CAAC;AAEb,IAAI,CAAC;IACH,YAAY,EAAE,OAAO,CAAC,GAAG,EAAE;IAC3B,wEAAwE;IACxE,0CAA0C;IAC1C,WAAW,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY;QACnC,CAAC,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,cAAc;QAC3C,CAAC,CAAC,EAAE;CACP,CAAC,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process environment variables based on specific rules.
|
|
3
|
+
*
|
|
4
|
+
* @param config - The configuration object.
|
|
5
|
+
* @returns An object containing processed environment variables.
|
|
6
|
+
*/
|
|
7
|
+
export declare const processEnv: (config: {
|
|
8
|
+
[name: string]: string | undefined;
|
|
9
|
+
}) => {
|
|
10
|
+
[name: string]: string | undefined;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=process-env.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-env.d.ts","sourceRoot":"","sources":["../src/process-env.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,eAAO,MAAM,UAAU,GAAI,QAAQ;IACjC,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CACpC,KAAG;IAAE,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;CA6CvC,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process environment variables based on specific rules.
|
|
3
|
+
*
|
|
4
|
+
* @param config - The configuration object.
|
|
5
|
+
* @returns An object containing processed environment variables.
|
|
6
|
+
*/
|
|
7
|
+
export const processEnv = (config) => {
|
|
8
|
+
// node env should be DEVELOPMENT | PRODUCTION as these are commonly used in
|
|
9
|
+
// node land this variable is used inside webpack
|
|
10
|
+
const nodeEnv = process.env.NODE_ENV;
|
|
11
|
+
// config env is dependent on the env that it's run, this can be anything from
|
|
12
|
+
// stage-prod this allows you to set use different .env environments
|
|
13
|
+
const configEnv = process.env.ENV;
|
|
14
|
+
const envObject = Object.keys(config).reduce((acc, key) => {
|
|
15
|
+
acc[key] = config[key];
|
|
16
|
+
return acc;
|
|
17
|
+
}, {});
|
|
18
|
+
const processEnvObj = Object.keys(process.env).reduce((acc, key) => {
|
|
19
|
+
acc[key] = process.env[key];
|
|
20
|
+
return acc;
|
|
21
|
+
}, {});
|
|
22
|
+
// Don't allow overwriting of NODE_ENV
|
|
23
|
+
envObject.NODE_ENV = nodeEnv;
|
|
24
|
+
// anything from dev, qa, prive and prod
|
|
25
|
+
envObject.ENV = configEnv;
|
|
26
|
+
let mergedEnvVars = Object.assign(envObject, processEnvObj);
|
|
27
|
+
mergedEnvVars = Object.keys(mergedEnvVars).reduce((acc, key) => {
|
|
28
|
+
acc[key] = mergedEnvVars[key];
|
|
29
|
+
return acc;
|
|
30
|
+
}, {});
|
|
31
|
+
return Object.keys(mergedEnvVars).reduce((acc, key) => {
|
|
32
|
+
acc[key] = mergedEnvVars[key];
|
|
33
|
+
return acc;
|
|
34
|
+
}, {});
|
|
35
|
+
};
|
|
36
|
+
//# sourceMappingURL=process-env.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"process-env.js","sourceRoot":"","sources":["../src/process-env.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,MAE1B,EAA0C,EAAE;IAC3C,4EAA4E;IAC5E,iDAAiD;IACjD,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC;IACrC,8EAA8E;IAC9E,oEAAoE;IACpE,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;IAElC,MAAM,S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
+
export declare const mergeDefaults: <T extends Record<string, unknown>>(target: T, source?: Record<string, unknown>) => T;
|
|
2
|
+
export declare const mergeAssign: <T extends Record<string, unknown>>(target: T, source?: Record<string, unknown>) => T;
|
|
3
|
+
/**
|
|
4
|
+
* Resolve an absolute path by combining the `appDirectory` and `relativePath`.
|
|
5
|
+
*
|
|
6
|
+
* @param relativePath - The relative path to resolve.
|
|
7
|
+
* @param appDirectory - The base directory for the resolution.
|
|
8
|
+
* @returns The resolved absolute path.
|
|
9
|
+
*/
|
|
10
|
+
export declare const resolveApp: (relativePath: string, appDirectory: string) => string;
|
|
11
|
+
/**
|
|
12
|
+
* Removes invalid leading characters (characters from A to Z and _(underscore)
|
|
13
|
+
* are allowed) from the given string.
|
|
14
|
+
*
|
|
15
|
+
* @param str - The input string.
|
|
16
|
+
* @returns The string with invalid leading characters removed.
|
|
17
|
+
*/
|
|
18
|
+
export declare const removeInvalidLeadingChars: (str: string) => string;
|
|
19
|
+
export declare const isEmpty: (str: string | null | undefined) => boolean;
|
|
20
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7D,QAAQ,CAAC,EACT,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC/B,CAcF,CAAC;AAEF,eAAO,MAAM,WAAW,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC3D,QAAQ,CAAC,EACT,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAC/B,CAYF,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,GACrB,cAAc,MAAM,EACpB,cAAc,MAAM,KACnB,MAAkD,CAAC;AAEtD;;;;;;GAMG;AACH,eAAO,MAAM,yBAAyB,GAAI,KAAK,MAAM,KAAG,MAavD,CAAC;AAEF,eAAO,MAAM,OAAO,GAAI,KAAK,MAAM,GAAG,IAAI,GAAG,SAAS,KAAG,OACxB,CAAC"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
export const mergeDefaults = (target, source) => {
|
|
3
|
+
const targetRecord = target;
|
|
4
|
+
if (!source) {
|
|
5
|
+
return target;
|
|
6
|
+
}
|
|
7
|
+
Object.entries(source).forEach(([key, value]) => {
|
|
8
|
+
if (targetRecord[key] === undefined) {
|
|
9
|
+
targetRecord[key] = value;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
return target;
|
|
13
|
+
};
|
|
14
|
+
export const mergeAssign = (target, source) => {
|
|
15
|
+
const targetRecord = target;
|
|
16
|
+
if (!source) {
|
|
17
|
+
return target;
|
|
18
|
+
}
|
|
19
|
+
Object.entries(source).forEach(([key, value]) => {
|
|
20
|
+
targetRecord[key] = value;
|
|
21
|
+
});
|
|
22
|
+
return target;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Resolve an absolute path by combining the `appDirectory` and `relativePath`.
|
|
26
|
+
*
|
|
27
|
+
* @param relativePath - The relative path to resolve.
|
|
28
|
+
* @param appDirectory - The base directory for the resolution.
|
|
29
|
+
* @returns The resolved absolute path.
|
|
30
|
+
*/
|
|
31
|
+
export const resolveApp = (relativePath, appDirectory) => path.resolve(appDirectory, relativePath);
|
|
32
|
+
/**
|
|
33
|
+
* Removes invalid leading characters (characters from A to Z and _(underscore)
|
|
34
|
+
* are allowed) from the given string.
|
|
35
|
+
*
|
|
36
|
+
* @param str - The input string.
|
|
37
|
+
* @returns The string with invalid leading characters removed.
|
|
38
|
+
*/
|
|
39
|
+
export const removeInvalidLeadingChars = (str) => {
|
|
40
|
+
const chars = str.split("");
|
|
41
|
+
let notFound = true;
|
|
42
|
+
let i = 0;
|
|
43
|
+
while (notFound && i < chars.length) {
|
|
44
|
+
const currentChar = chars[i];
|
|
45
|
+
if (currentChar !== undefined && /[a-z_]/i.test(currentChar)) {
|
|
46
|
+
notFound = false;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
i++;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return str.slice(i);
|
|
53
|
+
};
|
|
54
|
+
export const isEmpty = (str) => str == null || str.length === 0;
|
|
55
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAC;AAE7B,MAAM,CAAC,MAAM,aAAa,GAAG,CAC3B,MAAS,EACT,MAAgC,EAC7B,EAAE;IACL,MAAM,YAAY,GAAG,MAAiC,CAAC;IAEvD,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;QAC9C,IAAI,YAAY,CAAC,GAAG,CAAC,KAAK,SAAS,EAAE,CAAC;YACpC,YAAY,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;QAC5B,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,WAAW,GAAG,CACzB,MAAS,EACT,MAAgC,EAC7B,EAAE;IACL,MAAM,YAAY,GAAG,MAAiC,CAAC;IAEvD,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,EAAE,EAAE;QAC9C,YAAY,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IAC5B,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CACxB,YAAoB,EACpB,YAAoB,EACZ,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,YAAY,EAAE,YAAY,CAAC,CAAC;AAEtD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CAAC,GAAW,EAAU,EAAE;IAC/D,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IAC5B,IAAI,QAAQ,GAAG,IAAI,CAAC;IACpB,IAAI,CAAC,GAAG,CAAC,CAAC;IACV,OAAO,QAAQ,IAAI,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC;QACpC,MAAM,WAAW,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QAC7B,IAAI,WAAW,KAAK,SAAS,IAAI,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC;YAC7D,QAAQ,GAAG,KAAK,CAAC;QACnB,CAAC;aAAM,CAAC;YACN,CAAC,EAAE,CAAC;QACN,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACtB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,GAA8B,EAAW,EAAE,CACjE,GAAG,IAAI,IAAI,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cogs/config",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Framework-agnostic layered-dotenv loader — resolves the canonical .env file chain, expands ${VAR} references, and folds values into process.env with deploy-injected variables staying authoritative",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/catesandrew/cogs.git",
|
|
9
|
+
"directory": "packages/config"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"main": "./dist/index.js",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"import": "./dist/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./preload": {
|
|
20
|
+
"types": "./dist/preload.d.ts",
|
|
21
|
+
"import": "./dist/preload.js"
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"!dist/tsconfig.tsbuildinfo"
|
|
28
|
+
],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"dotenv": "^16.6.1",
|
|
34
|
+
"dotenv-expand": "^12.0.3"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"vitest": "^3.0.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -p tsconfig.json",
|
|
41
|
+
"lint": "biome check .",
|
|
42
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
43
|
+
"test": "vitest run"
|
|
44
|
+
}
|
|
45
|
+
}
|