@romaintaillandier1978/dotenv-never-lies 0.3.0 → 0.4.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 +328 -69
- 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 +34 -0
- package/dist/cli/commands/export.d.ts.map +1 -0
- package/dist/cli/commands/export.js +263 -0
- package/dist/cli/commands/generate.d.ts +7 -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 +14 -7
- package/dist/cli/index.js +265 -78
- 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/load-schema.d.ts.map +1 -1
- package/dist/cli/utils/load-schema.js +19 -12
- 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.js +2 -2
- 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 @@
|
|
|
1
|
+
{"version":3,"file":"export.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/export.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,gBAAgB,CAAC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AAC,EAAE,SAAS,gBAAgB,EAAE,UAAU,MAAM,EAAE,KAAG,MAErH,CAAC"}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import dnl from "../../index.js";
|
|
2
|
+
import { loadDef } from "../utils/load-schema.js";
|
|
3
|
+
import { resolveSchemaPath } from "../utils/resolve-schema.js";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { UsageError } from "../../errors.js";
|
|
6
|
+
export const exportFormatsNames = [
|
|
7
|
+
"docker-env",
|
|
8
|
+
"docker-args",
|
|
9
|
+
"env",
|
|
10
|
+
"k8s-configmap",
|
|
11
|
+
"k8s-secret",
|
|
12
|
+
"github-env",
|
|
13
|
+
"github-secret",
|
|
14
|
+
"gitlab-env",
|
|
15
|
+
"json",
|
|
16
|
+
"ts",
|
|
17
|
+
"js",
|
|
18
|
+
];
|
|
19
|
+
export const exportCommand = async (options) => {
|
|
20
|
+
if (options.githubOrg && options.format !== "github-secret") {
|
|
21
|
+
throw new UsageError("--github-org ne peut être utilisé qu’avec le format github-secret");
|
|
22
|
+
}
|
|
23
|
+
if (options.k8sName && !options.format.startsWith("k8s-")) {
|
|
24
|
+
throw new UsageError("--k8s-name ne peut être utilisé qu’avec un format k8s-*");
|
|
25
|
+
}
|
|
26
|
+
const schemaPath = resolveSchemaPath(options?.schema);
|
|
27
|
+
const envDef = (await loadDef(schemaPath));
|
|
28
|
+
const warnings = [];
|
|
29
|
+
const content = contentByFormat(options.format, envDef, options, warnings);
|
|
30
|
+
return {
|
|
31
|
+
content,
|
|
32
|
+
warnings,
|
|
33
|
+
out: options.out,
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
export const contentByFormat = (format, envDef, options, warnings) => {
|
|
37
|
+
switch (format) {
|
|
38
|
+
case "k8s-configmap":
|
|
39
|
+
return exportK8sConfigmap(envDef, options, warnings);
|
|
40
|
+
case "k8s-secret":
|
|
41
|
+
return exportK8sSecret(envDef, options, warnings);
|
|
42
|
+
case "ts":
|
|
43
|
+
return exportTs(envDef, options, warnings);
|
|
44
|
+
case "js":
|
|
45
|
+
return exportJs(envDef, options, warnings);
|
|
46
|
+
case "github-env":
|
|
47
|
+
return exportGithubEnv(envDef, options, warnings);
|
|
48
|
+
case "github-secret":
|
|
49
|
+
return exportGithubSecret(envDef, options, warnings);
|
|
50
|
+
case "gitlab-env":
|
|
51
|
+
return exportGitlabEnv(envDef, options, warnings);
|
|
52
|
+
case "json":
|
|
53
|
+
return exportJson(envDef, options, warnings);
|
|
54
|
+
case "env":
|
|
55
|
+
return exportEnv(envDef, options, warnings);
|
|
56
|
+
case "docker-args":
|
|
57
|
+
return exportDockerArgs(envDef, options, warnings);
|
|
58
|
+
case "docker-env":
|
|
59
|
+
return exportDockerEnv(envDef, options, warnings);
|
|
60
|
+
default:
|
|
61
|
+
throw new UsageError(`Format ${format} non supporté`);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
export const exportJson = (envDef, options, warnings) => {
|
|
65
|
+
if (options?.includeComments) {
|
|
66
|
+
warnings.push("--include-comments option ignorée pour le format json");
|
|
67
|
+
}
|
|
68
|
+
const source = options?.source ? dnl.readEnvFile(path.resolve(process.cwd(), options.source)) : process.env;
|
|
69
|
+
const values = envDef.assert({ source });
|
|
70
|
+
const args = {};
|
|
71
|
+
for (const [key, value] of Object.entries(values)) {
|
|
72
|
+
if (options?.excludeSecret && envDef.def[key].secret) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (options?.hideSecret && envDef.def[key].secret) {
|
|
76
|
+
args[key] = "********";
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
args[key] = value;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return JSON.stringify(args, null, 2);
|
|
83
|
+
};
|
|
84
|
+
// ca clone le .env
|
|
85
|
+
export const exportEnv = (envDef, options, warnings) => {
|
|
86
|
+
const source = options?.source ? dnl.readEnvFile(path.resolve(process.cwd(), options.source)) : process.env;
|
|
87
|
+
const values = envDef.assert({ source });
|
|
88
|
+
const args = [];
|
|
89
|
+
for (const [key, value] of Object.entries(values)) {
|
|
90
|
+
if (options?.excludeSecret && envDef.def[key].secret) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (options?.includeComments && envDef.def[key].description) {
|
|
94
|
+
args.push(`# ${envDef.def[key].description}`);
|
|
95
|
+
}
|
|
96
|
+
if (options?.hideSecret && envDef.def[key].secret) {
|
|
97
|
+
args.push(`${key}=********`);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
args.push(`${key}=${value}`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return args.join("\n");
|
|
104
|
+
};
|
|
105
|
+
export const exportDockerArgs = (envDef, options, warnings) => {
|
|
106
|
+
if (options?.includeComments) {
|
|
107
|
+
warnings.push("--include-comments option invalide avec le format docker-args");
|
|
108
|
+
}
|
|
109
|
+
const source = options?.source ? dnl.readEnvFile(path.resolve(process.cwd(), options.source)) : process.env;
|
|
110
|
+
const values = envDef.assert({ source });
|
|
111
|
+
const args = [];
|
|
112
|
+
for (const [key, value] of Object.entries(values)) {
|
|
113
|
+
if (options?.excludeSecret && envDef.def[key].secret) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (options?.hideSecret && envDef.def[key].secret) {
|
|
117
|
+
args.push(`-e "${key}=********"`);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
args.push(`-e "${key}=${value}"`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return args.join(" ");
|
|
124
|
+
};
|
|
125
|
+
export const exportDockerEnv = (envDef, options, warnings) => {
|
|
126
|
+
const source = options?.source ? dnl.readEnvFile(path.resolve(process.cwd(), options.source)) : process.env;
|
|
127
|
+
const values = envDef.assert({ source });
|
|
128
|
+
const args = [];
|
|
129
|
+
for (const [key, value] of Object.entries(values)) {
|
|
130
|
+
if (options?.excludeSecret && envDef.def[key].secret) {
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (options?.includeComments && envDef.def[key].description) {
|
|
134
|
+
args.push(`# ${envDef.def[key].description}`);
|
|
135
|
+
}
|
|
136
|
+
if (options?.hideSecret && envDef.def[key].secret) {
|
|
137
|
+
args.push(`${key}=********`);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
args.push(`${key}=${value}`);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return args.join("\n");
|
|
144
|
+
};
|
|
145
|
+
export const exportK8sConfigmap = (envDef, options, warnings) => {
|
|
146
|
+
const source = options?.source ? dnl.readEnvFile(path.resolve(process.cwd(), options.source)) : process.env;
|
|
147
|
+
const values = envDef.assert({ source });
|
|
148
|
+
const args = [];
|
|
149
|
+
args.push(`apiVersion: v1`);
|
|
150
|
+
args.push(`kind: ConfigMap`);
|
|
151
|
+
args.push(`metadata:`);
|
|
152
|
+
const name = options?.k8sName ?? "env-config";
|
|
153
|
+
args.push(` name: ${name}`);
|
|
154
|
+
args.push(`data:`);
|
|
155
|
+
for (const [key, value] of Object.entries(values)) {
|
|
156
|
+
if (envDef.def[key].secret) {
|
|
157
|
+
if (options?.excludeSecret)
|
|
158
|
+
continue;
|
|
159
|
+
if (!options?.hideSecret) {
|
|
160
|
+
warnings.push(`Secret ${key} exporté dans un ConfigMap. Utilisez le format k8s-secret.`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const v = options?.hideSecret && envDef.def[key].secret ? "********" : value;
|
|
164
|
+
args.push(` ${key}: ${JSON.stringify(v)}`);
|
|
165
|
+
}
|
|
166
|
+
return args.join("\n");
|
|
167
|
+
};
|
|
168
|
+
export const exportK8sSecret = (envDef, options, warnings) => {
|
|
169
|
+
const source = options?.source ? dnl.readEnvFile(path.resolve(process.cwd(), options.source)) : process.env;
|
|
170
|
+
const values = envDef.assert({ source });
|
|
171
|
+
const args = [];
|
|
172
|
+
args.push(`apiVersion: v1`);
|
|
173
|
+
args.push(`kind: Secret`);
|
|
174
|
+
args.push(`type: Opaque`);
|
|
175
|
+
args.push(`metadata:`);
|
|
176
|
+
const name = options?.k8sName ?? "env-secret";
|
|
177
|
+
args.push(` name: ${name}`);
|
|
178
|
+
args.push(`stringData:`);
|
|
179
|
+
for (const [key, value] of Object.entries(values)) {
|
|
180
|
+
if (options?.excludeSecret && envDef.def[key].secret)
|
|
181
|
+
continue;
|
|
182
|
+
if (!envDef.def[key].secret)
|
|
183
|
+
continue; // Secret = uniquement les variables marquées secret
|
|
184
|
+
const v = options?.hideSecret ? "********" : value;
|
|
185
|
+
args.push(` ${key}: ${JSON.stringify(v)}`);
|
|
186
|
+
}
|
|
187
|
+
return args.join("\n");
|
|
188
|
+
};
|
|
189
|
+
export const exportTs = (envDef, options, warnings) => {
|
|
190
|
+
const source = options?.source ? dnl.readEnvFile(path.resolve(process.cwd(), options.source)) : process.env;
|
|
191
|
+
const values = envDef.assert({ source });
|
|
192
|
+
const middle = [];
|
|
193
|
+
for (const [key, value] of Object.entries(values)) {
|
|
194
|
+
if (options?.excludeSecret && envDef.def[key].secret) {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
if (options?.includeComments && envDef.def[key].description) {
|
|
198
|
+
middle.push(` // ${envDef.def[key].description}`);
|
|
199
|
+
}
|
|
200
|
+
if (options?.hideSecret && envDef.def[key].secret) {
|
|
201
|
+
middle.push(` ${key}: "********",`);
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
middle.push(` ${key}: ${JSON.stringify(value, null, 2)},`);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return `export const env = {\n${middle.join("\n")}\n} as const;`;
|
|
208
|
+
};
|
|
209
|
+
export const exportJs = (envDef, options, warnings) => {
|
|
210
|
+
const source = options?.source ? dnl.readEnvFile(path.resolve(process.cwd(), options.source)) : process.env;
|
|
211
|
+
const values = envDef.assert({ source });
|
|
212
|
+
const middle = [];
|
|
213
|
+
for (const [key, value] of Object.entries(values)) {
|
|
214
|
+
if (options?.excludeSecret && envDef.def[key].secret) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
if (options?.includeComments && envDef.def[key].description) {
|
|
218
|
+
middle.push(` // ${envDef.def[key].description}`);
|
|
219
|
+
}
|
|
220
|
+
if (options?.hideSecret && envDef.def[key].secret) {
|
|
221
|
+
middle.push(` ${key}: "********",`);
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
middle.push(` ${key}: ${JSON.stringify(value, null, 2)},`);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return `export const env = {\n${middle.join("\n")}\n};`;
|
|
228
|
+
};
|
|
229
|
+
export const exportGithubEnv = (envDef, options, warnings) => {
|
|
230
|
+
const source = options?.source ? dnl.readEnvFile(path.resolve(process.cwd(), options.source)) : process.env;
|
|
231
|
+
const values = envDef.assert({ source });
|
|
232
|
+
const args = [];
|
|
233
|
+
for (const [key, value] of Object.entries(values)) {
|
|
234
|
+
if (options?.excludeSecret && envDef.def[key].secret)
|
|
235
|
+
continue;
|
|
236
|
+
const v = options?.hideSecret && envDef.def[key].secret ? "********" : value;
|
|
237
|
+
args.push(`echo "${key}=${v}" >> $GITHUB_ENV`);
|
|
238
|
+
}
|
|
239
|
+
return args.join("\n");
|
|
240
|
+
};
|
|
241
|
+
export const exportGithubSecret = (envDef, options, warnings) => {
|
|
242
|
+
if (options?.hideSecret) {
|
|
243
|
+
warnings.push("--hide-secret est incompatible avec github-secret");
|
|
244
|
+
}
|
|
245
|
+
if (options?.githubOrg && options.githubOrg.includes(" ")) {
|
|
246
|
+
warnings.push("github-org contient un espace, commande gh probablement invalide");
|
|
247
|
+
}
|
|
248
|
+
const source = options?.source ? dnl.readEnvFile(path.resolve(process.cwd(), options.source)) : process.env;
|
|
249
|
+
const values = envDef.assert({ source });
|
|
250
|
+
const scopeFlag = options?.githubOrg ? `--org ${options?.githubOrg}` : "";
|
|
251
|
+
const args = [];
|
|
252
|
+
for (const [key, value] of Object.entries(values)) {
|
|
253
|
+
if (!envDef.def[key].secret)
|
|
254
|
+
continue;
|
|
255
|
+
if (options?.excludeSecret)
|
|
256
|
+
continue;
|
|
257
|
+
args.push(`echo ${JSON.stringify(value)} | gh secret set ${key} ${scopeFlag}`.trim());
|
|
258
|
+
}
|
|
259
|
+
return args.join("\n");
|
|
260
|
+
};
|
|
261
|
+
export const exportGitlabEnv = (envDef, options, warnings) => {
|
|
262
|
+
return exportEnv(envDef, options, warnings);
|
|
263
|
+
};
|
|
@@ -1,7 +1,12 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type GenerateCliOptions = {
|
|
2
2
|
schema?: string;
|
|
3
3
|
out?: string;
|
|
4
4
|
includeSecret?: boolean;
|
|
5
5
|
force?: boolean;
|
|
6
|
-
}
|
|
6
|
+
};
|
|
7
|
+
export type GenerateResult = {
|
|
8
|
+
content: string;
|
|
9
|
+
out: string;
|
|
10
|
+
};
|
|
11
|
+
export declare const generateCommand: (opts?: GenerateCliOptions | undefined) => Promise<GenerateResult>;
|
|
7
12
|
//# sourceMappingURL=generate.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/generate.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/generate.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,kBAAkB,GAAG;IAC7B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,KAAK,CAAC,EAAE,OAAO,CAAC;CACnB,CAAC;AACF,MAAM,MAAM,cAAc,GAAG;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,eAAO,MAAM,eAAe,GAAU,OAAO,kBAAkB,GAAG,SAAS,KAAG,OAAO,CAAC,cAAc,CA6BnG,CAAC"}
|
|
@@ -3,14 +3,14 @@ import path from "node:path";
|
|
|
3
3
|
import { loadDef } from "../utils/load-schema.js";
|
|
4
4
|
import { resolveSchemaPath } from "../utils/resolve-schema.js";
|
|
5
5
|
import { getDefaultEnvValue } from "../utils/printer.js";
|
|
6
|
+
import { ExportError } from "../../errors.js";
|
|
6
7
|
export const generateCommand = async (opts) => {
|
|
7
|
-
const outFile = opts
|
|
8
|
+
const outFile = opts?.out ?? ".env";
|
|
8
9
|
const target = path.resolve(process.cwd(), outFile);
|
|
9
|
-
if (fs.existsSync(target) && !opts
|
|
10
|
-
|
|
11
|
-
process.exit(1);
|
|
10
|
+
if (fs.existsSync(target) && !opts?.force) {
|
|
11
|
+
throw new ExportError(`${outFile} already exists. Use --force to overwrite.`);
|
|
12
12
|
}
|
|
13
|
-
const schemaPath = resolveSchemaPath(opts
|
|
13
|
+
const schemaPath = resolveSchemaPath(opts?.schema);
|
|
14
14
|
const envDef = (await loadDef(schemaPath));
|
|
15
15
|
const lines = [];
|
|
16
16
|
for (const [key, def] of Object.entries(envDef.def)) {
|
|
@@ -23,6 +23,8 @@ export const generateCommand = async (opts) => {
|
|
|
23
23
|
lines.push(""); // blank line
|
|
24
24
|
}
|
|
25
25
|
const output = lines.join("\n");
|
|
26
|
-
|
|
27
|
-
|
|
26
|
+
return {
|
|
27
|
+
content: output,
|
|
28
|
+
out: target,
|
|
29
|
+
};
|
|
28
30
|
};
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type ReverseEnvCliOptions = {
|
|
2
2
|
source?: string;
|
|
3
3
|
out?: string;
|
|
4
4
|
force?: boolean;
|
|
5
5
|
guessSecret?: boolean;
|
|
6
|
-
}
|
|
6
|
+
};
|
|
7
|
+
export type ReverseEnvResult = {
|
|
8
|
+
content: string;
|
|
9
|
+
out: string;
|
|
10
|
+
warnings: string[];
|
|
11
|
+
};
|
|
12
|
+
export declare const reverseEnvCommand: (opts?: ReverseEnvCliOptions | undefined) => Promise<ReverseEnvResult>;
|
|
7
13
|
//# sourceMappingURL=reverseEnv.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"reverseEnv.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/reverseEnv.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"reverseEnv.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/reverseEnv.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,oBAAoB,GAAG;IAC/B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,WAAW,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC3B,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,EAAE,CAAC;CACtB,CAAC;AACF,eAAO,MAAM,iBAAiB,GAAU,OAAO,oBAAoB,GAAG,SAAS,KAAG,OAAO,CAAC,gBAAgB,CAuCzG,CAAC"}
|
|
@@ -2,13 +2,16 @@ import path from "node:path";
|
|
|
2
2
|
import dnl from "../../index.js";
|
|
3
3
|
import { guessSecret, inferSchema } from "../utils/infer-schema.js";
|
|
4
4
|
import fs from "node:fs";
|
|
5
|
+
import { ExportError } from "../../errors.js";
|
|
5
6
|
export const reverseEnvCommand = async (opts) => {
|
|
6
|
-
const source = path.resolve(process.cwd(), opts
|
|
7
|
-
|
|
7
|
+
const source = path.resolve(process.cwd(), opts?.source ?? ".env");
|
|
8
|
+
if (!fs.existsSync(source)) {
|
|
9
|
+
throw new ExportError(`Source env file not found: ${source}`);
|
|
10
|
+
}
|
|
11
|
+
const out = opts?.out ?? "env.dnl.ts";
|
|
8
12
|
const target = path.resolve(process.cwd(), out);
|
|
9
|
-
if (fs.existsSync(target) && !opts
|
|
10
|
-
|
|
11
|
-
process.exit(1);
|
|
13
|
+
if (fs.existsSync(target) && !opts?.force) {
|
|
14
|
+
throw new ExportError(`${out} already exists. Use --force to overwrite.`);
|
|
12
15
|
}
|
|
13
16
|
const env = dnl.readEnvFile(source);
|
|
14
17
|
const lines = [];
|
|
@@ -23,11 +26,15 @@ export const reverseEnvCommand = async (opts) => {
|
|
|
23
26
|
lines.push(` ${key}: {`);
|
|
24
27
|
lines.push(` description: "TODO",`);
|
|
25
28
|
lines.push(` schema: ${inferSchema(value)},`);
|
|
26
|
-
if (opts
|
|
29
|
+
if (opts?.guessSecret && guessSecret(key)) {
|
|
27
30
|
lines.push(` secret: true,`);
|
|
28
31
|
}
|
|
29
32
|
lines.push(` },`);
|
|
30
33
|
}
|
|
31
34
|
lines.push(`});`);
|
|
32
|
-
|
|
35
|
+
return {
|
|
36
|
+
content: lines.join("\n"),
|
|
37
|
+
out: target,
|
|
38
|
+
warnings: ["Generated schema is a prototype. Review schemas, descriptions and secrets before using."],
|
|
39
|
+
};
|
|
33
40
|
};
|