Skip to content

Commit 60440c1

Browse files
committed
improve import, docs, and logo
1 parent 2315f5c commit 60440c1

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

etc/dgen.svg

Lines changed: 1 addition & 0 deletions
Loading

mod.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import vento from "https://deno.land/x/vento@v0.12.1/mod.ts";
22
import { Filter } from "https://deno.land/x/vento@v0.12.1/src/environment.ts";
33
import { parse as parseJsonc } from "https://deno.land/std@0.212.0/jsonc/parse.ts";
44
import { parseArgs } from "https://deno.land/std@0.212.0/cli/parse_args.ts";
5+
import * as path from "https://deno.land/std@0.212.0/path/mod.ts";
56

67
/**
78
* Arguments object to pass to the codegen(...) function.
@@ -98,6 +99,32 @@ export const DEFAULT_ARGS: Partial<CodegenArgs> = {
9899
/**
99100
* Generate code from a Vento template and optional processor file
100101
* @param args Arguments object to pass to the code
102+
* @param args.templateVtoPath Full path to the template file (vento .vto
103+
* template)
104+
* @param args.processorTsPath Full path to processor file [optional], which
105+
* will pass data and additional filters to the template. Must export a
106+
* default function that takes an optional data Json object and returns an
107+
* object with the final data and filters to pass to the template, like so:
108+
* ```ts
109+
* export default (dataJson: any) => ({
110+
* data: {
111+
* myVar: "yeet",
112+
* hello() {
113+
* return "hello world";
114+
* },
115+
* filters: {
116+
* upper: (str: string) => str.toUpperCase(),
117+
* }
118+
* });
119+
* ```
120+
* @param args.dataJsonPath JSON string to pass to the template as data.
121+
* @param args.outputPath Full path to the final output file, can be any file
122+
* type (.ts, .json, .md, etc.)
123+
* @param args.filters Filters to pass to the template, which are arbitrary
124+
* functions that can transform any variable.
125+
* @param args.data Arbitrary data to pass to the template
126+
* @param args.flags Additional flags to run alongside the codegen process
127+
* @param args.error Optional error handler
101128
* @returns Generated code as a string
102129
*/
103130
export const codegen = async (args: CodegenArgs): Promise<string> => {
@@ -139,7 +166,9 @@ export const codegen = async (args: CodegenArgs): Promise<string> => {
139166

140167
if (processorTsPath) {
141168
try {
142-
const processor = (await import(processorTsPath)).default;
169+
// Resolve as http(s) URL or file path relative to current working directory
170+
const processorTsPathResolved = processorTsPath.startsWith('http') ? processorTsPath : path.resolve(Deno.cwd(), processorTsPath);
171+
const processor = (await import(processorTsPathResolved)).default;
143172
const result = await processor(processorData);
144173

145174
processorData = result.data;

0 commit comments

Comments
 (0)