Skip to content

Commit

Permalink
improve import, docs, and logo
Browse files Browse the repository at this point in the history
  • Loading branch information
mattvr committed Mar 20, 2024
1 parent 2315f5c commit 60440c1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 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.
31 changes: 30 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import vento from "https://deno.land/x/[email protected]/mod.ts";
import { Filter } from "https://deno.land/x/[email protected]/src/environment.ts";
import { parse as parseJsonc } from "https://deno.land/[email protected]/jsonc/parse.ts";
import { parseArgs } from "https://deno.land/[email protected]/cli/parse_args.ts";
import * as path from "https://deno.land/[email protected]/path/mod.ts";

/**
* Arguments object to pass to the codegen(...) function.
Expand Down Expand Up @@ -98,6 +99,32 @@ export const DEFAULT_ARGS: Partial<CodegenArgs> = {
/**
* Generate code from a Vento template and optional processor file
* @param args Arguments object to pass to the code
* @param args.templateVtoPath Full path to the template file (vento .vto
* template)
* @param args.processorTsPath Full path to processor file [optional], which
* will pass data and additional filters to the template. Must export a
* default function that takes an optional data Json object and returns an
* object with the final data and filters to pass to the template, like so:
* ```ts
* export default (dataJson: any) => ({
* data: {
* myVar: "yeet",
* hello() {
* return "hello world";
* },
* filters: {
* upper: (str: string) => str.toUpperCase(),
* }
* });
* ```
* @param args.dataJsonPath JSON string to pass to the template as data.
* @param args.outputPath Full path to the final output file, can be any file
* type (.ts, .json, .md, etc.)
* @param args.filters Filters to pass to the template, which are arbitrary
* functions that can transform any variable.
* @param args.data Arbitrary data to pass to the template
* @param args.flags Additional flags to run alongside the codegen process
* @param args.error Optional error handler
* @returns Generated code as a string
*/
export const codegen = async (args: CodegenArgs): Promise<string> => {
Expand Down Expand Up @@ -139,7 +166,9 @@ export const codegen = async (args: CodegenArgs): Promise<string> => {

if (processorTsPath) {
try {
const processor = (await import(processorTsPath)).default;
// Resolve as http(s) URL or file path relative to current working directory
const processorTsPathResolved = processorTsPath.startsWith('http') ? processorTsPath : path.resolve(Deno.cwd(), processorTsPath);
const processor = (await import(processorTsPathResolved)).default;
const result = await processor(processorData);

processorData = result.data;
Expand Down

0 comments on commit 60440c1

Please sign in to comment.