@adonisjs/env 3.0.8 → 4.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/build/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { Env } from './src/Env';
2
- export { EnvParser } from './src/Parser';
3
- export { envLoader } from './src/loader';
1
+ export { Env } from './src/env.js';
2
+ export { EnvParser } from './src/parser.js';
3
+ export { EnvLoader } from './src/loader.js';
package/build/index.js CHANGED
@@ -1,17 +1,3 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/env
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.envLoader = exports.EnvParser = exports.Env = void 0;
12
- var Env_1 = require("./src/Env");
13
- Object.defineProperty(exports, "Env", { enumerable: true, get: function () { return Env_1.Env; } });
14
- var Parser_1 = require("./src/Parser");
15
- Object.defineProperty(exports, "EnvParser", { enumerable: true, get: function () { return Parser_1.EnvParser; } });
16
- var loader_1 = require("./src/loader");
17
- Object.defineProperty(exports, "envLoader", { enumerable: true, get: function () { return loader_1.envLoader; } });
1
+ export { Env } from './src/env.js';
2
+ export { EnvParser } from './src/parser.js';
3
+ export { EnvLoader } from './src/loader.js';
@@ -0,0 +1,21 @@
1
+ import { EnvValidator } from './validator.js';
2
+ import { type ValidateFn } from '@poppinss/validator-lite';
3
+ export declare class Env<EnvValues extends Record<string, any>> {
4
+ #private;
5
+ constructor(values: EnvValues);
6
+ static schema: {
7
+ number: typeof import("@poppinss/validator-lite/build/src/schema/number.js").number;
8
+ string: typeof import("@poppinss/validator-lite/build/src/schema/string.js").string;
9
+ boolean: typeof import("@poppinss/validator-lite/build/src/schema/boolean.js").boolean;
10
+ enum: typeof import("@poppinss/validator-lite/build/src/schema/oneOf.js").oneOf;
11
+ };
12
+ static rules<T extends {
13
+ [key: string]: ValidateFn<unknown>;
14
+ }>(schema: T): EnvValidator<T>['validate'];
15
+ get<K extends keyof EnvValues>(key: K): EnvValues[K];
16
+ get<K extends keyof EnvValues>(key: K, defaultValue: Exclude<EnvValues[K], undefined>): Exclude<EnvValues[K], undefined>;
17
+ get(key: string): string | undefined;
18
+ get(key: string, defaultValue: string): string;
19
+ set<K extends keyof EnvValues>(key: K, value: EnvValues[K]): void;
20
+ set(key: string, value: string): void;
21
+ }
@@ -0,0 +1,27 @@
1
+ import { EnvValidator } from './validator.js';
2
+ import { schema as envSchema } from '@poppinss/validator-lite';
3
+ export class Env {
4
+ #values;
5
+ constructor(values) {
6
+ this.#values = values;
7
+ }
8
+ static schema = envSchema;
9
+ static rules(schema) {
10
+ const validator = new EnvValidator(schema);
11
+ return validator.validate.bind(validator);
12
+ }
13
+ get(key, defaultValue) {
14
+ if (this.#values[key] !== undefined) {
15
+ return this.#values[key];
16
+ }
17
+ const envValue = process.env[key];
18
+ if (envValue) {
19
+ return envValue;
20
+ }
21
+ return defaultValue;
22
+ }
23
+ set(key, value) {
24
+ this.#values[key] = value;
25
+ process.env[key] = value;
26
+ }
27
+ }
@@ -0,0 +1,5 @@
1
+ import { Exception } from '@poppinss/utils';
2
+ export declare class MissingEnvPathFileException extends Exception {
3
+ static status: number;
4
+ static code: string;
5
+ }
@@ -0,0 +1,5 @@
1
+ import { Exception } from '@poppinss/utils';
2
+ export class MissingEnvPathFileException extends Exception {
3
+ static status = 500;
4
+ static code = 'E_MISSING_ENV_PATH_FILE';
5
+ }
@@ -1,7 +1,9 @@
1
- /**
2
- * Reads `.env` file contents
3
- */
4
- export declare function envLoader(appRoot: string): {
5
- envContents: string;
6
- testEnvContent: string;
7
- };
1
+ /// <reference types="node" resolution-mode="require"/>
2
+ export declare class EnvLoader {
3
+ #private;
4
+ constructor(appRoot: string | URL);
5
+ load(): Promise<{
6
+ envContents: string;
7
+ currentEnvContents: string;
8
+ }>;
9
+ }
@@ -1,49 +1,34 @@
1
- "use strict";
2
- /*
3
- * @adonisjs/env
4
- *
5
- * (c) Harminder Virk <virk@adonisjs.com>
6
- *
7
- * For the full copyright and license information, please view the LICENSE
8
- * file that was distributed with this source code.
9
- */
10
- Object.defineProperty(exports, "__esModule", { value: true });
11
- exports.envLoader = void 0;
12
- const fs_1 = require("fs");
13
- const path_1 = require("path");
14
- const utils_1 = require("@poppinss/utils");
15
- /**
16
- * Loads file from the disk and optionally ignores the missing
17
- * file errors
18
- */
19
- function loadFile(filePath, optional = false) {
20
- try {
21
- return (0, fs_1.readFileSync)(filePath, 'utf-8');
1
+ import { fileURLToPath } from 'node:url';
2
+ import { readFile } from 'node:fs/promises';
3
+ import { isAbsolute, join } from 'node:path';
4
+ import { MissingEnvPathFileException } from './exceptions/missing_env_path_file.js';
5
+ export class EnvLoader {
6
+ #appRoot;
7
+ constructor(appRoot) {
8
+ this.#appRoot = typeof appRoot === 'string' ? appRoot : fileURLToPath(appRoot);
22
9
  }
23
- catch (error) {
24
- if (error.code !== 'ENOENT') {
25
- throw error;
10
+ async #loadFile(filePath, optional = false) {
11
+ try {
12
+ return await readFile(filePath, 'utf-8');
26
13
  }
27
- if (!optional) {
28
- throw new utils_1.Exception(`The "${filePath}" file is missing`, 500, 'E_MISSING_ENV_FILE');
14
+ catch (error) {
15
+ if (error.code !== 'ENOENT') {
16
+ throw error;
17
+ }
18
+ if (optional) {
19
+ return '';
20
+ }
21
+ throw new MissingEnvPathFileException(`Cannot find env file from "ENV_PATH"`, {
22
+ cause: error,
23
+ });
29
24
  }
30
25
  }
31
- return '';
32
- }
33
- /**
34
- * Reads `.env` file contents
35
- */
36
- function envLoader(appRoot) {
37
- const envPath = process.env.ENV_PATH || '.env';
38
- const absPath = (0, path_1.isAbsolute)(envPath) ? envPath : (0, path_1.join)(appRoot, envPath);
39
- const envContents = loadFile(absPath, true);
40
- /**
41
- * Optionally loading the `.env.testing` file in test environment
42
- */
43
- let testEnvContent = '';
44
- if (process.env.NODE_ENV === 'testing') {
45
- testEnvContent = loadFile((0, path_1.join)(appRoot, '.env.testing'), true);
26
+ async load() {
27
+ const envFile = process.env.ENV_PATH || '.env';
28
+ const envPath = isAbsolute(envFile) ? envFile : join(this.#appRoot, envFile);
29
+ const envContents = await this.#loadFile(envPath, !process.env.ENV_PATH);
30
+ const currentEnvPath = join(this.#appRoot, `.env.${process.env.NODE_ENV}`);
31
+ const currentEnvContents = await this.#loadFile(currentEnvPath, true);
32
+ return { envContents, currentEnvContents };
46
33
  }
47
- return { testEnvContent, envContents };
48
34
  }
49
- exports.envLoader = envLoader;
@@ -0,0 +1,6 @@
1
+ import { DotenvParseOutput } from 'dotenv';
2
+ export declare class EnvParser {
3
+ #private;
4
+ constructor(envContents: string);
5
+ parse(): DotenvParseOutput;
6
+ }
@@ -0,0 +1,60 @@
1
+ import dotenv from 'dotenv';
2
+ export class EnvParser {
3
+ #envContents;
4
+ constructor(envContents) {
5
+ this.#envContents = envContents;
6
+ }
7
+ #getValue(key, parsed) {
8
+ if (parsed[key]) {
9
+ return this.#interpolate(parsed[key], parsed);
10
+ }
11
+ return '';
12
+ }
13
+ #interpolateMustache(token, parsed) {
14
+ const closingBrace = token.indexOf('}');
15
+ if (closingBrace === -1) {
16
+ return token;
17
+ }
18
+ const varReference = token.slice(1, closingBrace).trim();
19
+ return `${this.#getValue(varReference, parsed)}${token.slice(closingBrace + 1)}`;
20
+ }
21
+ #interpolateVariable(token, parsed) {
22
+ return token.replace(/[a-zA-Z0-9_]+/, (key) => {
23
+ return this.#getValue(key, parsed);
24
+ });
25
+ }
26
+ #interpolate(value, parsed) {
27
+ const tokens = value.split('$');
28
+ let newValue = '';
29
+ let skipNextToken = true;
30
+ tokens.forEach((token) => {
31
+ if (token === '\\') {
32
+ newValue += '$';
33
+ skipNextToken = true;
34
+ return;
35
+ }
36
+ if (skipNextToken) {
37
+ newValue += token.replace(/\\$/, '$');
38
+ if (token.endsWith('\\')) {
39
+ return;
40
+ }
41
+ }
42
+ else {
43
+ if (token.startsWith('{')) {
44
+ newValue += this.#interpolateMustache(token, parsed);
45
+ return;
46
+ }
47
+ newValue += this.#interpolateVariable(token, parsed);
48
+ }
49
+ skipNextToken = false;
50
+ });
51
+ return newValue;
52
+ }
53
+ parse() {
54
+ const envCollection = dotenv.parse(this.#envContents.trim());
55
+ return Object.keys(envCollection).reduce((result, key) => {
56
+ result[key] = this.#interpolate(envCollection[key], envCollection);
57
+ return result;
58
+ }, {});
59
+ }
60
+ }
@@ -0,0 +1,12 @@
1
+ import { ValidateFn } from '@poppinss/validator-lite';
2
+ export declare class EnvValidator<Schema extends {
3
+ [key: string]: ValidateFn<unknown>;
4
+ }> {
5
+ #private;
6
+ constructor(schema: Schema);
7
+ validate(values: {
8
+ [K: string]: string | undefined;
9
+ }): {
10
+ [K in keyof Schema]: ReturnType<Schema[K]>;
11
+ };
12
+ }
@@ -0,0 +1,12 @@
1
+ export class EnvValidator {
2
+ #schema;
3
+ constructor(schema) {
4
+ this.#schema = schema;
5
+ }
6
+ validate(values) {
7
+ return Object.keys(this.#schema).reduce((result, key) => {
8
+ result[key] = this.#schema[key](key, values[key]);
9
+ return result;
10
+ }, { ...values });
11
+ }
12
+ }
package/package.json CHANGED
@@ -1,142 +1,118 @@
1
1
  {
2
- "name": "@adonisjs/env",
3
- "version": "3.0.8",
4
- "description": "Environment variable manager for Node.js",
5
- "main": "build/index.js",
6
- "files": [
7
- "build/adonis-typings",
8
- "build/src",
9
- "build/index.d.ts",
10
- "build/index.js"
11
- ],
12
- "scripts": {
13
- "mrm": "mrm --preset=@adonisjs/mrm-preset",
14
- "pretest": "npm run lint",
15
- "test": "node -r @adonisjs/require-ts/build/register bin/test.ts",
16
- "clean": "del-cli build",
17
- "compile": "npm run lint && npm run clean && tsc",
18
- "build": "npm run compile",
19
- "commit": "git-cz",
20
- "release": "np --message=\"chore(release): %s\"",
21
- "version": "npm run build",
22
- "prepublishOnly": "npm run build",
23
- "lint": "eslint . --ext=.ts",
24
- "format": "prettier --write .",
25
- "sync-labels": "github-label-sync --labels ./node_modules/@adonisjs/mrm-preset/gh-labels.json adonisjs/env"
26
- },
27
- "keywords": [
28
- "env",
29
- "adonisjs"
30
- ],
31
- "author": "virk,adonisjs",
32
- "license": "MIT",
33
- "devDependencies": {
34
- "@adonisjs/mrm-preset": "^5.0.3",
35
- "@adonisjs/require-ts": "^2.0.10",
36
- "@japa/assert": "^1.2.3",
37
- "@japa/run-failed-tests": "^1.0.3",
38
- "@japa/runner": "^2.0.6",
39
- "@japa/spec-reporter": "^1.1.7",
40
- "@poppinss/dev-utils": "^2.0.2",
41
- "@types/node": "^17.0.23",
42
- "commitizen": "^4.2.4",
43
- "cz-conventional-changelog": "^3.3.0",
44
- "del-cli": "^4.0.1",
45
- "eslint": "^8.12.0",
46
- "eslint-config-prettier": "^8.5.0",
47
- "eslint-plugin-adonis": "^2.1.0",
48
- "eslint-plugin-prettier": "^4.0.0",
49
- "github-label-sync": "^2.1.1",
50
- "husky": "^7.0.1",
51
- "mrm": "^4.0.0",
52
- "np": "^7.6.1",
53
- "prettier": "^2.6.2",
54
- "typescript": "^4.6.3"
55
- },
56
- "nyc": {
57
- "exclude": [
58
- "test"
59
- ],
60
- "extension": [
61
- ".ts"
62
- ]
63
- },
64
- "husky": {
65
- "hooks": {
66
- "commit-msg": "node ./node_modules/@adonisjs/mrm-preset/validateCommit/conventional/validate.js"
67
- }
68
- },
69
- "config": {
70
- "commitizen": {
71
- "path": "cz-conventional-changelog"
72
- }
73
- },
74
- "np": {
75
- "contents": ".",
76
- "anyBranch": false
77
- },
78
- "dependencies": {
79
- "@poppinss/utils": "^4.0.2",
80
- "dotenv": "^16.0.0",
81
- "validator": "^13.7.0"
82
- },
83
- "directories": {
84
- "doc": "docs",
85
- "test": "test"
86
- },
87
- "repository": {
88
- "type": "git",
89
- "url": "git+https://github.com/adonisjs/env.git"
90
- },
91
- "bugs": {
92
- "url": "https://github.com/adonisjs/env/issues"
93
- },
94
- "homepage": "https://github.com/adonisjs/env#readme",
95
- "publishConfig": {
96
- "access": "public",
97
- "tag": "latest"
98
- },
99
- "mrmConfig": {
100
- "core": true,
101
- "license": "MIT",
102
- "services": [
103
- "github-actions"
104
- ],
105
- "minNodeVersion": "14.15.4",
106
- "probotApps": [
107
- "stale",
108
- "lock"
109
- ],
110
- "runGhActionsOnWindows": false
111
- },
112
- "eslintConfig": {
113
- "extends": [
114
- "plugin:adonis/typescriptPackage",
115
- "prettier"
116
- ],
117
- "plugins": [
118
- "prettier"
119
- ],
120
- "rules": {
121
- "prettier/prettier": [
122
- "error",
123
- {
124
- "endOfLine": "auto"
125
- }
126
- ]
127
- }
128
- },
129
- "eslintIgnore": [
130
- "build"
131
- ],
132
- "prettier": {
133
- "trailingComma": "es5",
134
- "semi": false,
135
- "singleQuote": true,
136
- "useTabs": false,
137
- "quoteProps": "consistent",
138
- "bracketSpacing": true,
139
- "arrowParens": "always",
140
- "printWidth": 100
141
- }
2
+ "name": "@adonisjs/env",
3
+ "version": "4.0.0-0",
4
+ "description": "Environment variable manager for Node.js",
5
+ "main": "build/index.js",
6
+ "type": "module",
7
+ "files": [
8
+ "build/src",
9
+ "build/index.d.ts",
10
+ "build/index.js"
11
+ ],
12
+ "exports": {
13
+ ".": "./build/index.js"
14
+ },
15
+ "scripts": {
16
+ "pretest": "npm run lint",
17
+ "test": "npm run vscode:test",
18
+ "clean": "del-cli build",
19
+ "compile": "npm run lint && npm run clean && tsc",
20
+ "build": "npm run compile",
21
+ "release": "np",
22
+ "version": "npm run build",
23
+ "prepublishOnly": "npm run build",
24
+ "lint": "eslint . --ext=.ts",
25
+ "format": "prettier --write .",
26
+ "sync-labels": "github-label-sync --labels .github/labels.json adonisjs/env",
27
+ "vscode:test": "node --loader=ts-node/esm bin/test.ts"
28
+ },
29
+ "keywords": [
30
+ "env",
31
+ "adonisjs"
32
+ ],
33
+ "author": "virk,adonisjs",
34
+ "license": "MIT",
35
+ "devDependencies": {
36
+ "@commitlint/cli": "^17.1.2",
37
+ "@commitlint/config-conventional": "^17.1.0",
38
+ "@japa/assert": "^1.3.6",
39
+ "@japa/expect-type": "^1.0.2",
40
+ "@japa/run-failed-tests": "^1.1.0",
41
+ "@japa/runner": "^2.2.2",
42
+ "@japa/spec-reporter": "^1.3.2",
43
+ "@poppinss/dev-utils": "^2.0.3",
44
+ "@swc/core": "^1.3.9",
45
+ "@types/fs-extra": "^9.0.13",
46
+ "@types/node": "^18.11.0",
47
+ "del-cli": "^5.0.0",
48
+ "eslint": "^8.25.0",
49
+ "eslint-config-prettier": "^8.5.0",
50
+ "eslint-plugin-adonis": "^3.0.3",
51
+ "eslint-plugin-prettier": "^4.2.1",
52
+ "fs-extra": "^10.1.0",
53
+ "github-label-sync": "^2.2.0",
54
+ "husky": "^8.0.1",
55
+ "np": "^7.6.2",
56
+ "prettier": "^2.7.1",
57
+ "ts-node": "^10.9.1",
58
+ "typescript": "^4.8.4"
59
+ },
60
+ "dependencies": {
61
+ "@poppinss/utils": "^6.0.0-1",
62
+ "@poppinss/validator-lite": "^1.0.1",
63
+ "dotenv": "^16.0.3"
64
+ },
65
+ "repository": {
66
+ "type": "git",
67
+ "url": "git+https://github.com/adonisjs/env.git"
68
+ },
69
+ "bugs": {
70
+ "url": "https://github.com/adonisjs/env/issues"
71
+ },
72
+ "homepage": "https://github.com/adonisjs/env#readme",
73
+ "eslintConfig": {
74
+ "extends": [
75
+ "plugin:adonis/typescriptPackage",
76
+ "prettier"
77
+ ],
78
+ "plugins": [
79
+ "prettier"
80
+ ],
81
+ "rules": {
82
+ "prettier/prettier": [
83
+ "error",
84
+ {
85
+ "endOfLine": "auto"
86
+ }
87
+ ]
88
+ }
89
+ },
90
+ "eslintIgnore": [
91
+ "build"
92
+ ],
93
+ "prettier": {
94
+ "trailingComma": "es5",
95
+ "semi": false,
96
+ "singleQuote": true,
97
+ "useTabs": false,
98
+ "quoteProps": "consistent",
99
+ "bracketSpacing": true,
100
+ "arrowParens": "always",
101
+ "printWidth": 100
102
+ },
103
+ "commitlint": {
104
+ "extends": [
105
+ "@commitlint/config-conventional"
106
+ ]
107
+ },
108
+ "publishConfig": {
109
+ "access": "public",
110
+ "tag": "next"
111
+ },
112
+ "np": {
113
+ "message": "chore(release): %s",
114
+ "tag": "next",
115
+ "branch": "main",
116
+ "anyBranch": false
117
+ }
142
118
  }