@wxn0brp/create-db-adapter 0.0.0-stage → 0.20261129.108

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/README.md +84 -2
  2. package/dist/bin.js +6 -0
  3. package/dist/cli/args.js +108 -0
  4. package/dist/cli/output.js +31 -0
  5. package/dist/cli/tui.js +84 -0
  6. package/dist/generate.js +31 -0
  7. package/dist/index.js +8 -0
  8. package/dist/naming.js +51 -0
  9. package/dist/options.js +34 -0
  10. package/dist/package-meta.js +46 -0
  11. package/dist/plan.js +36 -0
  12. package/dist/run.js +60 -0
  13. package/dist/template-engine.js +48 -0
  14. package/dist/types.js +1 -0
  15. package/dist/variants.js +40 -0
  16. package/package.json +39 -5
  17. package/templates/a/src/action.ts +99 -0
  18. package/templates/a/src/index.ts +20 -0
  19. package/templates/a/src/types.ts +3 -0
  20. package/templates/a/valthera-e2e/index.ts +18 -0
  21. package/templates/b/src/action.ts +139 -0
  22. package/templates/b/src/file/index.ts +91 -0
  23. package/templates/b/src/format.ts +13 -0
  24. package/templates/b/src/index.ts +22 -0
  25. package/templates/b/src/types.ts +3 -0
  26. package/templates/b/valthera-e2e/index.ts +18 -0
  27. package/templates/c/src/action.ts +91 -0
  28. package/templates/c/src/index.ts +21 -0
  29. package/templates/c/src/translate.ts +8 -0
  30. package/templates/c/src/types.ts +3 -0
  31. package/templates/c/valthera-e2e/index.ts +12 -0
  32. package/templates/common/.github/workflows/build.yml +46 -0
  33. package/templates/common/LICENSE +21 -0
  34. package/templates/common/README.md +33 -0
  35. package/templates/common/biome.json.tmpl +6 -0
  36. package/templates/common/package.json +49 -0
  37. package/templates/common/rusalka/build.ts +1 -0
  38. package/templates/common/src/version.ts +1 -0
  39. package/templates/common/suglite.json5 +6 -0
  40. package/templates/common/tsconfig.json.tmpl +27 -0
  41. package/templates/common/valthera-e2e/config.json +6 -0
  42. package/templates/common/valthera-e2e/tsconfig.json.tmpl +18 -0
  43. package/templates/d/src/action.ts +106 -0
  44. package/templates/d/src/index.ts +22 -0
  45. package/templates/d/src/types.ts +3 -0
  46. package/templates/d/valthera-e2e/config.json +9 -0
  47. package/templates/d/valthera-e2e/index.ts +12 -0
package/README.md CHANGED
@@ -1,3 +1,85 @@
1
- # Temporary Holding Version
1
+ # @wxn0brp/create-db-adapter
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Scaffold generator for [ValtheraDB](https://github.com/wxn0brP/ValtheraDB) storage and specialized adapters.
4
+
5
+ It produces a ready-to-build package that follows the ecosystem conventions: the `db-storage-<name>` / `db-spec-<name>` naming, the `0.CA.B` version scheme, the `tsc && tsc-alias` build, the rusalka deploy workflow and the `valthera-e2e` compliance suite.
6
+
7
+ ## Usage
8
+
9
+ ```bash
10
+ bun create @wxn0brp/db-adapter
11
+ ```
12
+
13
+ or run it directly:
14
+
15
+ ```bash
16
+ bunx @wxn0brp/create-db-adapter -v a -n csv
17
+ ```
18
+
19
+ Interactively (no flags) it opens a small TUI: pick the variant with the arrow keys, fill in the name and target directory, and toggle the extras with space. Pass both `--variant` and `--name` to skip the wizard entirely; other flags pre-fill the wizard and skip their own question.
20
+
21
+ ## Variants
22
+
23
+ | Variant | Base | You implement |
24
+ | ------- | ---- | ------------- |
25
+ | `a` | `CustomAdapterBase` + `CustomFileCpu` | `read`/`write` and collection management |
26
+ | `b` | `AdapterBase` + custom `FileCpu` | the `FileCpu` interface |
27
+ | `c` | `AdapterBase` (full) | every CRUD method, query translation |
28
+ | `d` | `AdapterBase` (partial) | only the operations your data model supports |
29
+
30
+ Variants `a`, `b` and `c` produce a CRUD package (`db-storage-*`, keyword `vdb-adapter`). Variant `d` produces a specialized package (`db-spec-*`, keyword `vdb-spec`) with a pre-filled `valthera-e2e/config.json` skip list to trim.
31
+
32
+ See the [adapter guide](https://wxn0brp.github.io/ValtheraDB/dev/adapter) for the contract details.
33
+
34
+ ## Options
35
+
36
+ ```
37
+ -v, --variant <a|b|c|d> Adapter variant
38
+ -n, --name <name> Adapter name, e.g. postgres
39
+ -d, --dir <path> Target directory (default: ./db-storage-<name>)
40
+ -s, --scope <@scope> npm scope (default: lowercase NIT_USER)
41
+ --author <author> Package author (default: NIT_USER or OS username)
42
+ --core <range> Core version range (default: detected or ^0.13.0)
43
+ --no-github Skip the GitHub Actions workflow
44
+ --no-e2e Skip the valthera-e2e suite
45
+ --no-install Skip bun install after generating
46
+ -f, --force Allow writing into a non-empty directory
47
+ --dry-run Print the file plan without writing
48
+ --json Machine-readable output (no wizard, no install)
49
+ -h, --help Show this help
50
+ ```
51
+
52
+ The author and npm scope use the same resolution as `nitl` from the dotfiles: `NIT_USER` when set, otherwise the OS username. The scope is the lowercase username, and the repository and homepage are derived from the author. Passing `--scope` or `--author` still overrides them.
53
+
54
+ ## Generated layout
55
+
56
+ ```
57
+ package.json tsconfig.json biome.json suglite.json5 .gitignore
58
+ README.md LICENSE rusalka/build.ts .github/workflows/build.yml
59
+ src/version.ts src/index.ts src/...
60
+ valthera-e2e/index.ts valthera-e2e/tsconfig.json valthera-e2e/config.json
61
+ ```
62
+
63
+ Every `src/index.ts` exports a `create<Name>Adapter` factory plus a `DYNAMIC` map (`{ def: create<Name>Adapter }`) so the package works with `@wxn0brp/db-resolver` (`VDB()`). Add extra keys to `DYNAMIC` only for explicit variants.
64
+
65
+ ## Templates
66
+
67
+ The generated content lives as real files under `templates/`, not as strings in `src`. The generator walks and copies them, replacing `___token___` placeholders (`___pascal___`, `___package___`, `___version___`, `___keyword___`, ...). The delimiter is a valid TypeScript identifier so the templates parse cleanly in an editor or compiler.
68
+
69
+ - `templates/common/` is copied first, `templates/<variant>/` second; a file in the variant directory overrides the same path in `common/` (used by variant `d` to ship a pre-filled `valthera-e2e/config.json`).
70
+ - A trailing `.tmpl` in a template path is stripped on output. It is used for files that tooling would otherwise treat specially, such as `biome.json` and `tsconfig.json`.
71
+ - `--no-github` excludes the `.github/` prefix, `--no-e2e` excludes `valthera-e2e/`.
72
+
73
+ To change what gets generated, edit the template file. To change variant facts (kind, keyword, prompt text), edit `src/variants.ts`.
74
+
75
+ ## Programmatic API
76
+
77
+ ```typescript
78
+ import { buildPlan, writePlan, resolveOptions } from "@wxn0brp/create-db-adapter";
79
+ ```
80
+
81
+ `buildPlan` returns an in-memory `{ path, content }[]` without touching the filesystem, which makes it easy to inspect or test.
82
+
83
+ ## License
84
+
85
+ MIT
package/dist/bin.js ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bun
2
+ import { run } from "./run.js";
3
+ run().catch(err => {
4
+ console.error(err instanceof Error ? err.message : String(err));
5
+ process.exit(1);
6
+ });
@@ -0,0 +1,108 @@
1
+ import { parseArgs } from "node:util";
2
+ import { nitUser } from "../naming.js";
3
+ import { VARIANT_LIST, isVariantId } from "../variants.js";
4
+ export function printHelp() {
5
+ const variants = VARIANT_LIST.map(spec => ` ${spec.label}`).join("\n");
6
+ const user = nitUser();
7
+ console.log(`
8
+ Usage: create-db-adapter [options]
9
+
10
+ ValtheraDB adapter scaffold generator. Run it without options for the
11
+ interactive wizard, or pass --variant and --name to generate without prompts.
12
+
13
+ Options:
14
+ -v, --variant <a|b|c|d> Adapter variant
15
+ -n, --name <name> Adapter name, e.g. postgres
16
+ -d, --dir <path> Target directory (default: ./db-storage-<name>)
17
+ -s, --scope <@scope> npm scope (default: @${user.toLowerCase()})
18
+ --author <author> Package author (default: ${user})
19
+ --core <range> Core version range (default: detected or ^0.13.0)
20
+ --no-github Skip the GitHub Actions workflow
21
+ --no-e2e Skip the valthera-e2e suite
22
+ --no-install Skip bun install after generating
23
+ -f, --force Allow writing into a non-empty directory
24
+ --dry-run Print the file plan without writing
25
+ --json Machine-readable output (no wizard, no install)
26
+ -h, --help Show this help
27
+
28
+ Variants:
29
+ ${variants}
30
+
31
+ Examples:
32
+ create-db-adapter
33
+ create-db-adapter -v a -n csv
34
+ create-db-adapter -v c -n postgres -d ./db-storage-postgres
35
+ `.trim());
36
+ }
37
+ export function getArgs(argv = process.argv.slice(2)) {
38
+ const { values } = parseArgs({
39
+ args: argv,
40
+ options: {
41
+ variant: {
42
+ type: "string",
43
+ short: "v",
44
+ },
45
+ name: {
46
+ type: "string",
47
+ short: "n",
48
+ },
49
+ scope: {
50
+ type: "string",
51
+ short: "s",
52
+ },
53
+ dir: {
54
+ type: "string",
55
+ short: "d",
56
+ },
57
+ core: {
58
+ type: "string",
59
+ },
60
+ author: {
61
+ type: "string",
62
+ },
63
+ "no-github": {
64
+ type: "boolean",
65
+ },
66
+ "no-e2e": {
67
+ type: "boolean",
68
+ },
69
+ "no-install": {
70
+ type: "boolean",
71
+ },
72
+ force: {
73
+ type: "boolean",
74
+ short: "f",
75
+ },
76
+ "dry-run": {
77
+ type: "boolean",
78
+ },
79
+ json: {
80
+ type: "boolean",
81
+ },
82
+ help: {
83
+ type: "boolean",
84
+ short: "h",
85
+ },
86
+ },
87
+ strict: true,
88
+ allowPositionals: false,
89
+ });
90
+ const rawVariant = values.variant;
91
+ if (rawVariant && !isVariantId(rawVariant))
92
+ throw new Error(`Unknown variant "${rawVariant}". Use a, b, c or d.`);
93
+ return {
94
+ variant: rawVariant,
95
+ name: values.name,
96
+ scope: values.scope,
97
+ dir: values.dir,
98
+ core: values.core,
99
+ author: values.author,
100
+ github: !values["no-github"],
101
+ e2e: !values["no-e2e"],
102
+ install: !values["no-install"],
103
+ force: Boolean(values.force),
104
+ dryRun: Boolean(values["dry-run"]),
105
+ json: Boolean(values.json),
106
+ help: Boolean(values.help),
107
+ };
108
+ }
@@ -0,0 +1,31 @@
1
+ const DOCS_URL = "https://wxn0brp.github.io/ValtheraDB/dev/adapter";
2
+ export function printPlan(meta, plan) {
3
+ console.log(`Package: ${meta.name} (${meta.keyword})`);
4
+ console.log(`Target: ${meta.dir}`);
5
+ console.log(`Files (${plan.length}):`);
6
+ for (const file of plan)
7
+ console.log(` ${file.path}`);
8
+ }
9
+ export function printReport(meta, result, install) {
10
+ console.log(`Created ${result.files.length} files in ${result.dir}`);
11
+ console.log("");
12
+ console.log("Next steps:");
13
+ if (!install)
14
+ console.log(` cd ${meta.dir} && bun install`);
15
+ console.log(" bun run build");
16
+ console.log(" bun test");
17
+ console.log(" valthera-e2e");
18
+ console.log("");
19
+ console.log(`Docs: ${DOCS_URL}`);
20
+ }
21
+ export function reportJson(meta, plan, result) {
22
+ return JSON.stringify({
23
+ meta,
24
+ files: plan.map(file => file.path),
25
+ ...(result
26
+ ? {
27
+ dir: result.dir,
28
+ }
29
+ : {}),
30
+ }, null, 2);
31
+ }
@@ -0,0 +1,84 @@
1
+ import { checkbox, input, select } from "@inquirer/prompts";
2
+ import { assertValidName } from "../naming.js";
3
+ import { VARIANT_LIST, variant } from "../variants.js";
4
+ export function isInteractive() {
5
+ return Boolean(process.stdin.isTTY && process.stdout.isTTY);
6
+ }
7
+ function isCancel(err) {
8
+ return err instanceof Error && err.name === "ExitPromptError";
9
+ }
10
+ function validateName(value) {
11
+ try {
12
+ assertValidName(value.trim());
13
+ return true;
14
+ }
15
+ catch (err) {
16
+ return err.message;
17
+ }
18
+ }
19
+ export async function runWizard(args) {
20
+ try {
21
+ const variantId = args.variant ??
22
+ (await select({
23
+ message: "Adapter variant",
24
+ choices: VARIANT_LIST.map(spec => ({
25
+ value: spec.id,
26
+ name: spec.label,
27
+ description: spec.description,
28
+ })),
29
+ }));
30
+ const name = args.name ??
31
+ (await input({
32
+ message: "Adapter name:",
33
+ validate: validateName,
34
+ }));
35
+ const spec = variant(variantId);
36
+ const defaultDir = `./db-${spec.kind}-${name}`;
37
+ const dir = args.dir ??
38
+ (await input({
39
+ message: "Target directory",
40
+ default: defaultDir,
41
+ }));
42
+ const features = await checkbox({
43
+ message: "Extras",
44
+ choices: [
45
+ {
46
+ value: "github",
47
+ name: "GitHub Actions workflow",
48
+ description: "Build on push",
49
+ checked: args.github,
50
+ },
51
+ {
52
+ value: "e2e",
53
+ name: "valthera-e2e suite",
54
+ description: "Compliance tests",
55
+ checked: args.e2e,
56
+ },
57
+ {
58
+ value: "install",
59
+ name: "Install dependencies",
60
+ description: "Run bun install",
61
+ checked: args.install,
62
+ },
63
+ ],
64
+ required: false,
65
+ });
66
+ return {
67
+ answers: {
68
+ variant: variantId,
69
+ name,
70
+ dir,
71
+ github: features.includes("github"),
72
+ e2e: features.includes("e2e"),
73
+ },
74
+ install: features.includes("install"),
75
+ };
76
+ }
77
+ catch (err) {
78
+ if (isCancel(err)) {
79
+ console.log("Cancelled.");
80
+ process.exit(0);
81
+ }
82
+ throw err;
83
+ }
84
+ }
@@ -0,0 +1,31 @@
1
+ import { mkdir, readdir } from "node:fs/promises";
2
+ import { dirname, join, resolve } from "node:path";
3
+ export async function isEmptyDir(dir) {
4
+ try {
5
+ const entries = await readdir(dir);
6
+ return entries.length === 0;
7
+ }
8
+ catch (err) {
9
+ if (err?.code === "ENOENT")
10
+ return true;
11
+ throw err;
12
+ }
13
+ }
14
+ export async function writePlan(dir, plan, opts = {}) {
15
+ const target = resolve(dir);
16
+ if (!(await isEmptyDir(target)) && !opts.force)
17
+ throw new Error(`Target directory "${dir}" is not empty. Use --force to overwrite.`);
18
+ const files = [];
19
+ for (const file of plan) {
20
+ const full = join(target, file.path);
21
+ await mkdir(dirname(full), {
22
+ recursive: true,
23
+ });
24
+ await Bun.write(full, file.content);
25
+ files.push(full);
26
+ }
27
+ return {
28
+ dir: target,
29
+ files,
30
+ };
31
+ }
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export { buildPlan } from "./plan.js";
2
+ export { writePlan, isEmptyDir } from "./generate.js";
3
+ export { resolveOptions } from "./options.js";
4
+ export { run } from "./run.js";
5
+ export { packageMeta, repoBaseName, templateVars, } from "./package-meta.js";
6
+ export { renderTemplates, substitute, templatesDir, } from "./template-engine.js";
7
+ export { VARIANTS, VARIANT_LIST, isVariantId, variant, } from "./variants.js";
8
+ export { assertValidName, adapterVersion, camelCase, coreMinor, detectCoreRange, nitUser, pascalCase, } from "./naming.js";
package/dist/naming.js ADDED
@@ -0,0 +1,51 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { userInfo } from "node:os";
3
+ import { dirname, join, resolve } from "node:path";
4
+ export const DEFAULT_CORE_RANGE = "^0.13.0";
5
+ export function nitUser() {
6
+ return process.env.NIT_USER || userInfo().username;
7
+ }
8
+ const NAME_RE = /^[a-z0-9][a-z0-9-]*$/;
9
+ export function assertValidName(name) {
10
+ if (!NAME_RE.test(name))
11
+ throw new Error(`Invalid adapter name "${name}": use lowercase letters, digits and dashes`);
12
+ }
13
+ export function pascalCase(name) {
14
+ return name
15
+ .split(/[-_]+/)
16
+ .filter(Boolean)
17
+ .map(part => part[0].toUpperCase() + part.slice(1))
18
+ .join("");
19
+ }
20
+ export function camelCase(name) {
21
+ const pascal = pascalCase(name);
22
+ return pascal[0].toLowerCase() + pascal.slice(1);
23
+ }
24
+ export function coreMinor(coreRange) {
25
+ const match = /\d+\.(\d+)\./.exec(coreRange);
26
+ if (!match)
27
+ throw new Error(`Cannot parse core minor from "${coreRange}"`);
28
+ return Number(match[1]);
29
+ }
30
+ export function adapterVersion(coreRange) {
31
+ return `0.${coreMinor(coreRange)}0.0`;
32
+ }
33
+ export function detectCoreRange(cwd = process.cwd()) {
34
+ let dir = resolve(cwd);
35
+ while (true) {
36
+ const pkgPath = join(dir, "node_modules", "@wxn0brp", "db-core", "package.json");
37
+ if (existsSync(pkgPath)) {
38
+ try {
39
+ const version = JSON.parse(readFileSync(pkgPath, "utf-8"))
40
+ .version;
41
+ if (version)
42
+ return `^${version}`;
43
+ }
44
+ catch { }
45
+ }
46
+ const parent = dirname(dir);
47
+ if (parent === dir)
48
+ return DEFAULT_CORE_RANGE;
49
+ dir = parent;
50
+ }
51
+ }
@@ -0,0 +1,34 @@
1
+ import { assertValidName, detectCoreRange, nitUser } from "./naming.js";
2
+ import { isVariantId, variant } from "./variants.js";
3
+ export function resolveOptions(args, answers = {}) {
4
+ const variantId = answers.variant ?? args.variant;
5
+ if (!variantId)
6
+ throw new Error("Missing adapter variant. Use --variant a|b|c|d.");
7
+ if (!isVariantId(variantId))
8
+ throw new Error(`Unknown variant "${variantId}". Use a, b, c or d.`);
9
+ const name = (answers.name ?? args.name ?? "").trim();
10
+ assertValidName(name);
11
+ const spec = variant(variantId);
12
+ const author = answers.author ?? args.author ?? nitUser();
13
+ const scope = answers.scope ?? args.scope ?? `@${nitUser().toLowerCase()}`;
14
+ const coreRange = answers.core ?? args.core ?? detectCoreRange();
15
+ const repoBase = `ValtheraDB-${spec.kind}-${name}`;
16
+ const repo = answers.repo ?? `https://github.com/${author}/${repoBase}.git`;
17
+ const homepage = answers.homepage ?? repo.replace(/\.git$/, "");
18
+ const description = answers.desc ??
19
+ `ValtheraDB ${spec.kind === "spec" ? "specialized" : "storage"} adapter for ${name}`;
20
+ return {
21
+ variant: variantId,
22
+ name,
23
+ scope,
24
+ dir: answers.dir ?? args.dir ?? `./db-${spec.kind}-${name}`,
25
+ coreRange,
26
+ description,
27
+ author,
28
+ repo,
29
+ homepage,
30
+ github: answers.github ?? args.github,
31
+ e2e: answers.e2e ?? args.e2e,
32
+ licenseYear: String(new Date().getFullYear()),
33
+ };
34
+ }
@@ -0,0 +1,46 @@
1
+ import { adapterVersion, camelCase, pascalCase } from "./naming.js";
2
+ import { variant } from "./variants.js";
3
+ export function repoBaseName(opts) {
4
+ return `ValtheraDB-${variant(opts.variant).kind}-${opts.name}`;
5
+ }
6
+ export function packageMeta(opts) {
7
+ const spec = variant(opts.variant);
8
+ const pascal = pascalCase(opts.name);
9
+ const camel = camelCase(opts.name);
10
+ const scope = opts.scope ? `${opts.scope.replace(/\/+$/, "")}/` : "";
11
+ return {
12
+ name: `${scope}db-${spec.kind}-${opts.name}`,
13
+ kind: spec.kind,
14
+ keyword: spec.keyword,
15
+ version: adapterVersion(opts.coreRange),
16
+ repo: opts.repo,
17
+ homepage: opts.homepage,
18
+ description: opts.description,
19
+ dir: opts.dir,
20
+ pascal,
21
+ camel,
22
+ factory: `create${pascal}Adapter`,
23
+ valtheraFactory: `create${pascal}Valthera`,
24
+ };
25
+ }
26
+ export function templateVars(opts) {
27
+ const meta = packageMeta(opts);
28
+ return {
29
+ name: opts.name,
30
+ package: meta.name,
31
+ pascal: meta.pascal,
32
+ camel: meta.camel,
33
+ scope: opts.scope,
34
+ version: meta.version,
35
+ core: opts.coreRange,
36
+ keyword: meta.keyword,
37
+ kind: meta.kind,
38
+ repo: meta.repo,
39
+ homepage: meta.homepage,
40
+ author: opts.author,
41
+ year: opts.licenseYear,
42
+ description: meta.description,
43
+ factory: meta.factory,
44
+ valtheraFactory: meta.valtheraFactory,
45
+ };
46
+ }
package/dist/plan.js ADDED
@@ -0,0 +1,36 @@
1
+ import { join } from "node:path";
2
+ import { assertValidName } from "./naming.js";
3
+ import { templateVars } from "./package-meta.js";
4
+ import { renderTemplates, templatesDir } from "./template-engine.js";
5
+ import { variant } from "./variants.js";
6
+ const TOKEN_RE = /___[A-Za-z][A-Za-z0-9]*___/;
7
+ export function buildPlan(opts) {
8
+ assertValidName(opts.name);
9
+ const spec = variant(opts.variant);
10
+ const dirs = [
11
+ join(templatesDir(), "common"),
12
+ join(templatesDir(), spec.id),
13
+ ];
14
+ const exclude = [
15
+ ...(!opts.github
16
+ ? [
17
+ ".github/",
18
+ ]
19
+ : []),
20
+ ...(!opts.e2e
21
+ ? [
22
+ "valthera-e2e/",
23
+ ]
24
+ : []),
25
+ ];
26
+ const plan = renderTemplates(dirs, templateVars(opts), {
27
+ exclude,
28
+ });
29
+ for (const file of plan) {
30
+ if (file.path.startsWith("/") || file.path.split("/").includes(".."))
31
+ throw new Error(`Unsafe file path in plan: ${file.path}`);
32
+ if (TOKEN_RE.test(file.content))
33
+ throw new Error(`Unresolved template token in ${file.path}`);
34
+ }
35
+ return plan;
36
+ }
package/dist/run.js ADDED
@@ -0,0 +1,60 @@
1
+ import { getArgs, printHelp } from "./cli/args.js";
2
+ import { printPlan, printReport, reportJson } from "./cli/output.js";
3
+ import { isInteractive, runWizard } from "./cli/tui.js";
4
+ import { writePlan } from "./generate.js";
5
+ import { resolveOptions } from "./options.js";
6
+ import { packageMeta } from "./package-meta.js";
7
+ import { buildPlan } from "./plan.js";
8
+ export async function run(argv = process.argv.slice(2)) {
9
+ const args = getArgs(argv);
10
+ if (args.help) {
11
+ printHelp();
12
+ return;
13
+ }
14
+ let answers = {};
15
+ let install = args.install && !args.json;
16
+ const useWizard = isInteractive() && !args.json && !(args.variant && args.name);
17
+ if (useWizard) {
18
+ const wizard = await runWizard(args);
19
+ answers = wizard.answers;
20
+ install = wizard.install;
21
+ }
22
+ else {
23
+ if (!args.variant)
24
+ throw new Error("Missing --variant. Use a, b, c or d (or run interactively).");
25
+ if (!args.name)
26
+ throw new Error("Missing --name.");
27
+ }
28
+ const opts = resolveOptions(args, answers);
29
+ const meta = packageMeta(opts);
30
+ const plan = buildPlan(opts);
31
+ if (args.dryRun) {
32
+ if (args.json)
33
+ console.log(reportJson(meta, plan));
34
+ else
35
+ printPlan(meta, plan);
36
+ return;
37
+ }
38
+ const result = await writePlan(opts.dir, plan, {
39
+ force: args.force,
40
+ });
41
+ if (args.json) {
42
+ console.log(reportJson(meta, plan, result));
43
+ }
44
+ else {
45
+ printReport(meta, result, install);
46
+ }
47
+ if (install) {
48
+ console.log("");
49
+ console.log("Running bun install...");
50
+ const proc = Bun.spawn([
51
+ "bun",
52
+ "install",
53
+ ], {
54
+ cwd: result.dir,
55
+ stdout: "inherit",
56
+ stderr: "inherit",
57
+ });
58
+ await proc.exited;
59
+ }
60
+ }
@@ -0,0 +1,48 @@
1
+ import { readdirSync, readFileSync, statSync } from "node:fs";
2
+ import { join, relative, sep } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ const TOKEN_RE = /___([A-Za-z][A-Za-z0-9]*)___/g;
5
+ export function templatesDir() {
6
+ return fileURLToPath(new URL("../templates/", import.meta.url));
7
+ }
8
+ function walk(dir, base = dir) {
9
+ const found = [];
10
+ for (const entry of readdirSync(dir)) {
11
+ const full = join(dir, entry);
12
+ if (statSync(full).isDirectory()) {
13
+ found.push(...walk(full, base));
14
+ }
15
+ else {
16
+ found.push(relative(base, full).split(sep).join("/"));
17
+ }
18
+ }
19
+ return found;
20
+ }
21
+ export function substitute(content, vars) {
22
+ return content.replace(TOKEN_RE, (raw, token) => {
23
+ if (!Object.hasOwn(vars, token))
24
+ throw new Error(`Unknown template token ${raw}`);
25
+ return vars[token];
26
+ });
27
+ }
28
+ export function renderTemplates(dirs, vars, opts = {}) {
29
+ const files = new Map();
30
+ for (const dir of dirs)
31
+ for (const source of walk(dir)) {
32
+ const path = source.endsWith(".tmpl")
33
+ ? source.slice(0, -".tmpl".length)
34
+ : source;
35
+ files.set(path, substitute(readFileSync(join(dir, source), "utf-8"), vars));
36
+ }
37
+ const exclude = opts.exclude ?? [];
38
+ const plan = [];
39
+ for (const [path, content] of files) {
40
+ if (exclude.some(prefix => path.startsWith(prefix)))
41
+ continue;
42
+ plan.push({
43
+ path,
44
+ content,
45
+ });
46
+ }
47
+ return plan.sort((a, b) => a.path.localeCompare(b.path));
48
+ }
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,40 @@
1
+ export const VARIANTS = {
2
+ a: {
3
+ id: "a",
4
+ kind: "storage",
5
+ keyword: "vdb-adapter",
6
+ label: "A - CustomFileCpu, implement read/write only",
7
+ description: "Whole-file storage, CRUD provided by CustomFileCpu.",
8
+ },
9
+ b: {
10
+ id: "b",
11
+ kind: "storage",
12
+ keyword: "vdb-adapter",
13
+ label: "B - AdapterBase + custom FileCpu",
14
+ description: "Custom file format via the FileCpu interface.",
15
+ },
16
+ c: {
17
+ id: "c",
18
+ kind: "storage",
19
+ keyword: "vdb-adapter",
20
+ label: "C - AdapterBase, full CRUD (SQL/NoSQL/API)",
21
+ description: "Translate every operation to an external backend.",
22
+ },
23
+ d: {
24
+ id: "d",
25
+ kind: "spec",
26
+ keyword: "vdb-spec",
27
+ label: "D - specialized adapter with partial CRUD",
28
+ description: "Non-document data model with declared partial coverage.",
29
+ },
30
+ };
31
+ export const VARIANT_LIST = Object.values(VARIANTS);
32
+ export function isVariantId(value) {
33
+ return Object.hasOwn(VARIANTS, value);
34
+ }
35
+ export function variant(id) {
36
+ const spec = VARIANTS[id];
37
+ if (!spec)
38
+ throw new Error(`Unknown variant "${id}"`);
39
+ return spec;
40
+ }