-
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.
- Loading branch information
0 parents
commit f7b0ec9
Showing
26 changed files
with
596 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
* | ||
!.gitignore | ||
!etc | ||
!examples | ||
!examples/** | ||
!templates | ||
!scripts | ||
!**/*.ts | ||
!**/*.md | ||
!**/*.svg | ||
!**/*.png | ||
!**/*.json | ||
!**/*.jsonc | ||
!**/*.sh | ||
!**/*.vto |
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,55 @@ | ||
<p align="center"><a href="https://github.com/mattvr/dgen"> | ||
<img src="/etc/dgen.svg" ></a></p> | ||
<p align="center"><strong>Turn data into generated code and text files.</strong></p> | ||
|
||
--- | ||
|
||
`dgen` takes a template file, plus some data or executable code, and then generates a new file from it. | ||
|
||
It simplifies going from structured data to code – and can be used to generate and format TypeScript, HTML, Markdown, JSON, blog posts, or **any** other type of text file. | ||
|
||
This can be used as a command line utility, or as a module in your own codebase | ||
via its exported `codegen` function. | ||
|
||
## Examples | ||
|
||
<p align="center"><img src="/etc/md.png" ></p> | ||
<p align="center"><img src="/etc/ts.png" ></p> | ||
|
||
Generate Markdown, Typescript, and much more. Check out the full [examples](examples/). | ||
|
||
## Setup | ||
|
||
[Install Deno](https://docs.deno.com/runtime/manual) and ensure it is in your | ||
path. | ||
|
||
Then, run: `deno install -frA --name=dgen mod.ts` | ||
|
||
This will install `dgen` as a command line utility. | ||
|
||
## Usage | ||
|
||
A template is required, and ideally a data file or some TypeScript code to | ||
return data. | ||
|
||
Templates must be [vento (.vto) files](https://github.com/oscarotero/vento). | ||
|
||
|
||
```sh | ||
# Use with input file, data, and output. | ||
dgen --in=myCodegenTemplate.vto --data=myCodegenData.json --out=myCodegenFile.ts | ||
|
||
# Use with input file, data, output, plus additional processor step. | ||
dgen --in=myCodegenTemplate.vto --data=myCodegenData.json --processor=myTransformationStep.ts --out=myCodegenFile.ts | ||
``` | ||
|
||
### Command Line Arguments | ||
|
||
| Option | Description | Example Usage | | ||
| ------------- | -------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ | | ||
| `--in` | Path to the template file (vento .vto template), required | `--in <template.vto>` | | ||
| `--out` | Path to the output file, optional, will print to stdout if not provided | `--out <output.ts>` | | ||
| `--data` | Path to the data file (JSON or JSONC), optional | `--data <data.json>` | | ||
| `--processor` | Path to the JS/TS processor file, optional | `--processor <processor.ts>` | | ||
| `--flags` | Additional flags to run alongside the codegen process, optional. Accepts 'fmt', 'check', 'print_info'. Set to 'none' to skip defaults. | `--flags fmt,check,print_info` | | ||
| `--help` | Print the help message | `--help` | |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,10 @@ | ||
## Examples | ||
|
||
A few examples are provided in this folder. See the [input](input/) and corresponding [output](output/) folders. | ||
|
||
Try them out by running **`./scripts/run-example-<name>.sh` from the parent folder**. | ||
|
||
- `basic` - A basic example of using `dgen` with just a template files and the default data. It creates a TypeScript file. | ||
- `data` - An example of using `dgen` with a data file. It creates a TypeScript file and then executes it. | ||
- `markdown` - An example of using `dgen` with a data file and outputting Markdown. | ||
- `processor` - An example of using `dgen` with a processor file. |
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,4 @@ | ||
// Example TypeScript template | ||
|
||
const str = "{{ hello() }}, my name is {{ name }}!" | ||
console.log(str); |
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,29 @@ | ||
{ | ||
// This is a JSONC file, which means it's a JSON file with comments. | ||
"players": [ | ||
{ | ||
"name": "art3mis", | ||
"level": 10, | ||
"isOnline": true, | ||
"notes": "This ain't no game, kid!" | ||
}, | ||
{ | ||
"name": "parzival", | ||
"level": 5, | ||
"isOnline": false, | ||
"notes": "I'm not a gunter, I'm a gamer." | ||
}, | ||
{ | ||
"name": "aech", | ||
"level": 7, | ||
"isOnline": true, | ||
"notes": "It is on like Red Dawn!" | ||
}, | ||
{ | ||
"name": "gunter", | ||
"level": 2, | ||
"isOnline": false, | ||
"notes": "I support the OASIS." | ||
} | ||
] | ||
} |
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,30 @@ | ||
// Example Typescript code with a JSONC data file | ||
|
||
class Player { | ||
name: string; | ||
level: number; | ||
isOnline: boolean; | ||
|
||
constructor(name: string, level: number, isOnline: boolean) { | ||
this.name = name; | ||
this.level = level; | ||
this.isOnline = isOnline; | ||
} | ||
|
||
toString() { | ||
return `${this.name} (Lvl. ${this.level})`; | ||
} | ||
} | ||
|
||
const players = [ | ||
{{ for player of players }} | ||
// {{ player.notes}} | ||
new Player('{{ player.name }}', {{ player.level }}, {{ player.isOnline }}), | ||
{{ /for }} | ||
]; | ||
|
||
console.log(`There are ${players.length} players in the game.`); | ||
|
||
const onlinePlayers = players.filter(player => player.isOnline).join(', '); | ||
|
||
console.log(`The following players are online: ${onlinePlayers}`); |
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,14 @@ | ||
{ | ||
"sections": { | ||
"header": { | ||
"title": "dgen markdown template", | ||
"content": "This is the header." | ||
}, | ||
"content": { | ||
"content": "## Hello, World!\n\nThis is a simple example of a markdown template." | ||
}, | ||
"footer": { | ||
"content": "This is the footer." | ||
} | ||
} | ||
} |
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,5 @@ | ||
{{ for section of sections }} | ||
{{ if section.title }}# {{ section.title }}{{ /if }} | ||
{{ if section.subtitle }}## {{ section.subtitle }}{{ /if }} | ||
{{ if section.content }}{{ section.content }}{{ /if }} | ||
{{ /for }} |
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,12 @@ | ||
export default async function codegen( | ||
data: any, | ||
): Promise<Record<string, unknown>> { | ||
return { | ||
data: { | ||
...(data ? data : {}), | ||
pi: Math.PI, | ||
e: Math.E, | ||
c: 299792458, | ||
} | ||
} | ||
} |
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,7 @@ | ||
# dgen processor example | ||
|
||
Here are some math constants: | ||
|
||
e: {{ e }} | ||
pi: {{ pi }} | ||
c: {{ c }} m/s |
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,4 @@ | ||
// Example TypeScript template | ||
|
||
const str = "sup dawg, my name is Bobert Paulson!"; | ||
console.log(str); |
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,37 @@ | ||
// Example Typescript code with a JSONC data file | ||
|
||
class Player { | ||
name: string; | ||
level: number; | ||
isOnline: boolean; | ||
|
||
constructor(name: string, level: number, isOnline: boolean) { | ||
this.name = name; | ||
this.level = level; | ||
this.isOnline = isOnline; | ||
} | ||
|
||
toString() { | ||
return `${this.name} (Lvl. ${this.level})`; | ||
} | ||
} | ||
|
||
const players = [ | ||
// This ain't no game, kid! | ||
new Player("art3mis", 10, true), | ||
|
||
// I'm not a gunter, I'm a gamer. | ||
new Player("parzival", 5, false), | ||
|
||
// It is on like Red Dawn! | ||
new Player("aech", 7, true), | ||
|
||
// I support the OASIS. | ||
new Player("gunter", 2, false), | ||
]; | ||
|
||
console.log(`There are ${players.length} players in the game.`); | ||
|
||
const onlinePlayers = players.filter((player) => player.isOnline).join(", "); | ||
|
||
console.log(`The following players are online: ${onlinePlayers}`); |
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,13 @@ | ||
# dgen markdown template | ||
|
||
This is the header. | ||
|
||
|
||
|
||
## Hello, World! | ||
|
||
This is a simple example of a markdown template. | ||
|
||
|
||
|
||
This is the footer. |
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,7 @@ | ||
# dgen processor example | ||
|
||
Here are some math constants: | ||
|
||
e: 2.718281828459045 | ||
pi: 3.141592653589793 | ||
c: 299792458 m/s |
Oops, something went wrong.