@e-n-v/env 0.0.2 → 0.0.4
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 +43 -24
- package/dist/BooleanMap-B2eIP3tZ.d.cts +32 -0
- package/dist/BooleanMap-DmFnCA-O.d.ts +32 -0
- package/dist/EnvChannel-LMIRYvT1.d.cts +16 -0
- package/dist/EnvChannel-w27PEm7x.d.ts +16 -0
- package/dist/EnvChannelOptions-BPkWDrmr.d.cts +108 -0
- package/dist/EnvChannelOptions-CRwDLWjd.d.ts +108 -0
- package/dist/EnvModel-CrGclUpJ.d.cts +134 -0
- package/dist/EnvModel-D8rt0EJ6.d.ts +134 -0
- package/dist/EnvSource-BVnmdHku.cjs +420 -0
- package/dist/EnvSource-DA3ZGlES.js +400 -0
- package/dist/ProcessEnvChannel-D465c-dp.d.cts +97 -0
- package/dist/ProcessEnvChannel-Do4-yiLI.d.ts +97 -0
- package/dist/StringEnvVarSchema-B8MT7htU.d.ts +64 -0
- package/dist/StringEnvVarSchema-BF6C0nyT.d.cts +64 -0
- package/dist/channels.cjs +8 -0
- package/dist/channels.d.cts +20 -0
- package/dist/channels.d.ts +20 -0
- package/dist/channels.js +4 -0
- package/dist/chunk-pbleEpgJ.cjs +44 -0
- package/dist/cli.cjs +16 -10
- package/dist/cli.js +7 -2
- package/dist/coerce-DNIuYAx4.js +11028 -0
- package/dist/coerce-b7UVodS9.cjs +12365 -0
- package/dist/core.cjs +68 -0
- package/dist/core.d.cts +6 -0
- package/dist/core.d.ts +6 -0
- package/dist/core.js +40 -0
- package/dist/custom-Bt_wnxIu.js +114 -0
- package/dist/custom-D_Rqjm09.cjs +168 -0
- package/dist/files.cjs +18 -0
- package/dist/files.d.cts +38 -0
- package/dist/files.d.ts +38 -0
- package/dist/files.js +13 -0
- package/dist/index-D8pslhp4.d.cts +170 -0
- package/dist/index-Dq_0aeAL.d.ts +170 -0
- package/dist/model-Dic8IJsf.d.ts +29 -0
- package/dist/model-ft0WhSSS.d.cts +29 -0
- package/dist/model.cjs +14 -0
- package/dist/model.d.cts +35 -0
- package/dist/model.d.ts +35 -0
- package/dist/model.js +9 -0
- package/dist/parse-BT1QSa5e.d.cts +28 -0
- package/dist/parse-DwtLpVhC.d.ts +28 -0
- package/dist/parse.cjs +17 -0
- package/dist/parse.d.cts +52 -0
- package/dist/parse.d.ts +52 -0
- package/dist/parse.js +10 -0
- package/dist/prompt-C5WudV9x.d.cts +74 -0
- package/dist/prompt-yKdccbtZ.d.ts +74 -0
- package/dist/prompt.cjs +24 -0
- package/dist/prompt.d.cts +17 -0
- package/dist/prompt.d.ts +17 -0
- package/dist/prompt.js +12 -0
- package/dist/redis-CJOpRPjc.js +395 -0
- package/dist/redis-EcC7TX3K.cjs +611 -0
- package/dist/resolveChannel-CjAuxy3p.cjs +257 -0
- package/dist/resolveChannel-v9BDsw-M.js +224 -0
- package/dist/root.cjs +36 -0
- package/dist/root.d.cts +21 -0
- package/dist/root.d.ts +21 -0
- package/dist/root.js +21 -0
- package/dist/schema-DH4GiW9A.d.cts +18 -0
- package/dist/schema-jN4EtvEP.d.ts +18 -0
- package/dist/schemas/zod.cjs +629 -0
- package/dist/schemas/zod.d.cts +239 -0
- package/dist/schemas/zod.d.ts +239 -0
- package/dist/schemas/zod.js +550 -0
- package/dist/schemas.cjs +133 -0
- package/dist/schemas.d.cts +3 -0
- package/dist/schemas.d.ts +3 -0
- package/dist/schemas.js +6 -0
- package/dist/src-B7FG1rak.js +638 -0
- package/dist/src-BBwjK5SK.js +127 -0
- package/dist/src-CLTURaoo.js +311 -0
- package/dist/src-CVdUqK3q.js +850 -0
- package/dist/src-Cjo2XA4n.cjs +138 -0
- package/dist/{src-oetJm-nZ.cjs → src-DWYoqPX1.cjs} +31 -1964
- package/dist/src-DZmpNGrQ.cjs +1411 -0
- package/dist/{src-RlCvrI0s.js → src-Dtv7TlJx.js} +14 -1726
- package/dist/src-XjL5cyPG.cjs +873 -0
- package/dist/src-meUnCASg.cjs +413 -0
- package/dist/validate-BxA3a-ns.d.cts +203 -0
- package/dist/validate-CyxrRacF.d.ts +203 -0
- package/dist/vars-BNVI6k89.d.ts +21 -0
- package/dist/vars-CYOaskkK.cjs +27 -0
- package/dist/vars-CptZRPX5.d.cts +21 -0
- package/dist/vars-DOW0LiiN.js +22 -0
- package/dist/vars.cjs +138 -0
- package/dist/vars.d.cts +7 -0
- package/dist/vars.d.ts +7 -0
- package/dist/vars.js +9 -0
- package/package.json +57 -9
- package/assets/demo.gif +0 -0
- package/assets/env-logo.png +0 -0
- package/dist/bundle.cjs +0 -1290
- package/dist/bundle.d.cts +0 -1057
- package/dist/bundle.d.ts +0 -1055
- package/dist/bundle.js +0 -1244
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { a as EnumEnvVarSchema, l as EnvVarSchemaBase, r as NumberEnvVarSchema, s as BooleanEnvVarSchema, t as StringEnvVarSchema } from "./StringEnvVarSchema-B8MT7htU.js";
|
|
2
|
+
import { i as EnvVarSchema, n as Preprocessor, t as BooleanMap } from "./BooleanMap-DmFnCA-O.js";
|
|
3
|
+
import { AnySchema } from "joi";
|
|
4
|
+
import { ZodTypeAny } from "zod";
|
|
5
|
+
import { $ZodType } from "zod/v4/core";
|
|
6
|
+
import { Struct } from "superstruct";
|
|
7
|
+
|
|
8
|
+
//#region ../models/src/schemas.d.ts
|
|
9
|
+
type JoiSchema = AnySchema;
|
|
10
|
+
type ZodV3Schema = ZodTypeAny;
|
|
11
|
+
type ZodV4Schema = $ZodType;
|
|
12
|
+
type SuperstructSchema = Struct<any, any>;
|
|
13
|
+
type Schema = ZodV3Schema | ZodV4Schema | JoiSchema | SuperstructSchema | EnvVarSchema;
|
|
14
|
+
//#endregion
|
|
15
|
+
//#region ../models/src/types.d.ts
|
|
16
|
+
/**
|
|
17
|
+
* Supported schema types - local definition to avoid circular dependencies
|
|
18
|
+
*/
|
|
19
|
+
type SupportedSchema = Schema;
|
|
20
|
+
type InferZodOutput<T> = T extends {
|
|
21
|
+
_output: infer Output;
|
|
22
|
+
} ? Output : T extends {
|
|
23
|
+
_type: infer LegacyOutput;
|
|
24
|
+
} ? LegacyOutput : never;
|
|
25
|
+
type InferJoiOutput<T> = T extends {
|
|
26
|
+
validate: (...args: any[]) => infer Result;
|
|
27
|
+
} ? Result extends {
|
|
28
|
+
value: infer Value;
|
|
29
|
+
} ? Value : Result extends Promise<infer AsyncResult> ? AsyncResult extends {
|
|
30
|
+
value: infer AsyncValue;
|
|
31
|
+
} ? AsyncValue : unknown : unknown : T extends {
|
|
32
|
+
validateAsync: (...args: any[]) => Promise<infer AsyncValue>;
|
|
33
|
+
} ? AsyncValue : unknown;
|
|
34
|
+
/**
|
|
35
|
+
* Infer the TypeScript type from an EnvVarSchema instance
|
|
36
|
+
* Uses the schema's type property and values to determine the correct TypeScript type
|
|
37
|
+
*/
|
|
38
|
+
type InferSchemaType<T> = T extends {
|
|
39
|
+
readonly type: "string";
|
|
40
|
+
} ? string : T extends {
|
|
41
|
+
readonly type: "number";
|
|
42
|
+
} ? number : T extends {
|
|
43
|
+
readonly type: "boolean";
|
|
44
|
+
} ? boolean : T extends {
|
|
45
|
+
readonly type: "enum";
|
|
46
|
+
readonly values: readonly (infer U)[];
|
|
47
|
+
} ? U : T extends EnumEnvVarSchema<infer U> ? U : T extends NumberEnvVarSchema ? number : T extends StringEnvVarSchema ? string : T extends BooleanEnvVarSchema ? boolean : T extends EnvVarSchemaBase<infer U> ? U : T extends AnySchema ? InferJoiOutput<T> : T extends ZodTypeAny ? InferZodOutput<T> : T extends $ZodType ? InferZodOutput<T> : unknown;
|
|
48
|
+
/**
|
|
49
|
+
* Infer whether a schema field is optional based on its required property
|
|
50
|
+
*/
|
|
51
|
+
type InferOptional<T> = T extends {
|
|
52
|
+
required: false;
|
|
53
|
+
} ? true : T extends {
|
|
54
|
+
required: true;
|
|
55
|
+
} ? false : T extends {
|
|
56
|
+
required: boolean;
|
|
57
|
+
} ? boolean : false;
|
|
58
|
+
/**
|
|
59
|
+
* Utility type to flatten complex generic types for better IDE display
|
|
60
|
+
*/
|
|
61
|
+
type Simplify<T> = { [K in keyof T]: T[K] } & {};
|
|
62
|
+
/**
|
|
63
|
+
* Infer the complete environment type from a schemas record
|
|
64
|
+
* Handles optional vs required fields properly
|
|
65
|
+
* Uses Simplify to flatten the type display in IDEs
|
|
66
|
+
*/
|
|
67
|
+
type InferEnvType<T extends Record<string, SupportedSchema>> = Simplify<{ [K in keyof T]: InferOptional<T[K]> extends true ? InferSchemaType<T[K]> | undefined : InferSchemaType<T[K]> }>;
|
|
68
|
+
//#endregion
|
|
69
|
+
//#region ../models/src/EnvModelOptions.d.ts
|
|
70
|
+
type PreprocessorToggle<T> = Preprocessor<T> | boolean | undefined;
|
|
71
|
+
interface Preprocessors {
|
|
72
|
+
/**
|
|
73
|
+
* Custom string preprocessing function
|
|
74
|
+
*/
|
|
75
|
+
string?: PreprocessorToggle<string>;
|
|
76
|
+
/**
|
|
77
|
+
* Custom number preprocessing function
|
|
78
|
+
*/
|
|
79
|
+
number?: PreprocessorToggle<number>;
|
|
80
|
+
/**
|
|
81
|
+
* Custom boolean preprocessing function
|
|
82
|
+
*/
|
|
83
|
+
boolean?: boolean | Preprocessor<boolean> | BooleanMap | undefined;
|
|
84
|
+
/**
|
|
85
|
+
* Custom enum preprocessing function
|
|
86
|
+
*/
|
|
87
|
+
enum?: PreprocessorToggle<string>;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Options for creating an EnvModel instance
|
|
91
|
+
*/
|
|
92
|
+
interface EnvModelOptions<T extends Record<string, SupportedSchema> = Record<string, SupportedSchema>> {
|
|
93
|
+
/**
|
|
94
|
+
* Environment variable schemas
|
|
95
|
+
* Map of variable names to their schema definitions
|
|
96
|
+
*/
|
|
97
|
+
schemas: T;
|
|
98
|
+
/**
|
|
99
|
+
* Preprocessing configuration
|
|
100
|
+
* - `true`: Use default preprocessors for all types
|
|
101
|
+
* - `false`: Disable all preprocessing
|
|
102
|
+
* - Partial object: Customize specific type preprocessors
|
|
103
|
+
*/
|
|
104
|
+
preprocess?: true | false | Partial<Preprocessors>;
|
|
105
|
+
}
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region ../models/src/EnvModel.d.ts
|
|
108
|
+
/**
|
|
109
|
+
* Environment variable specification container
|
|
110
|
+
* Encapsulates schemas and preprocessor configuration
|
|
111
|
+
*/
|
|
112
|
+
declare class EnvModel<T extends Record<string, SupportedSchema> = Record<string, SupportedSchema>> {
|
|
113
|
+
/**
|
|
114
|
+
* Resolved environment variable schemas
|
|
115
|
+
*/
|
|
116
|
+
readonly schemas: Record<string, EnvVarSchema>;
|
|
117
|
+
/**
|
|
118
|
+
* Preprocessing configuration for environment variable types
|
|
119
|
+
*/
|
|
120
|
+
readonly preprocess: Preprocessors;
|
|
121
|
+
/**
|
|
122
|
+
* Create an EnvModel instance from options
|
|
123
|
+
* @param options - Specification options
|
|
124
|
+
*/
|
|
125
|
+
constructor(options: EnvModelOptions<T>);
|
|
126
|
+
/**
|
|
127
|
+
* Resolve preprocessor configuration from options
|
|
128
|
+
* @param config - Preprocessor configuration (true, false, or partial object)
|
|
129
|
+
* @returns Resolved preprocessors object
|
|
130
|
+
*/
|
|
131
|
+
private resolvePreprocessors;
|
|
132
|
+
}
|
|
133
|
+
//#endregion
|
|
134
|
+
export { SupportedSchema as a, InferSchemaType as i, EnvModelOptions as n, Schema as o, InferEnvType as r, EnvModel as t };
|
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-pbleEpgJ.cjs');
|
|
2
|
+
let node_fs = require("node:fs");
|
|
3
|
+
node_fs = require_chunk.__toESM(node_fs);
|
|
4
|
+
let node_path = require("node:path");
|
|
5
|
+
node_path = require_chunk.__toESM(node_path);
|
|
6
|
+
|
|
7
|
+
//#region ../files/src/utils.ts
|
|
8
|
+
function normalizeContent(content) {
|
|
9
|
+
return content.replace(/\r\n/g, "\n");
|
|
10
|
+
}
|
|
11
|
+
function splitLines(content) {
|
|
12
|
+
return content === "" ? [] : content.split("\n");
|
|
13
|
+
}
|
|
14
|
+
function validateKey(key) {
|
|
15
|
+
const trimmed = key.trim();
|
|
16
|
+
if (!trimmed) throw new TypeError("Environment variable name cannot be empty");
|
|
17
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(trimmed)) throw new TypeError(`Invalid environment variable name: ${trimmed}`);
|
|
18
|
+
return trimmed;
|
|
19
|
+
}
|
|
20
|
+
function parseAssignmentEndingAt(lines, endIndex) {
|
|
21
|
+
const lastLine = lines[endIndex];
|
|
22
|
+
if (typeof lastLine !== "string" || isSkippableLine(lastLine)) return null;
|
|
23
|
+
const block = [];
|
|
24
|
+
let start = endIndex;
|
|
25
|
+
while (start >= 0) {
|
|
26
|
+
const currentLine = lines[start];
|
|
27
|
+
if (typeof currentLine !== "string") break;
|
|
28
|
+
block.unshift(currentLine);
|
|
29
|
+
if (containsAssignmentOperator(currentLine)) {
|
|
30
|
+
const parsed = tryParseAssignmentBlock(block);
|
|
31
|
+
if (!parsed) return null;
|
|
32
|
+
return {
|
|
33
|
+
...parsed,
|
|
34
|
+
startIndex: start,
|
|
35
|
+
endIndex
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
start -= 1;
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
function isSkippableLine(line) {
|
|
43
|
+
const trimmed = line.trim();
|
|
44
|
+
return trimmed.length === 0 || trimmed.startsWith("#");
|
|
45
|
+
}
|
|
46
|
+
function containsAssignmentOperator(line) {
|
|
47
|
+
const trimmed = line.trimStart();
|
|
48
|
+
if (trimmed.length === 0 || trimmed.startsWith("#")) return false;
|
|
49
|
+
const commentIndex = trimmed.indexOf("#");
|
|
50
|
+
return (commentIndex === -1 ? trimmed : trimmed.slice(0, commentIndex)).includes("=");
|
|
51
|
+
}
|
|
52
|
+
function tryParseAssignmentBlock(blockLines) {
|
|
53
|
+
if (blockLines.length === 0) return null;
|
|
54
|
+
const firstLine = blockLines[0];
|
|
55
|
+
const leadingMatch = firstLine.match(/^\s*/);
|
|
56
|
+
const leading = leadingMatch ? leadingMatch[0] : "";
|
|
57
|
+
let remainder = firstLine.slice(leading.length);
|
|
58
|
+
if (remainder.length === 0 || remainder.startsWith("#")) return null;
|
|
59
|
+
let exportPrefix = "";
|
|
60
|
+
if (remainder.startsWith("export")) {
|
|
61
|
+
const afterExport = remainder.slice(6);
|
|
62
|
+
if (/^\s/.test(afterExport)) {
|
|
63
|
+
const spacingMatch = afterExport.match(/^\s+/);
|
|
64
|
+
const spacing = spacingMatch ? spacingMatch[0] : " ";
|
|
65
|
+
exportPrefix = `export${spacing}`;
|
|
66
|
+
remainder = afterExport.slice(spacing.length);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const equalsIndex = remainder.indexOf("=");
|
|
70
|
+
if (equalsIndex === -1) return null;
|
|
71
|
+
const key = validateKey(remainder.slice(0, equalsIndex).trim());
|
|
72
|
+
const parsedValue = parseValueSource([remainder.slice(equalsIndex + 1).replace(/^\s+/, ""), ...blockLines.slice(1)].join("\n"));
|
|
73
|
+
if (!parsedValue) return null;
|
|
74
|
+
const trailingTrimmed = parsedValue.trailing.trim();
|
|
75
|
+
if (trailingTrimmed.length > 0 && !trailingTrimmed.startsWith("#")) return null;
|
|
76
|
+
return {
|
|
77
|
+
key,
|
|
78
|
+
value: parsedValue.value,
|
|
79
|
+
leading,
|
|
80
|
+
exportPrefix,
|
|
81
|
+
trailing: parsedValue.trailing,
|
|
82
|
+
lines: [...blockLines]
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
function parseValueSource(source) {
|
|
86
|
+
if (source.length === 0) return {
|
|
87
|
+
value: "",
|
|
88
|
+
trailing: ""
|
|
89
|
+
};
|
|
90
|
+
const firstChar = source[0];
|
|
91
|
+
if (firstChar === "\"") return parseDoubleQuotedValue(source);
|
|
92
|
+
if (firstChar === "'") return parseSingleQuotedValue(source);
|
|
93
|
+
if (source.includes("\n")) return null;
|
|
94
|
+
const commentIndex = source.indexOf(" #");
|
|
95
|
+
const raw = commentIndex === -1 ? source : source.slice(0, commentIndex);
|
|
96
|
+
const trailing = commentIndex === -1 ? source.slice(raw.length) : source.slice(commentIndex);
|
|
97
|
+
return {
|
|
98
|
+
value: raw.trim(),
|
|
99
|
+
trailing
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
function parseDoubleQuotedValue(source) {
|
|
103
|
+
let value = "";
|
|
104
|
+
let escaping = false;
|
|
105
|
+
for (let i = 1; i < source.length; i += 1) {
|
|
106
|
+
const char = source[i] ?? "";
|
|
107
|
+
if (escaping) {
|
|
108
|
+
switch (char) {
|
|
109
|
+
case "n":
|
|
110
|
+
value += "\n";
|
|
111
|
+
break;
|
|
112
|
+
case "r":
|
|
113
|
+
value += "\r";
|
|
114
|
+
break;
|
|
115
|
+
case "t":
|
|
116
|
+
value += " ";
|
|
117
|
+
break;
|
|
118
|
+
case "\"":
|
|
119
|
+
value += "\"";
|
|
120
|
+
break;
|
|
121
|
+
case "\\":
|
|
122
|
+
value += "\\";
|
|
123
|
+
break;
|
|
124
|
+
default:
|
|
125
|
+
value += char;
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
escaping = false;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (char === "\\") {
|
|
132
|
+
escaping = true;
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
if (char === "\"") {
|
|
136
|
+
const trailing = source.slice(i + 1);
|
|
137
|
+
return {
|
|
138
|
+
value,
|
|
139
|
+
trailing
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (char === "\n") {
|
|
143
|
+
value += "\n";
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
value += char;
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
function parseSingleQuotedValue(source) {
|
|
151
|
+
let value = "";
|
|
152
|
+
for (let i = 1; i < source.length; i += 1) {
|
|
153
|
+
const char = source[i] ?? "";
|
|
154
|
+
if (char === "'") {
|
|
155
|
+
const trailing = source.slice(i + 1);
|
|
156
|
+
return {
|
|
157
|
+
value,
|
|
158
|
+
trailing
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
if (char === "\n") {
|
|
162
|
+
value += "\n";
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
value += char;
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region ../files/src/get.ts
|
|
172
|
+
function get(content, arg) {
|
|
173
|
+
const lines = splitLines(normalizeContent(content));
|
|
174
|
+
if (typeof arg === "undefined") {
|
|
175
|
+
const map = scanAll(lines);
|
|
176
|
+
return Object.fromEntries(map);
|
|
177
|
+
}
|
|
178
|
+
if (typeof arg === "string") return scanForSingle(lines, arg);
|
|
179
|
+
const normalizedTargets = Array.from(new Set(arg.map((name) => validateKey(name))));
|
|
180
|
+
const lookup = scanForMany(lines, new Set(normalizedTargets));
|
|
181
|
+
const ordered = normalizedTargets.map((key) => [key, lookup.get(key)]);
|
|
182
|
+
return Object.fromEntries(ordered);
|
|
183
|
+
}
|
|
184
|
+
function scanAll(lines) {
|
|
185
|
+
const result = /* @__PURE__ */ new Map();
|
|
186
|
+
iterateAssignments(lines, (assignment) => {
|
|
187
|
+
if (!result.has(assignment.key)) result.set(assignment.key, assignment.value);
|
|
188
|
+
});
|
|
189
|
+
return result;
|
|
190
|
+
}
|
|
191
|
+
function scanForSingle(lines, target) {
|
|
192
|
+
const name = validateKey(target);
|
|
193
|
+
let found;
|
|
194
|
+
iterateAssignments(lines, (assignment) => {
|
|
195
|
+
if (assignment.key === name) {
|
|
196
|
+
found = assignment.value;
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
return found;
|
|
201
|
+
}
|
|
202
|
+
function scanForMany(lines, targets) {
|
|
203
|
+
const normalizedTargets = /* @__PURE__ */ new Set();
|
|
204
|
+
for (const target of targets) normalizedTargets.add(validateKey(target));
|
|
205
|
+
const found = /* @__PURE__ */ new Map();
|
|
206
|
+
const remaining = new Set(normalizedTargets);
|
|
207
|
+
iterateAssignments(lines, (assignment) => {
|
|
208
|
+
if (!remaining.has(assignment.key) || found.has(assignment.key)) return;
|
|
209
|
+
found.set(assignment.key, assignment.value);
|
|
210
|
+
remaining.delete(assignment.key);
|
|
211
|
+
if (remaining.size === 0) return true;
|
|
212
|
+
});
|
|
213
|
+
return found;
|
|
214
|
+
}
|
|
215
|
+
function iterateAssignments(lines, callback) {
|
|
216
|
+
for (let i = lines.length - 1; i >= 0;) {
|
|
217
|
+
const parsed = parseAssignmentEndingAt(lines, i);
|
|
218
|
+
if (!parsed) {
|
|
219
|
+
i -= 1;
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (callback(parsed) === true) return;
|
|
223
|
+
i = parsed.startIndex - 1;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
//#endregion
|
|
228
|
+
//#region ../files/src/set.ts
|
|
229
|
+
function set(content, arg, value) {
|
|
230
|
+
const normalized = normalizeContent(content);
|
|
231
|
+
const lines = splitLines(normalized);
|
|
232
|
+
const entries = normalizeWriteArguments(arg, value);
|
|
233
|
+
if (entries.length === 0) return ensureTrailingNewline(normalized);
|
|
234
|
+
const replacements = new Map(entries);
|
|
235
|
+
const touched = /* @__PURE__ */ new Set();
|
|
236
|
+
for (let i = lines.length - 1; i >= 0;) {
|
|
237
|
+
if (replacements.size === 0) break;
|
|
238
|
+
const parsed = parseAssignmentEndingAt(lines, i);
|
|
239
|
+
if (!parsed) {
|
|
240
|
+
i -= 1;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
const { key, startIndex, endIndex, leading, exportPrefix, trailing, value: currentValue } = parsed;
|
|
244
|
+
const nextValue = replacements.get(key);
|
|
245
|
+
if (typeof nextValue === "undefined" || touched.has(key)) {
|
|
246
|
+
i = startIndex - 1;
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (nextValue === currentValue) {
|
|
250
|
+
touched.add(key);
|
|
251
|
+
replacements.delete(key);
|
|
252
|
+
i = startIndex - 1;
|
|
253
|
+
continue;
|
|
254
|
+
}
|
|
255
|
+
const block = buildAssignmentLines(key, nextValue, {
|
|
256
|
+
leading,
|
|
257
|
+
exportPrefix,
|
|
258
|
+
trailing
|
|
259
|
+
});
|
|
260
|
+
lines.splice(startIndex, endIndex - startIndex + 1, ...block);
|
|
261
|
+
touched.add(key);
|
|
262
|
+
replacements.delete(key);
|
|
263
|
+
i = startIndex - 1;
|
|
264
|
+
}
|
|
265
|
+
for (const [key, val] of entries) {
|
|
266
|
+
if (touched.has(key)) continue;
|
|
267
|
+
const block = buildAssignmentLines(key, val);
|
|
268
|
+
const insertIndex = lines.length > 0 && lines[lines.length - 1] === "" ? lines.length - 1 : lines.length;
|
|
269
|
+
lines.splice(insertIndex, 0, ...block);
|
|
270
|
+
touched.add(key);
|
|
271
|
+
}
|
|
272
|
+
return ensureTrailingNewline(lines.join("\n"));
|
|
273
|
+
}
|
|
274
|
+
function ensureTrailingNewline(content) {
|
|
275
|
+
if (content.length === 0) return "";
|
|
276
|
+
if (!content.endsWith("\n")) return `${content}\n`;
|
|
277
|
+
return content;
|
|
278
|
+
}
|
|
279
|
+
function isObjectRecord(value) {
|
|
280
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
281
|
+
}
|
|
282
|
+
function normalizeWriteArguments(arg, value) {
|
|
283
|
+
if (typeof arg === "string") {
|
|
284
|
+
if (typeof value === "undefined") throw new TypeError(`Value for variable "${arg}" must be provided`);
|
|
285
|
+
return [[validateKey(arg), formatValueInput(value)]];
|
|
286
|
+
}
|
|
287
|
+
if (!isObjectRecord(arg)) throw new TypeError("Argument must be a string name or an object of key-value pairs");
|
|
288
|
+
const entries = [];
|
|
289
|
+
for (const [key, rawValue] of Object.entries(arg)) {
|
|
290
|
+
if (typeof rawValue === "undefined") throw new TypeError(`Value for variable "${key}" must be provided`);
|
|
291
|
+
entries.push([validateKey(key), formatValueInput(rawValue)]);
|
|
292
|
+
}
|
|
293
|
+
return entries;
|
|
294
|
+
}
|
|
295
|
+
function formatValueInput(value) {
|
|
296
|
+
if (typeof value === "bigint") return value.toString();
|
|
297
|
+
return String(value);
|
|
298
|
+
}
|
|
299
|
+
function serializeValue(value) {
|
|
300
|
+
const normalized = value.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
301
|
+
if (!(normalized.length === 0 || /[\s"'\\#]/.test(normalized) || normalized.includes("\n"))) return normalized;
|
|
302
|
+
let result = "\"";
|
|
303
|
+
for (const char of normalized) {
|
|
304
|
+
if (char === "\"") {
|
|
305
|
+
result += "\\\"";
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (char === "\\") {
|
|
309
|
+
result += "\\\\";
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
if (char === "\n") {
|
|
313
|
+
result += "\n";
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
result += char;
|
|
317
|
+
}
|
|
318
|
+
result += "\"";
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
function buildAssignmentLines(key, value, style = {}) {
|
|
322
|
+
const leading = style.leading ?? "";
|
|
323
|
+
const exportPrefix = style.exportPrefix ?? "";
|
|
324
|
+
const trailing = style.trailing ?? "";
|
|
325
|
+
const segments = serializeValue(value).split("\n");
|
|
326
|
+
return segments.map((segment, index) => {
|
|
327
|
+
if (index === 0) return `${leading}${exportPrefix}${key}=${segment}${segments.length === 1 ? trailing : ""}`;
|
|
328
|
+
if (index === segments.length - 1 && trailing && segments.length > 1) return `${segment}${trailing}`;
|
|
329
|
+
return segment;
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
//#endregion
|
|
334
|
+
//#region ../files/src/EnvSource.ts
|
|
335
|
+
var EnvSource = class {
|
|
336
|
+
filePath;
|
|
337
|
+
constructor(filePath) {
|
|
338
|
+
this.filePath = (0, node_path.resolve)(process.cwd(), filePath);
|
|
339
|
+
}
|
|
340
|
+
readSync(arg) {
|
|
341
|
+
const content = this.readContentSync();
|
|
342
|
+
if (typeof arg === "undefined") return get(content);
|
|
343
|
+
if (typeof arg === "string") return get(content, arg);
|
|
344
|
+
return get(content, arg);
|
|
345
|
+
}
|
|
346
|
+
async read(arg) {
|
|
347
|
+
const content = await this.readContent();
|
|
348
|
+
if (typeof arg === "undefined") return get(content);
|
|
349
|
+
if (typeof arg === "string") return get(content, arg);
|
|
350
|
+
return get(content, arg);
|
|
351
|
+
}
|
|
352
|
+
writeSync(arg, value) {
|
|
353
|
+
const originalContent = this.readContentSync();
|
|
354
|
+
let nextContent;
|
|
355
|
+
if (typeof arg === "string") nextContent = set(originalContent, arg, value);
|
|
356
|
+
else nextContent = set(originalContent, arg);
|
|
357
|
+
if (nextContent === originalContent) return;
|
|
358
|
+
this.ensureDirectorySync();
|
|
359
|
+
(0, node_fs.writeFileSync)(this.filePath, nextContent, "utf8");
|
|
360
|
+
}
|
|
361
|
+
async write(arg, value) {
|
|
362
|
+
const originalContent = await this.readContent();
|
|
363
|
+
let nextContent;
|
|
364
|
+
if (typeof arg === "string") nextContent = set(originalContent, arg, value);
|
|
365
|
+
else nextContent = set(originalContent, arg);
|
|
366
|
+
if (nextContent === originalContent) return;
|
|
367
|
+
await this.ensureDirectory();
|
|
368
|
+
await node_fs.promises.writeFile(this.filePath, nextContent, "utf8");
|
|
369
|
+
}
|
|
370
|
+
readContentSync() {
|
|
371
|
+
try {
|
|
372
|
+
return (0, node_fs.readFileSync)(this.filePath, "utf8").replace(/\r\n/g, "\n");
|
|
373
|
+
} catch (error) {
|
|
374
|
+
if (error.code === "ENOENT") return "";
|
|
375
|
+
throw error;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
async readContent() {
|
|
379
|
+
try {
|
|
380
|
+
return (await node_fs.promises.readFile(this.filePath, "utf8")).replace(/\r\n/g, "\n");
|
|
381
|
+
} catch (error) {
|
|
382
|
+
if (error.code === "ENOENT") return "";
|
|
383
|
+
throw error;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
ensureDirectorySync() {
|
|
387
|
+
try {
|
|
388
|
+
(0, node_fs.mkdirSync)((0, node_path.dirname)(this.filePath), { recursive: true });
|
|
389
|
+
} catch (error) {
|
|
390
|
+
if (error.code !== "EEXIST") throw error;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
async ensureDirectory() {
|
|
394
|
+
try {
|
|
395
|
+
await node_fs.promises.mkdir((0, node_path.dirname)(this.filePath), { recursive: true });
|
|
396
|
+
} catch (error) {
|
|
397
|
+
if (error.code !== "EEXIST") throw error;
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
//#endregion
|
|
403
|
+
Object.defineProperty(exports, 'EnvSource', {
|
|
404
|
+
enumerable: true,
|
|
405
|
+
get: function () {
|
|
406
|
+
return EnvSource;
|
|
407
|
+
}
|
|
408
|
+
});
|
|
409
|
+
Object.defineProperty(exports, 'get', {
|
|
410
|
+
enumerable: true,
|
|
411
|
+
get: function () {
|
|
412
|
+
return get;
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
Object.defineProperty(exports, 'set', {
|
|
416
|
+
enumerable: true,
|
|
417
|
+
get: function () {
|
|
418
|
+
return set;
|
|
419
|
+
}
|
|
420
|
+
});
|