Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
mattvr committed Mar 12, 2024
0 parents commit f7b0ec9
Show file tree
Hide file tree
Showing 26 changed files with 596 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
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
55 changes: 55 additions & 0 deletions README.md
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` |
20 changes: 20 additions & 0 deletions etc/dgen.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added etc/md.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added etc/ts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions examples/README.md
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.
4 changes: 4 additions & 0 deletions examples/input/basic.vto
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);
29 changes: 29 additions & 0 deletions examples/input/data.jsonc
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."
}
]
}
30 changes: 30 additions & 0 deletions examples/input/data.vto
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}`);
14 changes: 14 additions & 0 deletions examples/input/markdown.jsonc
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."
}
}
}
5 changes: 5 additions & 0 deletions examples/input/markdown.vto
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 }}
12 changes: 12 additions & 0 deletions examples/input/processor.ts
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,
}
}
}
7 changes: 7 additions & 0 deletions examples/input/processor.vto
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
4 changes: 4 additions & 0 deletions examples/output/basic.ts
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);
37 changes: 37 additions & 0 deletions examples/output/data.ts
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}`);
13 changes: 13 additions & 0 deletions examples/output/markdown.md
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.
7 changes: 7 additions & 0 deletions examples/output/processor.md
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
Loading

0 comments on commit f7b0ec9

Please sign in to comment.