@romaintaillandier1978/dotenv-never-lies 0.3.0 → 1.1.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 +437 -160
- package/dist/cli/commands/assert.d.ts +4 -2
- package/dist/cli/commands/assert.d.ts.map +1 -1
- package/dist/cli/commands/assert.js +7 -9
- package/dist/cli/commands/explain.d.ts +6 -1
- package/dist/cli/commands/explain.d.ts.map +1 -1
- package/dist/cli/commands/explain.js +9 -16
- package/dist/cli/commands/export.d.ts +35 -0
- package/dist/cli/commands/export.d.ts.map +1 -0
- package/dist/cli/commands/export.js +268 -0
- package/dist/cli/commands/generate.d.ts +10 -2
- package/dist/cli/commands/generate.d.ts.map +1 -1
- package/dist/cli/commands/generate.js +9 -7
- package/dist/cli/commands/reverseEnv.d.ts +8 -2
- package/dist/cli/commands/reverseEnv.d.ts.map +1 -1
- package/dist/cli/commands/reverseEnv.js +21 -8
- package/dist/cli/index.js +323 -99
- package/dist/cli/utils/exitCodes.d.ts +8 -0
- package/dist/cli/utils/exitCodes.d.ts.map +1 -0
- package/dist/cli/utils/exitCodes.js +8 -0
- package/dist/cli/utils/infer-schema.d.ts.map +1 -1
- package/dist/cli/utils/infer-schema.js +4 -3
- package/dist/cli/utils/load-schema.d.ts.map +1 -1
- package/dist/cli/utils/load-schema.js +20 -12
- package/dist/cli/utils/printer.d.ts.map +1 -1
- package/dist/cli/utils/printer.js +5 -4
- package/dist/cli/utils/resolve-schema.d.ts.map +1 -1
- package/dist/cli/utils/resolve-schema.js +7 -3
- package/dist/cli/utils/toFile.d.ts +2 -0
- package/dist/cli/utils/toFile.d.ts.map +1 -0
- package/dist/cli/utils/toFile.js +8 -0
- package/dist/core.d.ts +33 -33
- package/dist/core.js +10 -10
- package/dist/errors.d.ts +29 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +33 -4
- package/dist/romaintaillandier1978-dotenv-never-lies-0.3.0.tgz +0 -0
- package/package.json +9 -9
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ExportError } from "../../errors.js";
|
|
2
|
+
import fs from "node:fs";
|
|
3
|
+
export const toFile = async (content, path, force = false) => {
|
|
4
|
+
if (fs.existsSync(path) && !force) {
|
|
5
|
+
throw new ExportError(`${path} already exists. Use --force to overwrite.`);
|
|
6
|
+
}
|
|
7
|
+
fs.writeFileSync(path, content);
|
|
8
|
+
};
|
package/dist/core.d.ts
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* An object containing environment variables as strings, coming from a .env file or from process.env.
|
|
4
4
|
*/
|
|
5
5
|
export type EnvSource = Record<string, string | undefined>;
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
7
|
+
* An environment variable.
|
|
8
8
|
*/
|
|
9
9
|
export interface EnvVarDefinition<T extends z.ZodType = z.ZodType> {
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
11
|
+
* The Zod schema of the environment variable.
|
|
12
12
|
*/
|
|
13
13
|
schema: T;
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* The description of the environment variable.
|
|
16
16
|
*/
|
|
17
17
|
description: string;
|
|
18
18
|
/**
|
|
19
|
-
*
|
|
19
|
+
* Whether the environment variable is secret (for tokens, passwords), reserved for future use (RFU).
|
|
20
20
|
*/
|
|
21
21
|
secret?: boolean;
|
|
22
22
|
/**
|
|
23
|
-
*
|
|
23
|
+
* Provides examples for this variable.
|
|
24
24
|
*/
|
|
25
25
|
examples?: string[];
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
*
|
|
28
|
+
* An object containing the defined environment variables.
|
|
29
29
|
*/
|
|
30
30
|
export type EnvDefinition = Record<string, EnvVarDefinition<any>>;
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
32
|
+
* The Zod shape of the environment schema.
|
|
33
33
|
*/
|
|
34
34
|
export type ZodShapeFromEnv<T extends EnvDefinition> = {
|
|
35
35
|
[K in keyof T & string]: T[K]["schema"];
|
|
36
36
|
};
|
|
37
37
|
/**
|
|
38
|
-
*
|
|
38
|
+
* The inferred type of the environment schema.
|
|
39
39
|
*/
|
|
40
40
|
export type InferEnv<T extends EnvDefinition> = {
|
|
41
41
|
[K in keyof T & string]: z.infer<T[K]["schema"]>;
|
|
@@ -47,60 +47,60 @@ type AssertFn<T extends EnvDefinition> = (options?: {
|
|
|
47
47
|
source?: EnvSource | undefined;
|
|
48
48
|
}) => InferEnv<T>;
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
51
|
-
* @template T -
|
|
50
|
+
* An object containing functions to check and assert environment variables.
|
|
51
|
+
* @template T - The environment schema to define.
|
|
52
52
|
*/
|
|
53
53
|
export type EnvDefinitionHelper<T extends EnvDefinition> = {
|
|
54
54
|
/**
|
|
55
|
-
*
|
|
55
|
+
* The defined environment schema.
|
|
56
56
|
*/
|
|
57
57
|
def: T;
|
|
58
58
|
/**
|
|
59
|
-
*
|
|
59
|
+
* The Zod shape of the environment schema.
|
|
60
60
|
*/
|
|
61
61
|
zodShape: ZodShapeFromEnv<T>;
|
|
62
62
|
/**
|
|
63
|
-
*
|
|
63
|
+
* The Zod schema of the environment schema.
|
|
64
64
|
*/
|
|
65
65
|
zodSchema: z.ZodObject<ZodShapeFromEnv<T>>;
|
|
66
66
|
/**
|
|
67
|
-
*
|
|
67
|
+
* Checks whether the environment variables are valid without throwing.
|
|
68
68
|
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
69
|
+
* If `options.source` is provided, it is used with strict mode (zod.strict()).
|
|
70
|
+
* Otherwise, `process.env` is used in non-strict mode.
|
|
71
71
|
*
|
|
72
|
-
* @param options -
|
|
73
|
-
* @returns true
|
|
72
|
+
* @param options - Options to check the environment variables.
|
|
73
|
+
* @returns true if the environment variables are valid, false otherwise.
|
|
74
74
|
*/
|
|
75
75
|
check: CheckFn;
|
|
76
76
|
/**
|
|
77
|
-
*
|
|
77
|
+
* Validates the environment variables and throws if they do not conform to the schema.
|
|
78
78
|
*
|
|
79
|
-
*
|
|
80
|
-
*
|
|
79
|
+
* If `options.source` is provided, it is used with strict mode (zod.strict()).
|
|
80
|
+
* Otherwise, `process.env` is used in non-strict mode.
|
|
81
81
|
*
|
|
82
|
-
* @param options -
|
|
83
|
-
* @returns
|
|
84
|
-
* @throws
|
|
82
|
+
* @param options - Options to load the environment variables.
|
|
83
|
+
* @returns The loaded environment variables.
|
|
84
|
+
* @throws If the environment variables are invalid.
|
|
85
85
|
*/
|
|
86
86
|
assert: AssertFn<T>;
|
|
87
87
|
};
|
|
88
88
|
/**
|
|
89
|
-
*
|
|
90
|
-
* @param def -
|
|
91
|
-
* @returns
|
|
89
|
+
* Defines an environment schema.
|
|
90
|
+
* @param def - The environment schema to define.
|
|
91
|
+
* @returns An object exposing functions to check and assert environment variables.
|
|
92
92
|
*/
|
|
93
93
|
export declare const define: <T extends EnvDefinition>(def: T) => EnvDefinitionHelper<T>;
|
|
94
94
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
95
|
+
* Reads a .env file and returns environment variables as an object.
|
|
96
|
+
* Uses dotenv and dotenv-expand.
|
|
97
97
|
* @example
|
|
98
98
|
* ```typescript
|
|
99
99
|
* const ENV = envDefinition.load({ source: readEnvFile(".env") });
|
|
100
100
|
* ```
|
|
101
|
-
* @param path -
|
|
102
|
-
* @returns
|
|
103
|
-
* @throws
|
|
101
|
+
* @param path - The path to the .env file.
|
|
102
|
+
* @returns The environment variables as an object.
|
|
103
|
+
* @throws If the .env file does not exist, or is not valid.
|
|
104
104
|
*/
|
|
105
105
|
export declare const readEnvFile: (path: string) => EnvSource;
|
|
106
106
|
export {};
|
package/dist/core.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
import dotenv from "dotenv";
|
|
3
3
|
import dotenvExpand from "dotenv-expand";
|
|
4
|
-
import {
|
|
4
|
+
import { DnlError, ExitCodes } from "./errors.js";
|
|
5
5
|
import fs from "fs";
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
* @param def -
|
|
9
|
-
* @returns
|
|
7
|
+
* Defines an environment schema.
|
|
8
|
+
* @param def - The environment schema to define.
|
|
9
|
+
* @returns An object exposing functions to check and assert environment variables.
|
|
10
10
|
*/
|
|
11
11
|
export const define = (def) => {
|
|
12
12
|
const zodShape = Object.fromEntries(Object.entries(def).map(([key, value]) => [key, value.schema]));
|
|
@@ -29,19 +29,19 @@ export const define = (def) => {
|
|
|
29
29
|
return { def, zodShape, zodSchema, check, assert };
|
|
30
30
|
};
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
32
|
+
* Reads a .env file and returns environment variables as an object.
|
|
33
|
+
* Uses dotenv and dotenv-expand.
|
|
34
34
|
* @example
|
|
35
35
|
* ```typescript
|
|
36
36
|
* const ENV = envDefinition.load({ source: readEnvFile(".env") });
|
|
37
37
|
* ```
|
|
38
|
-
* @param path -
|
|
39
|
-
* @returns
|
|
40
|
-
* @throws
|
|
38
|
+
* @param path - The path to the .env file.
|
|
39
|
+
* @returns The environment variables as an object.
|
|
40
|
+
* @throws If the .env file does not exist, or is not valid.
|
|
41
41
|
*/
|
|
42
42
|
export const readEnvFile = (path) => {
|
|
43
43
|
if (!fs.existsSync(path)) {
|
|
44
|
-
throw new
|
|
44
|
+
throw new DnlError(`Env file not found: ${path}`, ExitCodes.usageError);
|
|
45
45
|
}
|
|
46
46
|
const content = fs.readFileSync(path);
|
|
47
47
|
const parsed = dotenv.parse(content);
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,4 +1,31 @@
|
|
|
1
|
-
export declare
|
|
2
|
-
|
|
1
|
+
export declare enum ExitCodes {
|
|
2
|
+
success = 0,
|
|
3
|
+
usageError = 1,
|
|
4
|
+
schemaNotFound = 2,
|
|
5
|
+
validationError = 3,
|
|
6
|
+
exportError = 4
|
|
7
|
+
}
|
|
8
|
+
export declare class DnlError extends Error {
|
|
9
|
+
readonly exitCode: ExitCodes;
|
|
10
|
+
constructor(message: string, exitCode: ExitCodes);
|
|
11
|
+
}
|
|
12
|
+
export declare class UsageError extends DnlError {
|
|
13
|
+
constructor(message: string);
|
|
14
|
+
}
|
|
15
|
+
export declare class SchemaNotFoundError extends DnlError {
|
|
16
|
+
constructor(message: string);
|
|
17
|
+
}
|
|
18
|
+
export declare class ValidationError extends DnlError {
|
|
19
|
+
readonly issues?: {
|
|
20
|
+
key: string;
|
|
21
|
+
message: string;
|
|
22
|
+
}[] | undefined;
|
|
23
|
+
constructor(message: string, issues?: {
|
|
24
|
+
key: string;
|
|
25
|
+
message: string;
|
|
26
|
+
}[] | undefined);
|
|
27
|
+
}
|
|
28
|
+
export declare class ExportError extends DnlError {
|
|
29
|
+
constructor(message: string);
|
|
3
30
|
}
|
|
4
31
|
//# sourceMappingURL=errors.d.ts.map
|
package/dist/errors.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,qBAAa,
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,oBAAY,SAAS;IACjB,OAAO,IAAI;IACX,UAAU,IAAI;IACd,cAAc,IAAI;IAClB,eAAe,IAAI;IACnB,WAAW,IAAI;CAClB;AAED,qBAAa,QAAS,SAAQ,KAAK;aAGX,QAAQ,EAAE,SAAS;gBADnC,OAAO,EAAE,MAAM,EACC,QAAQ,EAAE,SAAS;CAI1C;AACD,qBAAa,UAAW,SAAQ,QAAQ;gBACxB,OAAO,EAAE,MAAM;CAG9B;AACD,qBAAa,mBAAoB,SAAQ,QAAQ;gBACjC,OAAO,EAAE,MAAM;CAG9B;AAED,qBAAa,eAAgB,SAAQ,QAAQ;aAGrB,MAAM,CAAC,EAAE;QACrB,GAAG,EAAE,MAAM,CAAC;QACZ,OAAO,EAAE,MAAM,CAAC;KACnB,EAAE;gBAJH,OAAO,EAAE,MAAM,EACC,MAAM,CAAC,EAAE;QACrB,GAAG,EAAE,MAAM,CAAC;QACZ,OAAO,EAAE,MAAM,CAAC;KACnB,EAAE,YAAA;CAIV;AAED,qBAAa,WAAY,SAAQ,QAAQ;gBACzB,OAAO,EAAE,MAAM;CAG9B"}
|
package/dist/errors.js
CHANGED
|
@@ -1,6 +1,35 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export var ExitCodes;
|
|
2
|
+
(function (ExitCodes) {
|
|
3
|
+
ExitCodes[ExitCodes["success"] = 0] = "success";
|
|
4
|
+
ExitCodes[ExitCodes["usageError"] = 1] = "usageError";
|
|
5
|
+
ExitCodes[ExitCodes["schemaNotFound"] = 2] = "schemaNotFound";
|
|
6
|
+
ExitCodes[ExitCodes["validationError"] = 3] = "validationError";
|
|
7
|
+
ExitCodes[ExitCodes["exportError"] = 4] = "exportError";
|
|
8
|
+
})(ExitCodes || (ExitCodes = {}));
|
|
9
|
+
export class DnlError extends Error {
|
|
10
|
+
constructor(message, exitCode) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.exitCode = exitCode;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export class UsageError extends DnlError {
|
|
16
|
+
constructor(message) {
|
|
17
|
+
super(message, ExitCodes.usageError);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class SchemaNotFoundError extends DnlError {
|
|
21
|
+
constructor(message) {
|
|
22
|
+
super(message, ExitCodes.schemaNotFound);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export class ValidationError extends DnlError {
|
|
26
|
+
constructor(message, issues) {
|
|
27
|
+
super(message, ExitCodes.validationError);
|
|
28
|
+
this.issues = issues;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export class ExportError extends DnlError {
|
|
32
|
+
constructor(message) {
|
|
33
|
+
super(message, ExitCodes.exportError);
|
|
5
34
|
}
|
|
6
35
|
}
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@romaintaillandier1978/dotenv-never-lies",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Typed, validated, and explicit environment variables — powered by Zod.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Romain TAILLANDIER",
|
|
@@ -9,6 +9,14 @@
|
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/romaintaillandier1978/dotenv-never-lies"
|
|
11
11
|
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "tsc",
|
|
14
|
+
"pack": "rm -rf dist && yarn build && npm pack --pack-destination ./dist",
|
|
15
|
+
"pack:check": "npm pack --dry-run",
|
|
16
|
+
"release:patch": "npm version patch",
|
|
17
|
+
"release:minor": "npm version minor",
|
|
18
|
+
"release:major": "npm version major"
|
|
19
|
+
},
|
|
12
20
|
"keywords": [
|
|
13
21
|
"env",
|
|
14
22
|
"environment",
|
|
@@ -42,14 +50,6 @@
|
|
|
42
50
|
"bin": {
|
|
43
51
|
"dnl": "./dist/cli/index.js"
|
|
44
52
|
},
|
|
45
|
-
"scripts": {
|
|
46
|
-
"build": "tsc",
|
|
47
|
-
"prepublishOnly": "npm test && npm run build",
|
|
48
|
-
"test": "yarn build && node dist/DnlTest.js",
|
|
49
|
-
"dev": "tsx src/cli/index.ts",
|
|
50
|
-
"devold": "yarn build && node dist/cli/index.js",
|
|
51
|
-
"gen": "rm -rf dist; rm dotenv-never-lies-v*.tgz; yarn build && yarn pack"
|
|
52
|
-
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"zod": ">=4.2.1"
|
|
55
55
|
},
|