-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
envar
writer to generate code for jsr:@wuespace/envar
With this change, it is possible to, based on the example env file, automatically generate code to use with the `jsr:@wuespace/envar` package. This generates a self-contained file which exports a function `initEnv(): Promise<void>`, which initializes the variables with basic validators. Usage: ```shell envardoc envar -o lib/env.ts example.env ```
- Loading branch information
Showing
4 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { Writer } from "$writers"; | ||
import type { Variable } from "$ast"; | ||
|
||
export class EnvarWriter implements Writer { | ||
writeHeader(prev: string): string { | ||
return prev + `import { | ||
initVariable, | ||
REQUIRED, | ||
OPTIONAL, | ||
} from "jsr:@wuespace/envar"; | ||
/** | ||
* Initializes the environment variables using the jsr:@wuespace/envar library. | ||
* | ||
* Automatically generated by [envardoc](https://jsr.io/@wuespace/envardoc). | ||
*/ | ||
export function initEnv(): Promise<void> { | ||
return Promise.all([ | ||
`; | ||
} | ||
|
||
writeVariable(variable: Variable, prev: string): string { | ||
variable.description?.trim().split("\n").forEach((line) => { | ||
prev += ` // ${line}\n`; | ||
}); | ||
if (variable.optional) { | ||
return this.writeOptionalVariable(variable, prev) + "\n"; | ||
} else { | ||
return this.writeRequiredVariable(variable, prev) + "\n"; | ||
} | ||
} | ||
|
||
private writeOptionalVariable(variable: Variable, prev: string): string { | ||
prev += ' initVariable("'; | ||
prev += variable.name; | ||
prev += '", OPTIONAL'; | ||
if (variable.defaultValue) { | ||
prev += ', "'; | ||
prev += variable.defaultValue; | ||
prev += '"'; | ||
} | ||
prev += "),\n"; | ||
return prev; | ||
} | ||
|
||
private writeRequiredVariable(variable: Variable, prev: string): string { | ||
prev += ' initVariable("'; | ||
prev += variable.name; | ||
prev += '", REQUIRED),\n'; | ||
return prev; | ||
} | ||
|
||
writeFooter(prev: string): string { | ||
return prev.trimEnd() + `\n ]); | ||
} | ||
`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./docs.ts"; | ||
export * from "./env-example.ts"; | ||
export * from "./envar.ts"; | ||
export * from "./Writer.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters