litedbmodel-gen 0.1.3 → 0.2.1

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.
package/cli-contract.yaml CHANGED
@@ -1,9 +1,9 @@
1
1
  # yaml-language-server: $schema=./node_modules/cli-contracts/schemas/cli-contract.schema.json
2
- cliContracts: 0.1.0
2
+ cli_contracts: 0.1.0
3
3
 
4
4
  info:
5
5
  title: litedbmodel-gen CLI
6
- version: 0.1.2
6
+ version: 0.2.0
7
7
  description: >-
8
8
  embedoc-based model code generator for litedbmodel.
9
9
  Parses SQL DDL (PostgreSQL / MySQL / SQLite) and generates
@@ -14,12 +14,23 @@ info:
14
14
  name: foo-ogawa
15
15
  url: https://github.com/foo-ogawa/litedbmodel.ts
16
16
 
17
- commandSets:
17
+ artifact_slots:
18
+ sql-schemas:
19
+ direction: read
20
+ description: SQL DDL files for schema parsing
21
+ embedoc-config:
22
+ direction: readwrite
23
+ description: embedoc.config.yaml configuration (read and updated during init)
24
+ generated-models:
25
+ direction: write
26
+ description: Generated TypeScript model definitions
27
+
28
+ command_sets:
18
29
  litedbmodel-gen:
19
30
  summary: embedoc-based model code generator for litedbmodel.
20
31
  executable: litedbmodel-gen
21
32
 
22
- globalOptions:
33
+ global_options:
23
34
  - name: version
24
35
  aliases: [V]
25
36
  description: Print version and exit.
@@ -59,7 +70,7 @@ commandSets:
59
70
  file:
60
71
  mode: read
61
72
  exists: true
62
- mediaType: application/yaml
73
+ media_type: application/yaml
63
74
  encoding: utf-8
64
75
 
65
76
  exits:
@@ -73,7 +84,7 @@ commandSets:
73
84
  files:
74
85
  - path: .embedoc/templates/model.hbs
75
86
  required: false
76
- mediaType: text/x-handlebars-template
87
+ media_type: text/x-handlebars-template
77
88
  encoding: utf-8
78
89
  description: Starter Handlebars template for model files (skipped if already exists).
79
90
 
@@ -85,10 +96,10 @@ commandSets:
85
96
  format: text
86
97
 
87
98
  x-agent:
88
- riskLevel: low
89
- requiresConfirmation: false
99
+ risk_level: low
100
+ requires_confirmation: false
90
101
  idempotent: true
91
- sideEffects:
102
+ side_effects:
92
103
  - file_write
93
- recommendedBeforeUse:
104
+ recommended_before_use:
94
105
  - Run "npx embedoc init" first to create embedoc.config.yaml.
package/dist/cli.js CHANGED
@@ -1,11 +1,26 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/cli.ts
4
- import { Command } from "commander";
5
4
  import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync } from "fs";
6
5
  import { resolve, dirname, join } from "path";
7
6
  import { fileURLToPath } from "url";
7
+ import { createRequire } from "module";
8
8
  import yaml from "js-yaml";
9
+
10
+ // src/generated/program.ts
11
+ import { Command } from "commander";
12
+ function createProgram(handlers2, version) {
13
+ const program = new Command();
14
+ program.name("litedbmodel-gen").version(version, "-V, --version").description("embedoc-based model code generator for litedbmodel.");
15
+ program.command("init").description("Set up litedbmodel-gen in an embedoc project.").argument("[config]", "Path to embedoc.config.yaml. Defaults to embedoc.config.yaml in the current directory.").action(async (config, opts, cmd) => {
16
+ await handlers2.init(config, {}, cmd.optsWithGlobals());
17
+ });
18
+ return program;
19
+ }
20
+
21
+ // src/cli.ts
22
+ var require2 = createRequire(import.meta.url);
23
+ var pkg = require2("../package.json");
9
24
  var __filename = fileURLToPath(import.meta.url);
10
25
  var __dirname = dirname(__filename);
11
26
  function resolveEmbedocDirs(configPath) {
@@ -26,25 +41,26 @@ function resolveEmbedocDirs(configPath) {
26
41
  );
27
42
  return { renderersDir, datasourcesDir, templatesDir };
28
43
  }
29
- var program = new Command();
30
- program.name("litedbmodel-gen").description("embedoc-based model code generator for litedbmodel").version("0.1.0");
31
- program.command("init").description("Set up litedbmodel-gen in an embedoc project").argument("[config]", "path to embedoc.config.yaml", "embedoc.config.yaml").action((configPath) => {
32
- const absConfigPath = resolve(configPath);
33
- if (!existsSync(absConfigPath)) {
34
- console.error(`Error: ${configPath} not found. Run "npx embedoc init" first.`);
35
- process.exit(1);
44
+ var handlers = {
45
+ init: async (config) => {
46
+ const configPath = config ?? "embedoc.config.yaml";
47
+ const absConfigPath = resolve(configPath);
48
+ if (!existsSync(absConfigPath)) {
49
+ console.error(`Error: ${configPath} not found. Run "npx embedoc init" first.`);
50
+ process.exit(1);
51
+ }
52
+ const dirs = resolveEmbedocDirs(absConfigPath);
53
+ console.log("Setting up litedbmodel-gen...\n");
54
+ copyTemplate(dirs.templatesDir);
55
+ updateRenderersIndex(dirs.renderersDir);
56
+ updateDatasourcesIndex(dirs.datasourcesDir);
57
+ updateConfig(absConfigPath);
58
+ console.log("\nDone! Next steps:");
59
+ console.log(" 1. Edit embedoc.config.yaml \u2014 set path/database in the schema datasource");
60
+ console.log(" 2. npx embedoc generate --datasource schema (create model files)");
61
+ console.log(" 3. npx embedoc build (fill in column definitions)");
36
62
  }
37
- const dirs = resolveEmbedocDirs(absConfigPath);
38
- console.log("Setting up litedbmodel-gen...\n");
39
- copyTemplate(dirs.templatesDir);
40
- updateRenderersIndex(dirs.renderersDir);
41
- updateDatasourcesIndex(dirs.datasourcesDir);
42
- updateConfig(absConfigPath);
43
- console.log("\nDone! Next steps:");
44
- console.log(" 1. Edit embedoc.config.yaml \u2014 set path/database in the schema datasource");
45
- console.log(" 2. npx embedoc generate --datasource schema (create model files)");
46
- console.log(" 3. npx embedoc build (fill in column definitions)");
47
- });
63
+ };
48
64
  function copyTemplate(templatesDir) {
49
65
  mkdirSync(templatesDir, { recursive: true });
50
66
  const destPath = join(templatesDir, "model.hbs");
@@ -168,5 +184,5 @@ function updateConfig(configPath) {
168
184
  writeFileSync(configPath, content, "utf-8");
169
185
  console.log(" update embedoc.config.yaml");
170
186
  }
171
- program.parse();
187
+ createProgram(handlers, pkg.version).parse();
172
188
  //# sourceMappingURL=cli.js.map
package/dist/cli.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/cli.ts"],"sourcesContent":["import { Command } from 'commander';\nimport { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync } from 'fs';\nimport { resolve, dirname, join } from 'path';\nimport { fileURLToPath } from 'url';\nimport yaml from 'js-yaml';\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\ninterface EmbedocDirs {\n renderersDir: string;\n datasourcesDir: string;\n templatesDir: string;\n}\n\nfunction resolveEmbedocDirs(configPath: string): EmbedocDirs {\n const configDir = dirname(configPath);\n const raw = readFileSync(configPath, 'utf-8');\n const config = (yaml.load(raw) ?? {}) as Record<string, unknown>;\n\n const renderersDir = resolve(\n configDir,\n (config['renderers_dir'] as string) ?? '.embedoc/renderers',\n );\n const datasourcesDir = resolve(\n configDir,\n (config['datasources_dir'] as string) ?? '.embedoc/datasources',\n );\n const templatesDir = resolve(\n configDir,\n (config['templates_dir'] as string) ?? '.embedoc/templates',\n );\n\n return { renderersDir, datasourcesDir, templatesDir };\n}\n\nconst program = new Command();\n\nprogram\n .name('litedbmodel-gen')\n .description('embedoc-based model code generator for litedbmodel')\n .version('0.1.0');\n\nprogram\n .command('init')\n .description('Set up litedbmodel-gen in an embedoc project')\n .argument('[config]', 'path to embedoc.config.yaml', 'embedoc.config.yaml')\n .action((configPath: string) => {\n const absConfigPath = resolve(configPath);\n if (!existsSync(absConfigPath)) {\n console.error(`Error: ${configPath} not found. Run \"npx embedoc init\" first.`);\n process.exit(1);\n }\n\n const dirs = resolveEmbedocDirs(absConfigPath);\n\n console.log('Setting up litedbmodel-gen...\\n');\n\n copyTemplate(dirs.templatesDir);\n updateRenderersIndex(dirs.renderersDir);\n updateDatasourcesIndex(dirs.datasourcesDir);\n updateConfig(absConfigPath);\n\n console.log('\\nDone! Next steps:');\n console.log(' 1. Edit embedoc.config.yaml — set path/database in the schema datasource');\n console.log(' 2. npx embedoc generate --datasource schema (create model files)');\n console.log(' 3. npx embedoc build (fill in column definitions)');\n });\n\nfunction copyTemplate(templatesDir: string): void {\n mkdirSync(templatesDir, { recursive: true });\n\n const destPath = join(templatesDir, 'model.hbs');\n if (existsSync(destPath)) {\n console.log(` skip ${destPath} (already exists)`);\n return;\n }\n\n const srcPath = resolvePackageTemplate();\n copyFileSync(srcPath, destPath);\n console.log(` create ${destPath}`);\n}\n\nfunction resolvePackageTemplate(): string {\n const candidates = [\n resolve(__dirname, '..', 'templates', 'model.hbs'),\n resolve(__dirname, 'templates', 'model.hbs'),\n ];\n for (const p of candidates) {\n if (existsSync(p)) return p;\n }\n throw new Error('Could not find model.hbs template in package');\n}\n\nfunction updateRenderersIndex(renderersDir: string): void {\n const filePath = join(renderersDir, 'index.ts');\n if (!existsSync(filePath)) {\n console.error(` warn ${filePath} not found, skipping renderers setup`);\n return;\n }\n\n let content = readFileSync(filePath, 'utf-8');\n if (content.includes('litedbmodel-gen')) {\n console.log(` skip ${filePath} (already configured)`);\n return;\n }\n\n const importLine = \"import { litedbmodelColumns } from 'litedbmodel-gen';\";\n\n const entry = ` litedbmodel_columns: {\n ...litedbmodelColumns,\n dependsOn: ['schema'],\n },`;\n\n if (content.includes('import ')) {\n const lastImportIdx = content.lastIndexOf('import ');\n const lineEnd = content.indexOf('\\n', lastImportIdx);\n content = content.slice(0, lineEnd + 1) + importLine + '\\n' + content.slice(lineEnd + 1);\n } else {\n content = importLine + '\\n\\n' + content;\n }\n\n content = insertIntoObject(content, 'embeds', entry);\n\n writeFileSync(filePath, content, 'utf-8');\n console.log(` update ${filePath}`);\n}\n\nfunction updateDatasourcesIndex(datasourcesDir: string): void {\n const filePath = join(datasourcesDir, 'index.ts');\n if (!existsSync(filePath)) {\n console.error(` warn ${filePath} not found, skipping datasources setup`);\n return;\n }\n\n let content = readFileSync(filePath, 'utf-8');\n if (content.includes('litedbmodel-gen')) {\n console.log(` skip ${filePath} (already configured)`);\n return;\n }\n\n const importLine = \"import { sqlSchema } from 'litedbmodel-gen';\";\n\n const entry = ' sql_schema: sqlSchema,';\n\n if (content.includes('import ')) {\n const lastImportIdx = content.lastIndexOf('import ');\n const lineEnd = content.indexOf('\\n', lastImportIdx);\n content = content.slice(0, lineEnd + 1) + importLine + '\\n' + content.slice(lineEnd + 1);\n } else {\n content = importLine + '\\n\\n' + content;\n }\n\n content = insertIntoObject(content, 'datasourceTypes', entry);\n\n writeFileSync(filePath, content, 'utf-8');\n console.log(` update ${filePath}`);\n}\n\nfunction insertIntoObject(source: string, objectName: string, entry: string): string {\n const pattern = new RegExp(`(export\\\\s+const\\\\s+${objectName}\\\\s*=\\\\s*\\\\{)([^}]*)(\\\\})`);\n const match = source.match(pattern);\n if (!match) return source;\n\n const [, opening, body, closing] = match;\n const trimmedBody = body.replace(/\\s+/g, ' ').trim();\n\n let newBody: string;\n if (trimmedBody === '' || trimmedBody.startsWith('//')) {\n newBody = `\\n${entry}\\n`;\n } else {\n newBody = body.trimEnd() + '\\n' + entry + '\\n';\n }\n\n return source.replace(pattern, `${opening}${newBody}${closing}`);\n}\n\nfunction updateConfig(configPath: string): void {\n let content = readFileSync(configPath, 'utf-8');\n\n if (content.includes('sql_schema')) {\n console.log(' skip embedoc.config.yaml (sql_schema already configured)');\n return;\n }\n\n const datasourceBlock = ` schema:\n type: sql_schema\n path: \"./db/schema.sql\"\n database: PostgreSQL\n generators:\n - output_path: \"./models/{model_class}.ts\"\n template: model.hbs\n overwrite: false`;\n\n if (content.includes('datasources: {}')) {\n content = content.replace('datasources: {}', 'datasources:\\n' + datasourceBlock);\n } else if (content.includes('datasources:')) {\n const idx = content.indexOf('datasources:');\n const lineEnd = content.indexOf('\\n', idx);\n content = content.slice(0, lineEnd + 1) + datasourceBlock + '\\n' + content.slice(lineEnd + 1);\n } else {\n content += '\\ndatasources:\\n' + datasourceBlock + '\\n';\n }\n\n if (!content.includes('models/**/*.ts')) {\n const targetsInsert = ` - pattern: \"./models/**/*.ts\"\n comment_style: block`;\n\n if (content.includes('targets:')) {\n const idx = content.indexOf('targets:');\n const lineEnd = content.indexOf('\\n', idx);\n content = content.slice(0, lineEnd + 1) + targetsInsert + '\\n' + content.slice(lineEnd + 1);\n }\n }\n\n writeFileSync(configPath, content, 'utf-8');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,IAAI,OAAO,QAAQ;AAAA,EAC5C;AAEA,SAAO,OAAO,QAAQ,SAAS,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,EAAE;AACjE;AAEA,SAAS,aAAa,YAA0B;AAC9C,MAAI,UAAU,aAAa,YAAY,OAAO;AAE9C,MAAI,QAAQ,SAAS,YAAY,GAAG;AAClC,YAAQ,IAAI,6DAA6D;AACzE;AAAA,EACF;AAEA,QAAM,kBAAkB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AASxB,MAAI,QAAQ,SAAS,iBAAiB,GAAG;AACvC,cAAU,QAAQ,QAAQ,mBAAmB,mBAAmB,eAAe;AAAA,EACjF,WAAW,QAAQ,SAAS,cAAc,GAAG;AAC3C,UAAM,MAAM,QAAQ,QAAQ,cAAc;AAC1C,UAAM,UAAU,QAAQ,QAAQ,MAAM,GAAG;AACzC,cAAU,QAAQ,MAAM,GAAG,UAAU,CAAC,IAAI,kBAAkB,OAAO,QAAQ,MAAM,UAAU,CAAC;AAAA,EAC9F,OAAO;AACL,eAAW,qBAAqB,kBAAkB;AAAA,EACpD;AAEA,MAAI,CAAC,QAAQ,SAAS,gBAAgB,GAAG;AACvC,UAAM,gBAAgB;AAAA;AAGtB,QAAI,QAAQ,SAAS,UAAU,GAAG;AAChC,YAAM,MAAM,QAAQ,QAAQ,UAAU;AACtC,YAAM,UAAU,QAAQ,QAAQ,MAAM,GAAG;AACzC,gBAAU,QAAQ,MAAM,GAAG,UAAU,CAAC,IAAI,gBAAgB,OAAO,QAAQ,MAAM,UAAU,CAAC;AAAA,IAC5F;AAAA,EACF;AAEA,gBAAc,YAAY,SAAS,OAAO;AAC1C,UAAQ,IAAI,+BAA+B;AAC7C;AAEA,QAAQ,MAAM;","names":[]}
1
+ {"version":3,"sources":["../src/cli.ts","../src/generated/program.ts"],"sourcesContent":["import { readFileSync, writeFileSync, existsSync, mkdirSync, copyFileSync } from 'fs';\nimport { resolve, dirname, join } from 'path';\nimport { fileURLToPath } from 'url';\nimport { createRequire } from 'module';\nimport yaml from 'js-yaml';\nimport { createProgram, type CommandHandlers } from './generated/program.js';\n\nconst require = createRequire(import.meta.url);\nconst pkg = require('../package.json') as { version: string };\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\n\ninterface EmbedocDirs {\n renderersDir: string;\n datasourcesDir: string;\n templatesDir: string;\n}\n\nfunction resolveEmbedocDirs(configPath: string): EmbedocDirs {\n const configDir = dirname(configPath);\n const raw = readFileSync(configPath, 'utf-8');\n const config = (yaml.load(raw) ?? {}) as Record<string, unknown>;\n\n const renderersDir = resolve(\n configDir,\n (config['renderers_dir'] as string) ?? '.embedoc/renderers',\n );\n const datasourcesDir = resolve(\n configDir,\n (config['datasources_dir'] as string) ?? '.embedoc/datasources',\n );\n const templatesDir = resolve(\n configDir,\n (config['templates_dir'] as string) ?? '.embedoc/templates',\n );\n\n return { renderersDir, datasourcesDir, templatesDir };\n}\n\nconst handlers: CommandHandlers = {\n init: async (config) => {\n const configPath = config ?? 'embedoc.config.yaml';\n const absConfigPath = resolve(configPath);\n if (!existsSync(absConfigPath)) {\n console.error(`Error: ${configPath} not found. Run \"npx embedoc init\" first.`);\n process.exit(1);\n }\n\n const dirs = resolveEmbedocDirs(absConfigPath);\n\n console.log('Setting up litedbmodel-gen...\\n');\n\n copyTemplate(dirs.templatesDir);\n updateRenderersIndex(dirs.renderersDir);\n updateDatasourcesIndex(dirs.datasourcesDir);\n updateConfig(absConfigPath);\n\n console.log('\\nDone! Next steps:');\n console.log(' 1. Edit embedoc.config.yaml — set path/database in the schema datasource');\n console.log(' 2. npx embedoc generate --datasource schema (create model files)');\n console.log(' 3. npx embedoc build (fill in column definitions)');\n },\n};\n\nfunction copyTemplate(templatesDir: string): void {\n mkdirSync(templatesDir, { recursive: true });\n\n const destPath = join(templatesDir, 'model.hbs');\n if (existsSync(destPath)) {\n console.log(` skip ${destPath} (already exists)`);\n return;\n }\n\n const srcPath = resolvePackageTemplate();\n copyFileSync(srcPath, destPath);\n console.log(` create ${destPath}`);\n}\n\nfunction resolvePackageTemplate(): string {\n const candidates = [\n resolve(__dirname, '..', 'templates', 'model.hbs'),\n resolve(__dirname, 'templates', 'model.hbs'),\n ];\n for (const p of candidates) {\n if (existsSync(p)) return p;\n }\n throw new Error('Could not find model.hbs template in package');\n}\n\nfunction updateRenderersIndex(renderersDir: string): void {\n const filePath = join(renderersDir, 'index.ts');\n if (!existsSync(filePath)) {\n console.error(` warn ${filePath} not found, skipping renderers setup`);\n return;\n }\n\n let content = readFileSync(filePath, 'utf-8');\n if (content.includes('litedbmodel-gen')) {\n console.log(` skip ${filePath} (already configured)`);\n return;\n }\n\n const importLine = \"import { litedbmodelColumns } from 'litedbmodel-gen';\";\n\n const entry = ` litedbmodel_columns: {\n ...litedbmodelColumns,\n dependsOn: ['schema'],\n },`;\n\n if (content.includes('import ')) {\n const lastImportIdx = content.lastIndexOf('import ');\n const lineEnd = content.indexOf('\\n', lastImportIdx);\n content = content.slice(0, lineEnd + 1) + importLine + '\\n' + content.slice(lineEnd + 1);\n } else {\n content = importLine + '\\n\\n' + content;\n }\n\n content = insertIntoObject(content, 'embeds', entry);\n\n writeFileSync(filePath, content, 'utf-8');\n console.log(` update ${filePath}`);\n}\n\nfunction updateDatasourcesIndex(datasourcesDir: string): void {\n const filePath = join(datasourcesDir, 'index.ts');\n if (!existsSync(filePath)) {\n console.error(` warn ${filePath} not found, skipping datasources setup`);\n return;\n }\n\n let content = readFileSync(filePath, 'utf-8');\n if (content.includes('litedbmodel-gen')) {\n console.log(` skip ${filePath} (already configured)`);\n return;\n }\n\n const importLine = \"import { sqlSchema } from 'litedbmodel-gen';\";\n\n const entry = ' sql_schema: sqlSchema,';\n\n if (content.includes('import ')) {\n const lastImportIdx = content.lastIndexOf('import ');\n const lineEnd = content.indexOf('\\n', lastImportIdx);\n content = content.slice(0, lineEnd + 1) + importLine + '\\n' + content.slice(lineEnd + 1);\n } else {\n content = importLine + '\\n\\n' + content;\n }\n\n content = insertIntoObject(content, 'datasourceTypes', entry);\n\n writeFileSync(filePath, content, 'utf-8');\n console.log(` update ${filePath}`);\n}\n\nfunction insertIntoObject(source: string, objectName: string, entry: string): string {\n const pattern = new RegExp(`(export\\\\s+const\\\\s+${objectName}\\\\s*=\\\\s*\\\\{)([^}]*)(\\\\})`);\n const match = source.match(pattern);\n if (!match) return source;\n\n const [, opening, body, closing] = match;\n const trimmedBody = body.replace(/\\s+/g, ' ').trim();\n\n let newBody: string;\n if (trimmedBody === '' || trimmedBody.startsWith('//')) {\n newBody = `\\n${entry}\\n`;\n } else {\n newBody = body.trimEnd() + '\\n' + entry + '\\n';\n }\n\n return source.replace(pattern, `${opening}${newBody}${closing}`);\n}\n\nfunction updateConfig(configPath: string): void {\n let content = readFileSync(configPath, 'utf-8');\n\n if (content.includes('sql_schema')) {\n console.log(' skip embedoc.config.yaml (sql_schema already configured)');\n return;\n }\n\n const datasourceBlock = ` schema:\n type: sql_schema\n path: \"./db/schema.sql\"\n database: PostgreSQL\n generators:\n - output_path: \"./models/{model_class}.ts\"\n template: model.hbs\n overwrite: false`;\n\n if (content.includes('datasources: {}')) {\n content = content.replace('datasources: {}', 'datasources:\\n' + datasourceBlock);\n } else if (content.includes('datasources:')) {\n const idx = content.indexOf('datasources:');\n const lineEnd = content.indexOf('\\n', idx);\n content = content.slice(0, lineEnd + 1) + datasourceBlock + '\\n' + content.slice(lineEnd + 1);\n } else {\n content += '\\ndatasources:\\n' + datasourceBlock + '\\n';\n }\n\n if (!content.includes('models/**/*.ts')) {\n const targetsInsert = ` - pattern: \"./models/**/*.ts\"\n comment_style: block`;\n\n if (content.includes('targets:')) {\n const idx = content.indexOf('targets:');\n const lineEnd = content.indexOf('\\n', idx);\n content = content.slice(0, lineEnd + 1) + targetsInsert + '\\n' + content.slice(lineEnd + 1);\n }\n }\n\n writeFileSync(configPath, content, 'utf-8');\n console.log(' update embedoc.config.yaml');\n}\n\ncreateProgram(handlers, pkg.version).parse();\n","// Auto-generated by cli-contracts. Do not edit.\nimport { Command } from \"commander\";\n\nexport interface CommandHandlers {\n init: (config: string | undefined, options: Record<string, never>, parentOpts: Record<string, unknown>) => Promise<void>;\n}\n\nexport function createProgram(\n handlers: CommandHandlers,\n version: string,\n): Command {\n const program = new Command();\n program\n .name(\"litedbmodel-gen\")\n .version(version, \"-V, --version\")\n .description(\"embedoc-based model code generator for litedbmodel.\");\n\n\n program\n .command(\"init\")\n .description(\"Set up litedbmodel-gen in an embedoc project.\")\n .argument(\"[config]\", \"Path to embedoc.config.yaml. Defaults to embedoc.config.yaml in the current directory.\")\n .action(async (config, opts, cmd) => {\n await handlers.init(config, {}, cmd.optsWithGlobals());\n });\n\n return program;\n}\n"],"mappings":";;;AAAA,SAAS,cAAc,eAAe,YAAY,WAAW,oBAAoB;AACjF,SAAS,SAAS,SAAS,YAAY;AACvC,SAAS,qBAAqB;AAC9B,SAAS,qBAAqB;AAC9B,OAAO,UAAU;;;ACHjB,SAAS,eAAe;AAMjB,SAAS,cACdA,WACA,SACS;AACT,QAAM,UAAU,IAAI,QAAQ;AAC5B,UACG,KAAK,iBAAiB,EACtB,QAAQ,SAAS,eAAe,EAChC,YAAY,qDAAqD;AAGpE,UACG,QAAQ,MAAM,EACd,YAAY,+CAA+C,EAC3D,SAAS,YAAY,wFAAwF,EAC7G,OAAO,OAAO,QAAQ,MAAM,QAAQ;AACnC,UAAMA,UAAS,KAAK,QAAQ,CAAC,GAAG,IAAI,gBAAgB,CAAC;AAAA,EACvD,CAAC;AAEH,SAAO;AACT;;;ADpBA,IAAMC,WAAU,cAAc,YAAY,GAAG;AAC7C,IAAM,MAAMA,SAAQ,iBAAiB;AAErC,IAAM,aAAa,cAAc,YAAY,GAAG;AAChD,IAAM,YAAY,QAAQ,UAAU;AAQpC,SAAS,mBAAmB,YAAiC;AAC3D,QAAM,YAAY,QAAQ,UAAU;AACpC,QAAM,MAAM,aAAa,YAAY,OAAO;AAC5C,QAAM,SAAU,KAAK,KAAK,GA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AAU,QAAQ,QAAQ,MAAM,GAAG;AACzC,cAAU,QAAQ,MAAM,GAAG,UAAU,CAAC,IAAI,kBAAkB,OAAO,QAAQ,MAAM,UAAU,CAAC;AAAA,EAC9F,OAAO;AACL,eAAW,qBAAqB,kBAAkB;AAAA,EACpD;AAEA,MAAI,CAAC,QAAQ,SAAS,gBAAgB,GAAG;AACvC,UAAM,gBAAgB;AAAA;AAGtB,QAAI,QAAQ,SAAS,UAAU,GAAG;AAChC,YAAM,MAAM,QAAQ,QAAQ,UAAU;AACtC,YAAM,UAAU,QAAQ,QAAQ,MAAM,GAAG;AACzC,gBAAU,QAAQ,MAAM,GAAG,UAAU,CAAC,IAAI,gBAAgB,OAAO,QAAQ,MAAM,UAAU,CAAC;AAAA,IAC5F;AAAA,EACF;AAEA,gBAAc,YAAY,SAAS,OAAO;AAC1C,UAAQ,IAAI,+BAA+B;AAC7C;AAEA,cAAc,UAAU,IAAI,OAAO,EAAE,MAAM;","names":["handlers","require"]}
@@ -2,7 +2,7 @@
2
2
 
3
3
  embedoc-based model code generator for litedbmodel. Parses SQL DDL (PostgreSQL / MySQL / SQLite) and generates TypeScript column definitions that stay in sync with your schema.
4
4
 
5
- **Version:** 0.1.2
5
+ **Version:** 0.2.0
6
6
 
7
7
  ## Table of Contents
8
8
 
@@ -60,12 +60,12 @@ litedbmodel-gen init path/to/embedoc.config.yaml
60
60
 
61
61
  ```yaml
62
62
  x-agent:
63
- riskLevel: low
64
- requiresConfirmation: false
63
+ risk_level: low
64
+ requires_confirmation: false
65
65
  idempotent: true
66
- sideEffects:
66
+ side_effects:
67
67
  - file_write
68
- recommendedBeforeUse:
68
+ recommended_before_use:
69
69
  - Run "npx embedoc init" first to create embedoc.config.yaml.
70
70
  ```
71
71
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "litedbmodel-gen",
3
- "version": "0.1.3",
3
+ "version": "0.2.1",
4
4
  "description": "embedoc-based model code generator for litedbmodel — generates TypeScript column definitions from SQL DDL (PostgreSQL/MySQL/SQLite)",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -51,7 +51,7 @@
51
51
  "devDependencies": {
52
52
  "@types/js-yaml": "^4.0.9",
53
53
  "@types/node": "^25.3.3",
54
- "cli-contracts": "^0.7.2",
54
+ "cli-contracts": "^0.30.2",
55
55
  "embedoc": "^0.11.0",
56
56
  "tsup": "^8.0.1",
57
57
  "typescript": "^5.3.0",