Skip to content

Commit c99c45f

Browse files
authored
Merge pull request #20 from wuespace/feat/envar-writer
Add `envar` writer to generate code for `jsr:@wuespace/envar`
2 parents d3cdfcf + a40f3a1 commit c99c45f

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ envardoc <writer> <path-to-env-file>
1818

1919
- `docs` - Prints the documentation in markdown format
2020
- `example` - Prints a `.env.example` file with all variables and comments
21+
- `envar` - Prints TypeScript code to use with the [`jsr:@wuespace/envar`](https://jsr.io/@wuespace/envar) package.
2122

2223
### With Deno
2324

lib/writers/envar.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import type { Writer } from "$writers";
2+
import type { Variable } from "$ast";
3+
4+
export class EnvarWriter implements Writer {
5+
writeHeader(prev: string): string {
6+
return prev + `import {
7+
initVariable,
8+
REQUIRED,
9+
OPTIONAL,
10+
} from "jsr:@wuespace/envar";
11+
12+
/**
13+
* Initializes the environment variables using the jsr:@wuespace/envar library.
14+
*
15+
* Automatically generated by [envardoc](https://jsr.io/@wuespace/envardoc).
16+
*/
17+
export function initEnv(): Promise<void> {
18+
return Promise.all([
19+
`;
20+
}
21+
22+
writeVariable(variable: Variable, prev: string): string {
23+
variable.description?.trim().split("\n").forEach((line) => {
24+
prev += ` // ${line}\n`;
25+
});
26+
if (variable.optional) {
27+
return this.writeOptionalVariable(variable, prev) + "\n";
28+
} else {
29+
return this.writeRequiredVariable(variable, prev) + "\n";
30+
}
31+
}
32+
33+
private writeOptionalVariable(variable: Variable, prev: string): string {
34+
prev += ' initVariable("';
35+
prev += variable.name;
36+
prev += '", OPTIONAL';
37+
if (variable.defaultValue) {
38+
prev += ', "';
39+
prev += variable.defaultValue;
40+
prev += '"';
41+
}
42+
prev += "),\n";
43+
return prev;
44+
}
45+
46+
private writeRequiredVariable(variable: Variable, prev: string): string {
47+
prev += ' initVariable("';
48+
prev += variable.name;
49+
prev += '", REQUIRED),\n';
50+
return prev;
51+
}
52+
53+
writeFooter(prev: string): string {
54+
return prev.trimEnd() + `\n ]);
55+
}
56+
`;
57+
}
58+
}

lib/writers/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./docs.ts";
22
export * from "./env-example.ts";
3+
export * from "./envar.ts";
34
export * from "./Writer.ts";

main.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { parse } from "$parse";
2-
import { DocsWriter, EnvExampleWriter, type Writer } from "$writers";
2+
import {
3+
DocsWriter,
4+
EnvarWriter,
5+
EnvExampleWriter,
6+
type Writer,
7+
} from "$writers";
38
import { Command } from "@cliffy/command";
49
import { exists } from "@std/fs";
510
import { resolve } from "@std/path";
@@ -23,6 +28,23 @@ if (import.meta.main) {
2328
}
2429
});
2530

31+
const envar = new Command()
32+
.arguments("<path:file>")
33+
.description("Generate jsr:@wuespace/envar source code from a .env file")
34+
.option("-o, --output <path:string>", "Output file path")
35+
.action(async ({ output }, path) => {
36+
const sourceCode = await parseAndConvert(
37+
path,
38+
new EnvarWriter(),
39+
);
40+
if (output) {
41+
await Deno.writeTextFile(output, sourceCode);
42+
console.log(`File written: ${output}`);
43+
} else {
44+
console.log(sourceCode);
45+
}
46+
});
47+
2648
const envExample = new Command()
2749
.arguments("<path:string>")
2850
.description("Generate example .env file")
@@ -52,6 +74,7 @@ if (import.meta.main) {
5274
)
5375
.command("docs", docs)
5476
.command("example", envExample)
77+
.command("envar", envar)
5578
.parse(Deno.args);
5679
}
5780

0 commit comments

Comments
 (0)