dotenv-gad 1.2.2 → 1.2.3
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/dist/cli/commands/sync.js +4 -1
- package/dist/cli/commands/utils.js +6 -1
- package/dist/schema.js +10 -4
- package/dist/types/index.d.ts +2 -1
- package/dist/types/schema.d.ts +11 -5
- package/dist/types/types.d.ts +28 -0
- package/dist/types/utils.d.ts +12 -8
- package/dist/types.js +1 -0
- package/dist/utils.js +9 -6
- package/package.json +9 -9
|
@@ -12,6 +12,9 @@ export default function (program) {
|
|
|
12
12
|
const spinner = ora("Generating .env.example.......").start();
|
|
13
13
|
try {
|
|
14
14
|
const schema = await loadSchema(rootOpts.schema);
|
|
15
|
+
if (!schema || typeof schema !== "object") {
|
|
16
|
+
throw new Error(`The schema loaded from "${rootOpts.schema}" is not valid.`);
|
|
17
|
+
}
|
|
15
18
|
let exampleContent = "# Auto-generated by dotenv-gad\n\n";
|
|
16
19
|
Object.entries(schema).forEach(([key, rule]) => {
|
|
17
20
|
if (rule.sensitive)
|
|
@@ -49,7 +52,7 @@ export default function (program) {
|
|
|
49
52
|
}
|
|
50
53
|
catch (error) {
|
|
51
54
|
spinner.fail(chalk.red(`Failed to generate .env.example`));
|
|
52
|
-
console.error(error);
|
|
55
|
+
console.error(chalk.yellow(error.message));
|
|
53
56
|
process.exit(1);
|
|
54
57
|
}
|
|
55
58
|
});
|
|
@@ -15,7 +15,12 @@ export async function loadSchema(schemaPath) {
|
|
|
15
15
|
const absPath = resolve(schemaPath);
|
|
16
16
|
const importModule = async (filePath) => {
|
|
17
17
|
const fileUrl = pathToFileURL(filePath).href;
|
|
18
|
-
|
|
18
|
+
const imported = await import(`${fileUrl}?t=${Date.now()}`);
|
|
19
|
+
const schema = imported.default || imported.schema || imported;
|
|
20
|
+
if (!schema || typeof schema !== "object") {
|
|
21
|
+
throw new Error(`Schema not found. Ensure you 'export default' or export const schema' in your file.`);
|
|
22
|
+
}
|
|
23
|
+
return schema;
|
|
19
24
|
};
|
|
20
25
|
const loadTsModule = async (tsFilePath) => {
|
|
21
26
|
const tempFile = join(__dirname, `../../temp-schema-${Date.now()}.mjs`);
|
package/dist/schema.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* A type-safe way to define your environment schema.
|
|
3
3
|
*
|
|
4
|
-
* @
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* @example
|
|
5
|
+
* const schema = defineSchema({
|
|
6
|
+
* APP_NAME: { type: "string", required: true },
|
|
7
|
+
* APP_PORT: { type: "number", default: 3000 },
|
|
8
|
+
* });
|
|
9
|
+
*
|
|
10
|
+
* @template S
|
|
11
|
+
* @param {S} schema - Environment schema definition
|
|
12
|
+
* @returns {S} - The same schema definition, but with type safety
|
|
7
13
|
*/
|
|
8
14
|
export function defineSchema(schema) {
|
|
9
15
|
return schema;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -5,8 +5,9 @@ import { loadEnv, createEnvProxy } from "./utils.js";
|
|
|
5
5
|
import { composeSchema } from "./compose.js";
|
|
6
6
|
import loadSchema from "./cli/commands/types.js";
|
|
7
7
|
import { applyFix } from "./cli/commands/utils.js";
|
|
8
|
+
import { ExtractEnv } from "./types.js";
|
|
8
9
|
export { defineSchema, AggregateError, EnvValidationError, EnvValidator, loadEnv, createEnvProxy, composeSchema, loadSchema, applyFix, };
|
|
9
|
-
export type { SchemaDefinition, SchemaRule };
|
|
10
|
+
export type { SchemaDefinition, SchemaRule, ExtractEnv };
|
|
10
11
|
export declare function validateEnv(schema: SchemaDefinition, options?: {
|
|
11
12
|
strict?: boolean;
|
|
12
13
|
}): Record<string, any>;
|
package/dist/types/schema.d.ts
CHANGED
|
@@ -26,11 +26,17 @@ export interface SchemaRule {
|
|
|
26
26
|
}
|
|
27
27
|
export type SchemaDefinition = Record<string, SchemaRule>;
|
|
28
28
|
/**
|
|
29
|
-
*
|
|
29
|
+
* A type-safe way to define your environment schema.
|
|
30
30
|
*
|
|
31
|
-
* @
|
|
32
|
-
*
|
|
33
|
-
*
|
|
31
|
+
* @example
|
|
32
|
+
* const schema = defineSchema({
|
|
33
|
+
* APP_NAME: { type: "string", required: true },
|
|
34
|
+
* APP_PORT: { type: "number", default: 3000 },
|
|
35
|
+
* });
|
|
36
|
+
*
|
|
37
|
+
* @template S
|
|
38
|
+
* @param {S} schema - Environment schema definition
|
|
39
|
+
* @returns {S} - The same schema definition, but with type safety
|
|
34
40
|
*/
|
|
35
|
-
export declare function defineSchema(schema:
|
|
41
|
+
export declare function defineSchema<const S extends SchemaDefinition>(schema: S): S;
|
|
36
42
|
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { SchemaDefinition, SchemaRule } from "./schema.js";
|
|
2
|
+
/**
|
|
3
|
+
* Type inference utilities for dotenv-gad.
|
|
4
|
+
* This provides autocomplete and type safety for environment variables.
|
|
5
|
+
*/
|
|
6
|
+
type TypeFromSchemaRule<T extends SchemaRule> = T['enum'] extends ReadonlyArray<infer E> ? E : T['type'] extends 'string' | 'email' | 'url' | 'ip' ? string : T['type'] extends 'number' | 'port' ? number : T['type'] extends 'boolean' ? boolean : T['type'] extends 'date' ? Date : T['type'] extends 'json' ? any : T['type'] extends 'array' ? T['items'] extends SchemaRule ? Array<TypeFromSchemaRule<T['items']>> : any[] : T['type'] extends 'object' ? T['properties'] extends Record<string, SchemaRule> ? {
|
|
7
|
+
[K in keyof T['properties']]: TypeFromSchemaRule<T['properties'][K]>;
|
|
8
|
+
} : Record<string, any> : any;
|
|
9
|
+
/**
|
|
10
|
+
* A property is optional if rrquired is explicitly false or
|
|
11
|
+
* required is not set and no default is provided.
|
|
12
|
+
*/
|
|
13
|
+
type IsOptional<T extends SchemaRule> = T['required'] extends true ? false : T['default'] extends undefined ? true : false;
|
|
14
|
+
/**
|
|
15
|
+
* Convert schema rule to Optional or Required type
|
|
16
|
+
*/
|
|
17
|
+
type PropertyToType<T extends SchemaRule> = IsOptional<T> extends true ? TypeFromSchemaRule<T> | undefined : TypeFromSchemaRule<T>;
|
|
18
|
+
export type InferEnv<S extends SchemaDefinition> = {
|
|
19
|
+
[K in keyof S]: PropertyToType<S[K]>;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Utility type to extract the environment type from a schema
|
|
23
|
+
*
|
|
24
|
+
* Usage:
|
|
25
|
+
* type MyEnv = ExtractEnv<typeof mySchema>
|
|
26
|
+
*/
|
|
27
|
+
export type ExtractEnv<S> = S extends SchemaDefinition ? InferEnv<S> : never;
|
|
28
|
+
export {};
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -1,18 +1,22 @@
|
|
|
1
1
|
import { SchemaDefinition } from "./schema.js";
|
|
2
|
+
import type { InferEnv } from "./types.js";
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
4
|
+
* Loads environment variables from .env file and validates them against
|
|
5
|
+
* the given schema.
|
|
5
6
|
*
|
|
6
|
-
* @
|
|
7
|
-
* @param
|
|
8
|
-
*
|
|
9
|
-
* @
|
|
7
|
+
* @template S The type of the schema definition.
|
|
8
|
+
* @param {S} schema The schema definition for the environment variables.
|
|
9
|
+
* @param {Object} [options] Optional options for the validation process.
|
|
10
|
+
* @param {boolean} [options.strict] When true, fail on environment variables not present in the schema.
|
|
11
|
+
* @param {boolean} [options.includeRaw] Include raw values in error reports (non-sensitive by default).
|
|
12
|
+
* @param {boolean} [options.includeSensitive] When used with `includeRaw`, will reveal values marked sensitive (use only for local debugging).
|
|
13
|
+
* @returns {InferEnv<S>} The validated environment variables.
|
|
10
14
|
*/
|
|
11
|
-
export declare function loadEnv(schema:
|
|
15
|
+
export declare function loadEnv<S extends SchemaDefinition>(schema: S, options?: {
|
|
12
16
|
strict?: boolean;
|
|
13
17
|
includeRaw?: boolean;
|
|
14
18
|
includeSensitive?: boolean;
|
|
15
|
-
}):
|
|
19
|
+
}): InferEnv<S>;
|
|
16
20
|
/**
|
|
17
21
|
* Create a proxy around the validated environment variables. The proxy will
|
|
18
22
|
* throw an error if you try to access a variable that is not validated.
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/utils.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import dotenv from "dotenv";
|
|
2
2
|
import { EnvValidator } from "./validator.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Loads environment variables from .env file and validates them against
|
|
5
|
+
* the given schema.
|
|
6
6
|
*
|
|
7
|
-
* @
|
|
8
|
-
* @param
|
|
9
|
-
*
|
|
10
|
-
* @
|
|
7
|
+
* @template S The type of the schema definition.
|
|
8
|
+
* @param {S} schema The schema definition for the environment variables.
|
|
9
|
+
* @param {Object} [options] Optional options for the validation process.
|
|
10
|
+
* @param {boolean} [options.strict] When true, fail on environment variables not present in the schema.
|
|
11
|
+
* @param {boolean} [options.includeRaw] Include raw values in error reports (non-sensitive by default).
|
|
12
|
+
* @param {boolean} [options.includeSensitive] When used with `includeRaw`, will reveal values marked sensitive (use only for local debugging).
|
|
13
|
+
* @returns {InferEnv<S>} The validated environment variables.
|
|
11
14
|
*/
|
|
12
15
|
export function loadEnv(schema, options) {
|
|
13
16
|
const env = dotenv.config().parsed || {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dotenv-gad",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/types/index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -72,24 +72,24 @@
|
|
|
72
72
|
"@types/jest": "^30.0.0",
|
|
73
73
|
"@types/node": "^24.0.3",
|
|
74
74
|
"@types/ora": "^3.1.0",
|
|
75
|
+
"chalk": "^5.3.0",
|
|
76
|
+
"commander": "^12.1.0",
|
|
75
77
|
"cross-env": "^7.0.3",
|
|
76
78
|
"cross-env-shell": "^7.0.3",
|
|
79
|
+
"dotenv": "^16.5.0",
|
|
77
80
|
"esbuild": "^0.25.5",
|
|
78
81
|
"eslint": "^9.17.0",
|
|
82
|
+
"figlet": "^1.8.0",
|
|
79
83
|
"globals": "^15.14.0",
|
|
80
84
|
"husky": "^9.1.7",
|
|
85
|
+
"inquirer": "^12.3.0",
|
|
81
86
|
"jest": "^30.0.3",
|
|
82
87
|
"jest-environment-jsdom": "^30.0.2",
|
|
88
|
+
"ora": "^8.1.1",
|
|
83
89
|
"prettier": "^3.4.2",
|
|
84
90
|
"ts-jest": "^29.4.0",
|
|
91
|
+
"tsup": "^8.5.1",
|
|
85
92
|
"typescript-eslint": "^8.19.1"
|
|
86
93
|
},
|
|
87
|
-
"dependencies": {
|
|
88
|
-
"chalk": "^5.3.0",
|
|
89
|
-
"commander": "^12.1.0",
|
|
90
|
-
"dotenv": "^16.5.0",
|
|
91
|
-
"figlet": "^1.8.0",
|
|
92
|
-
"inquirer": "^12.3.0",
|
|
93
|
-
"ora": "^8.1.1"
|
|
94
|
-
}
|
|
94
|
+
"dependencies": {}
|
|
95
95
|
}
|