Skip to content
This repository has been archived by the owner on May 31, 2023. It is now read-only.

Dengen: Module and CLI tool #2

Merged
merged 8 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"deno.enable": true,
"deno.config": "./deno.jsonc",
"editor.formatOnSave": true,
"editor.defaultFormatter": "denoland.vscode-deno"
}
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# dengen (because deno.land/x/generate is taken)
# Dengen

[![deno doc](https://doc.deno.land/badge.svg)](https://deno.land/x/generate)

Automate code generation by running procedures defined in comment annotations.

This repository is archived in favor of <https://deno.land/x/generate>.

---

Programmed with 🦕 by [**@EthanThatOneKid**](https://etok.codes/)
85 changes: 85 additions & 0 deletions cli/flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
globToRegExp,
isAbsolute,
isGlob,
join,
parse,
toFileUrl,
} from "../deps.ts";
import type { GenerateOptions } from "../mod.ts";

/**
* toGenerateOptions converts CLI flags to GenerateOptions.
*/
export function toGenerateOptions(flags: GenerateFlags): GenerateOptions {
return {
rootSpecifiers: flags["_"]
.map(String)
.map((specifier) =>
isAbsolute(specifier)
? specifier
: toFileUrl(join(Deno.cwd(), specifier)).href
),
dryRun: flags["dry-run"],
trace: flags["trace"],
verbose: flags["verbose"],
run: flags["run"].map((pattern) => new RegExp(pattern)),
skip: flags["skip"].map((pattern) => new RegExp(pattern)),
include: flags["include"]
.map((specifierOrGlob) =>
isGlob(specifierOrGlob)
? globToRegExp(specifierOrGlob)
: escapeRegExp(specifierOrGlob)
),
exclude: flags["exclude"]
.map((specifierOrGlob) =>
isGlob(specifierOrGlob)
? globToRegExp(specifierOrGlob)
: escapeRegExp(specifierOrGlob)
),
};
}

/**
* GenerateFlags is the CLI flags.
*/
export type GenerateFlags = ReturnType<typeof parseGenerateFlags>;

/**
* parseGenerateFlags parses the program's CLI flags.
*/
export function parseGenerateFlags(args: string[]) {
return parse(args, {
"--": false,
stopEarly: true,
string: ["run", "skip", "include", "exclude"],
collect: ["run", "skip", "include", "exclude"],
boolean: ["dry-run", "verbose", "trace"],
alias: {
"dry-run": ["n"],
"verbose": ["v"],
"trace": ["x"],
"run": ["r"],
"skip": ["s"],
},
default: {
"dry-run": false,
"verbose": false,
"trace": false,
},
});
}

/**
* escapeRegExp escapes a string for use in a regular expression.
*
* @see
* https://github.com/sindresorhus/escape-string-regexp#readme
*/
export function escapeRegExp(pattern: string): RegExp {
return new RegExp(
pattern
.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&")
.replace(/-/g, "\\x2d"),
);
}
30 changes: 30 additions & 0 deletions cli/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { handlers, setup } from "../deps.ts";
import { GENERATE, generate } from "../mod.ts";
import { parseGenerateFlags, toGenerateOptions } from "./flags.ts";

if (import.meta.main) {
await main();
}

/**
* main is the entrypoint of the program.
*/
async function main() {
const flags = parseGenerateFlags(Deno.args);
const options = toGenerateOptions(flags);

await setup({
loggers: {
[GENERATE]: {
// Suppress log messages if not verbose.
level: options.verbose ? "DEBUG" : "WARNING",
handlers: ["console"],
},
},
handlers: {
console: new handlers.ConsoleHandler("DEBUG"),
},
});

await generate(options);
}
10 changes: 10 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"lock": "./deno.lock",
"tasks": {
"lint": "deno lint",
"fmt": "deno fmt",
"udd": "deno run -r -A https://deno.land/x/udd/main.ts dev_deps.ts deps.ts && deno task lock",
"lock": "deno cache --lock=deno.lock --lock-write dev_deps.ts deps.ts",
"all": "deno task udd && deno task lint && deno task fmt"
}
}
37 changes: 37 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export { parse } from "https://deno.land/[email protected]/flags/mod.ts";
export {
dirname,
globToRegExp,
isAbsolute,
isGlob,
join,
toFileUrl,
} from "https://deno.land/[email protected]/path/mod.ts";
export {
getLogger,
handlers,
setup,
} from "https://deno.land/[email protected]/log/mod.ts";
export type { Reader } from "https://deno.land/[email protected]/types.d.ts";
export { readLines } from "https://deno.land/[email protected]/io/read_lines.ts";

export { createGraph } from "https://deno.land/x/[email protected]/mod.ts";
4 changes: 4 additions & 0 deletions dev_deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export {
assertEquals,
assertThrows,
} from "https://deno.land/[email protected]/testing/asserts.ts";
Loading