@intentius/chant 0.0.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.
Files changed (271) hide show
  1. package/README.md +365 -0
  2. package/package.json +22 -0
  3. package/src/attrref.test.ts +148 -0
  4. package/src/attrref.ts +50 -0
  5. package/src/barrel.test.ts +157 -0
  6. package/src/barrel.ts +101 -0
  7. package/src/bench.test.ts +227 -0
  8. package/src/build.test.ts +437 -0
  9. package/src/build.ts +425 -0
  10. package/src/builder.test.ts +312 -0
  11. package/src/builder.ts +56 -0
  12. package/src/child-project.ts +44 -0
  13. package/src/cli/commands/__fixtures__/init-lexicon-output/README.md +26 -0
  14. package/src/cli/commands/__fixtures__/init-lexicon-output/docs/astro.config.mjs +14 -0
  15. package/src/cli/commands/__fixtures__/init-lexicon-output/docs/package.json +16 -0
  16. package/src/cli/commands/__fixtures__/init-lexicon-output/docs/src/content/docs/index.mdx +8 -0
  17. package/src/cli/commands/__fixtures__/init-lexicon-output/docs/src/content.config.ts +7 -0
  18. package/src/cli/commands/__fixtures__/init-lexicon-output/docs/tsconfig.json +10 -0
  19. package/src/cli/commands/__fixtures__/init-lexicon-output/examples/getting-started/.gitkeep +0 -0
  20. package/src/cli/commands/__fixtures__/init-lexicon-output/justfile +26 -0
  21. package/src/cli/commands/__fixtures__/init-lexicon-output/package.json +29 -0
  22. package/src/cli/commands/__fixtures__/init-lexicon-output/src/codegen/docs.ts +25 -0
  23. package/src/cli/commands/__fixtures__/init-lexicon-output/src/codegen/generate-cli.ts +8 -0
  24. package/src/cli/commands/__fixtures__/init-lexicon-output/src/codegen/generate.ts +74 -0
  25. package/src/cli/commands/__fixtures__/init-lexicon-output/src/codegen/naming.ts +33 -0
  26. package/src/cli/commands/__fixtures__/init-lexicon-output/src/codegen/package.ts +25 -0
  27. package/src/cli/commands/__fixtures__/init-lexicon-output/src/codegen/rollback.ts +45 -0
  28. package/src/cli/commands/__fixtures__/init-lexicon-output/src/coverage.ts +11 -0
  29. package/src/cli/commands/__fixtures__/init-lexicon-output/src/generated/.gitkeep +0 -0
  30. package/src/cli/commands/__fixtures__/init-lexicon-output/src/import/generator.ts +10 -0
  31. package/src/cli/commands/__fixtures__/init-lexicon-output/src/import/parser.ts +10 -0
  32. package/src/cli/commands/__fixtures__/init-lexicon-output/src/index.ts +9 -0
  33. package/src/cli/commands/__fixtures__/init-lexicon-output/src/lint/rules/index.ts +1 -0
  34. package/src/cli/commands/__fixtures__/init-lexicon-output/src/lint/rules/sample.ts +18 -0
  35. package/src/cli/commands/__fixtures__/init-lexicon-output/src/lsp/completions.ts +14 -0
  36. package/src/cli/commands/__fixtures__/init-lexicon-output/src/lsp/hover.ts +14 -0
  37. package/src/cli/commands/__fixtures__/init-lexicon-output/src/plugin.ts +110 -0
  38. package/src/cli/commands/__fixtures__/init-lexicon-output/src/serializer.ts +24 -0
  39. package/src/cli/commands/__fixtures__/init-lexicon-output/src/spec/fetch.ts +21 -0
  40. package/src/cli/commands/__fixtures__/init-lexicon-output/src/spec/parse.ts +25 -0
  41. package/src/cli/commands/__fixtures__/init-lexicon-output/src/validate-cli.ts +4 -0
  42. package/src/cli/commands/__fixtures__/init-lexicon-output/src/validate.ts +24 -0
  43. package/src/cli/commands/__fixtures__/init-lexicon-output/tsconfig.json +10 -0
  44. package/src/cli/commands/__fixtures__/sample-rule.ts +11 -0
  45. package/src/cli/commands/__snapshots__/init-lexicon.test.ts.snap +222 -0
  46. package/src/cli/commands/build.test.ts +149 -0
  47. package/src/cli/commands/build.ts +344 -0
  48. package/src/cli/commands/diff.test.ts +148 -0
  49. package/src/cli/commands/diff.ts +221 -0
  50. package/src/cli/commands/doctor.test.ts +239 -0
  51. package/src/cli/commands/doctor.ts +224 -0
  52. package/src/cli/commands/import.test.ts +379 -0
  53. package/src/cli/commands/import.ts +335 -0
  54. package/src/cli/commands/init-lexicon.test.ts +297 -0
  55. package/src/cli/commands/init-lexicon.ts +993 -0
  56. package/src/cli/commands/init.test.ts +317 -0
  57. package/src/cli/commands/init.ts +505 -0
  58. package/src/cli/commands/licenses.ts +165 -0
  59. package/src/cli/commands/lint.test.ts +332 -0
  60. package/src/cli/commands/lint.ts +408 -0
  61. package/src/cli/commands/list.test.ts +100 -0
  62. package/src/cli/commands/list.ts +108 -0
  63. package/src/cli/commands/update.test.ts +38 -0
  64. package/src/cli/commands/update.ts +207 -0
  65. package/src/cli/conflict-check.test.ts +255 -0
  66. package/src/cli/conflict-check.ts +89 -0
  67. package/src/cli/debug.ts +8 -0
  68. package/src/cli/format.test.ts +140 -0
  69. package/src/cli/format.ts +133 -0
  70. package/src/cli/handlers/build.ts +58 -0
  71. package/src/cli/handlers/dev.ts +38 -0
  72. package/src/cli/handlers/init.ts +46 -0
  73. package/src/cli/handlers/lint.ts +36 -0
  74. package/src/cli/handlers/misc.ts +57 -0
  75. package/src/cli/handlers/serve.ts +26 -0
  76. package/src/cli/index.ts +3 -0
  77. package/src/cli/lsp/capabilities.ts +46 -0
  78. package/src/cli/lsp/diagnostics.ts +52 -0
  79. package/src/cli/lsp/server.test.ts +618 -0
  80. package/src/cli/lsp/server.ts +393 -0
  81. package/src/cli/main.test.ts +257 -0
  82. package/src/cli/main.ts +224 -0
  83. package/src/cli/mcp/resources/context.ts +59 -0
  84. package/src/cli/mcp/server.test.ts +747 -0
  85. package/src/cli/mcp/server.ts +402 -0
  86. package/src/cli/mcp/tools/build.ts +117 -0
  87. package/src/cli/mcp/tools/import.ts +48 -0
  88. package/src/cli/mcp/tools/lint.ts +45 -0
  89. package/src/cli/plugins.test.ts +31 -0
  90. package/src/cli/plugins.ts +94 -0
  91. package/src/cli/registry.ts +73 -0
  92. package/src/cli/reporters/stylish.test.ts +282 -0
  93. package/src/cli/reporters/stylish.ts +186 -0
  94. package/src/cli/watch.test.ts +81 -0
  95. package/src/cli/watch.ts +101 -0
  96. package/src/codegen/case.test.ts +30 -0
  97. package/src/codegen/case.ts +11 -0
  98. package/src/codegen/coverage.ts +167 -0
  99. package/src/codegen/docs.ts +634 -0
  100. package/src/codegen/fetch.test.ts +119 -0
  101. package/src/codegen/fetch.ts +261 -0
  102. package/src/codegen/generate-registry.test.ts +118 -0
  103. package/src/codegen/generate-registry.ts +107 -0
  104. package/src/codegen/generate-runtime-index.test.ts +81 -0
  105. package/src/codegen/generate-runtime-index.ts +99 -0
  106. package/src/codegen/generate-typescript.test.ts +146 -0
  107. package/src/codegen/generate-typescript.ts +161 -0
  108. package/src/codegen/generate.ts +206 -0
  109. package/src/codegen/json-patch.test.ts +113 -0
  110. package/src/codegen/json-patch.ts +151 -0
  111. package/src/codegen/json-schema.test.ts +196 -0
  112. package/src/codegen/json-schema.ts +209 -0
  113. package/src/codegen/naming.ts +201 -0
  114. package/src/codegen/package.ts +161 -0
  115. package/src/codegen/rollback.test.ts +92 -0
  116. package/src/codegen/rollback.ts +115 -0
  117. package/src/codegen/topo-sort.test.ts +69 -0
  118. package/src/codegen/topo-sort.ts +46 -0
  119. package/src/codegen/typecheck.test.ts +37 -0
  120. package/src/codegen/typecheck.ts +74 -0
  121. package/src/codegen/validate.test.ts +86 -0
  122. package/src/codegen/validate.ts +143 -0
  123. package/src/composite.test.ts +426 -0
  124. package/src/composite.ts +243 -0
  125. package/src/config.test.ts +91 -0
  126. package/src/config.ts +87 -0
  127. package/src/declarable.test.ts +160 -0
  128. package/src/declarable.ts +47 -0
  129. package/src/detectLexicon.test.ts +236 -0
  130. package/src/detectLexicon.ts +37 -0
  131. package/src/discovery/cache.test.ts +78 -0
  132. package/src/discovery/cache.ts +86 -0
  133. package/src/discovery/collect.test.ts +269 -0
  134. package/src/discovery/collect.ts +51 -0
  135. package/src/discovery/cycles.test.ts +238 -0
  136. package/src/discovery/cycles.ts +107 -0
  137. package/src/discovery/files.test.ts +154 -0
  138. package/src/discovery/files.ts +61 -0
  139. package/src/discovery/graph.test.ts +476 -0
  140. package/src/discovery/graph.ts +150 -0
  141. package/src/discovery/import.test.ts +199 -0
  142. package/src/discovery/import.ts +20 -0
  143. package/src/discovery/index.test.ts +272 -0
  144. package/src/discovery/index.ts +132 -0
  145. package/src/discovery/resolve.test.ts +267 -0
  146. package/src/discovery/resolve.ts +54 -0
  147. package/src/errors.test.ts +138 -0
  148. package/src/errors.ts +86 -0
  149. package/src/import/base-parser.test.ts +67 -0
  150. package/src/import/base-parser.ts +48 -0
  151. package/src/import/generator.ts +21 -0
  152. package/src/import/ir-utils.test.ts +103 -0
  153. package/src/import/ir-utils.ts +87 -0
  154. package/src/import/parser.ts +41 -0
  155. package/src/index.ts +60 -0
  156. package/src/intrinsic-interpolation.test.ts +91 -0
  157. package/src/intrinsic-interpolation.ts +89 -0
  158. package/src/intrinsic.test.ts +69 -0
  159. package/src/intrinsic.ts +43 -0
  160. package/src/lexicon-integrity.test.ts +94 -0
  161. package/src/lexicon-integrity.ts +69 -0
  162. package/src/lexicon-manifest.test.ts +101 -0
  163. package/src/lexicon-manifest.ts +71 -0
  164. package/src/lexicon-output.test.ts +182 -0
  165. package/src/lexicon-output.ts +82 -0
  166. package/src/lexicon-schema.test.ts +239 -0
  167. package/src/lexicon-schema.ts +144 -0
  168. package/src/lexicon.ts +212 -0
  169. package/src/lint/config-overrides.test.ts +254 -0
  170. package/src/lint/config.test.ts +644 -0
  171. package/src/lint/config.ts +375 -0
  172. package/src/lint/declarative.test.ts +256 -0
  173. package/src/lint/declarative.ts +187 -0
  174. package/src/lint/engine.test.ts +465 -0
  175. package/src/lint/engine.ts +172 -0
  176. package/src/lint/named-checks.test.ts +37 -0
  177. package/src/lint/named-checks.ts +33 -0
  178. package/src/lint/parser.test.ts +129 -0
  179. package/src/lint/parser.ts +42 -0
  180. package/src/lint/post-synth.test.ts +113 -0
  181. package/src/lint/post-synth.ts +76 -0
  182. package/src/lint/presets/relaxed.json +19 -0
  183. package/src/lint/presets/strict.json +19 -0
  184. package/src/lint/rule-loader.test.ts +67 -0
  185. package/src/lint/rule-loader.ts +67 -0
  186. package/src/lint/rule-options.test.ts +141 -0
  187. package/src/lint/rule.test.ts +196 -0
  188. package/src/lint/rule.ts +98 -0
  189. package/src/lint/rules/barrel-import-style.test.ts +80 -0
  190. package/src/lint/rules/barrel-import-style.ts +59 -0
  191. package/src/lint/rules/composite-scope.ts +55 -0
  192. package/src/lint/rules/cor017-composite-name-match.test.ts +107 -0
  193. package/src/lint/rules/cor017-composite-name-match.ts +108 -0
  194. package/src/lint/rules/cor018-composite-prefer-lexicon-type.test.ts +172 -0
  195. package/src/lint/rules/cor018-composite-prefer-lexicon-type.ts +167 -0
  196. package/src/lint/rules/declarable-naming-convention.test.ts +69 -0
  197. package/src/lint/rules/declarable-naming-convention.ts +70 -0
  198. package/src/lint/rules/enforce-barrel-import.test.ts +169 -0
  199. package/src/lint/rules/enforce-barrel-import.ts +81 -0
  200. package/src/lint/rules/enforce-barrel-ref.test.ts +114 -0
  201. package/src/lint/rules/enforce-barrel-ref.ts +75 -0
  202. package/src/lint/rules/evl001-non-literal-expression.test.ts +158 -0
  203. package/src/lint/rules/evl001-non-literal-expression.ts +149 -0
  204. package/src/lint/rules/evl002-control-flow-resource.test.ts +110 -0
  205. package/src/lint/rules/evl002-control-flow-resource.ts +61 -0
  206. package/src/lint/rules/evl003-dynamic-property-access.test.ts +63 -0
  207. package/src/lint/rules/evl003-dynamic-property-access.ts +41 -0
  208. package/src/lint/rules/evl004-spread-non-const.test.ts +130 -0
  209. package/src/lint/rules/evl004-spread-non-const.ts +111 -0
  210. package/src/lint/rules/evl005-resource-block-body.test.ts +59 -0
  211. package/src/lint/rules/evl005-resource-block-body.ts +49 -0
  212. package/src/lint/rules/evl006-barrel-usage.test.ts +63 -0
  213. package/src/lint/rules/evl006-barrel-usage.ts +95 -0
  214. package/src/lint/rules/evl007-invalid-siblings.test.ts +87 -0
  215. package/src/lint/rules/evl007-invalid-siblings.ts +139 -0
  216. package/src/lint/rules/evl008-unresolvable-barrel-ref.test.ts +118 -0
  217. package/src/lint/rules/evl008-unresolvable-barrel-ref.ts +140 -0
  218. package/src/lint/rules/evl009-composite-no-constant.test.ts +162 -0
  219. package/src/lint/rules/evl009-composite-no-constant.ts +171 -0
  220. package/src/lint/rules/evl010-composite-no-transform.test.ts +121 -0
  221. package/src/lint/rules/evl010-composite-no-transform.ts +69 -0
  222. package/src/lint/rules/export-required.test.ts +213 -0
  223. package/src/lint/rules/export-required.ts +158 -0
  224. package/src/lint/rules/file-declarable-limit.test.ts +148 -0
  225. package/src/lint/rules/file-declarable-limit.ts +96 -0
  226. package/src/lint/rules/flat-declarations.test.ts +210 -0
  227. package/src/lint/rules/flat-declarations.ts +70 -0
  228. package/src/lint/rules/index.ts +99 -0
  229. package/src/lint/rules/no-cyclic-declarable-ref.test.ts +135 -0
  230. package/src/lint/rules/no-cyclic-declarable-ref.ts +178 -0
  231. package/src/lint/rules/no-redundant-type-import.test.ts +129 -0
  232. package/src/lint/rules/no-redundant-type-import.ts +85 -0
  233. package/src/lint/rules/no-redundant-value-cast.test.ts +51 -0
  234. package/src/lint/rules/no-redundant-value-cast.ts +46 -0
  235. package/src/lint/rules/no-string-ref.test.ts +100 -0
  236. package/src/lint/rules/no-string-ref.ts +66 -0
  237. package/src/lint/rules/no-unused-declarable-import.test.ts +74 -0
  238. package/src/lint/rules/no-unused-declarable-import.ts +103 -0
  239. package/src/lint/rules/no-unused-declarable.test.ts +134 -0
  240. package/src/lint/rules/no-unused-declarable.ts +118 -0
  241. package/src/lint/rules/prefer-namespace-import.test.ts +102 -0
  242. package/src/lint/rules/prefer-namespace-import.ts +63 -0
  243. package/src/lint/rules/single-concern-file.test.ts +156 -0
  244. package/src/lint/rules/single-concern-file.ts +98 -0
  245. package/src/lint/rules/stale-barrel-types.ts +60 -0
  246. package/src/lint/selectors.test.ts +113 -0
  247. package/src/lint/selectors.ts +188 -0
  248. package/src/lsp/lexicon-providers.ts +191 -0
  249. package/src/lsp/types.ts +79 -0
  250. package/src/mcp/types.ts +22 -0
  251. package/src/project/scan.test.ts +178 -0
  252. package/src/project/scan.ts +182 -0
  253. package/src/project/sync.test.ts +87 -0
  254. package/src/project/sync.ts +46 -0
  255. package/src/project-validation.test.ts +64 -0
  256. package/src/project-validation.ts +79 -0
  257. package/src/pseudo-parameter.test.ts +39 -0
  258. package/src/pseudo-parameter.ts +47 -0
  259. package/src/runtime.ts +68 -0
  260. package/src/serializer-walker.test.ts +124 -0
  261. package/src/serializer-walker.ts +83 -0
  262. package/src/serializer.ts +42 -0
  263. package/src/sort.test.ts +290 -0
  264. package/src/sort.ts +58 -0
  265. package/src/stack-output.ts +82 -0
  266. package/src/types.test.ts +307 -0
  267. package/src/types.ts +46 -0
  268. package/src/utils.test.ts +195 -0
  269. package/src/utils.ts +46 -0
  270. package/src/validation.test.ts +308 -0
  271. package/src/validation.ts +50 -0
@@ -0,0 +1,224 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import { resolve } from "node:path";
4
+ import { formatSuccess, formatError } from "./format";
5
+ import { loadPlugins, resolveProjectLexicons } from "./plugins";
6
+ import { resolveCommand, type CommandDef, type ParsedArgs } from "./registry";
7
+ import { runBuild } from "./handlers/build";
8
+ import { runLint } from "./handlers/lint";
9
+ import { runDevGenerate, runDevPublish, runDevRollback, runDevUnknown } from "./handlers/dev";
10
+ import { runServeLsp, runServeMcp, runServeUnknown } from "./handlers/serve";
11
+ import { runInit, runInitLexicon } from "./handlers/init";
12
+ import { runList, runImport, runUpdate, runDoctor } from "./handlers/misc";
13
+
14
+ /**
15
+ * Parse command line arguments
16
+ */
17
+ export function parseArgs(args: string[]): ParsedArgs {
18
+ const result: ParsedArgs = {
19
+ command: "",
20
+ path: ".",
21
+ extraPositional: undefined,
22
+ extraPositional2: undefined,
23
+ output: undefined,
24
+ format: "",
25
+ force: undefined,
26
+ fix: false,
27
+ lexicon: undefined,
28
+ watch: false,
29
+ verbose: false,
30
+ help: false,
31
+ };
32
+
33
+ let i = 0;
34
+ while (i < args.length) {
35
+ const arg = args[i];
36
+
37
+ if (arg === "--help" || arg === "-h") {
38
+ result.help = true;
39
+ } else if (arg === "--output" || arg === "-o") {
40
+ result.output = args[++i];
41
+ } else if (arg === "--format" || arg === "-f") {
42
+ result.format = args[++i];
43
+ } else if (arg === "--lexicon" || arg === "-d") {
44
+ result.lexicon = args[++i];
45
+ } else if (arg === "--force") {
46
+ result.force = true;
47
+ } else if (arg === "--fix") {
48
+ result.fix = true;
49
+ } else if (arg === "--watch" || arg === "-w") {
50
+ result.watch = true;
51
+ } else if (arg === "--verbose" || arg === "-v") {
52
+ result.verbose = true;
53
+ } else if (!arg.startsWith("-")) {
54
+ if (!result.command) {
55
+ result.command = arg;
56
+ } else if (result.path === ".") {
57
+ result.path = arg;
58
+ } else if (!result.extraPositional) {
59
+ result.extraPositional = arg;
60
+ } else {
61
+ result.extraPositional2 = arg;
62
+ }
63
+ }
64
+
65
+ i++;
66
+ }
67
+
68
+ return result;
69
+ }
70
+
71
+ /**
72
+ * Print help message
73
+ */
74
+ function printHelp(): void {
75
+ console.log(`
76
+ chant - Declarative infrastructure specification toolkit
77
+
78
+ Usage:
79
+ chant <command> [options] [path]
80
+
81
+ Commands:
82
+ init Initialize a new chant project
83
+ init lexicon <name> Scaffold a new lexicon plugin project
84
+ build Build infrastructure from specification files
85
+ lint Check specifications for issues
86
+ list List discovered entities
87
+ import Import external template into TypeScript
88
+
89
+ Lexicon development:
90
+ dev generate Generate lexicon artifacts (+ validate + coverage)
91
+ dev publish Package lexicon for distribution
92
+ dev rollback List or restore generation snapshots
93
+
94
+ Servers:
95
+ serve lsp Start the LSP server (stdio)
96
+ serve mcp Start the MCP server (stdio)
97
+
98
+ Project:
99
+ update Sync lexicon types into .chant/types/
100
+ doctor Check project health and configuration
101
+
102
+ Options:
103
+ -o, --output <file> Write output to file instead of stdout
104
+ -f, --format <fmt> Output format (command-specific):
105
+ - build: json (default) or yaml
106
+ - list: text (default) or json
107
+ - lint: stylish (default), json, or sarif
108
+ -d, --lexicon <name> Build only the specified lexicon (e.g. aws, gitlab)
109
+ --fix Auto-fix fixable issues (lint command)
110
+ --force Force overwrite existing files (import command)
111
+ -w, --watch Watch for changes and rebuild/re-lint (build, lint)
112
+ -v, --verbose Show stack traces on errors
113
+ -h, --help Show this help message
114
+
115
+ Examples:
116
+ chant build ./infra/
117
+ chant build ./infra/ --output stack.json
118
+ chant build ./infra/ --format yaml
119
+ chant build ./infra/ --watch
120
+ chant import template.json --output ./infra/
121
+ chant lint ./infra/
122
+ chant lint ./infra/ --format sarif
123
+ chant lint ./infra/ --watch
124
+ chant list ./infra/
125
+ chant list ./infra/ --format json
126
+ `);
127
+ }
128
+
129
+ /**
130
+ * Load lexicon plugins for the given project path, or exit with an error.
131
+ */
132
+ async function loadPluginsOrExit(path: string): Promise<import("../lexicon").LexiconPlugin[]> {
133
+ let plugins;
134
+ try {
135
+ const lexiconNames = await resolveProjectLexicons(resolve(path));
136
+ plugins = await loadPlugins(lexiconNames);
137
+ } catch (error) {
138
+ const errorMessage = error instanceof Error ? error.message : String(error);
139
+ console.error(formatError({ message: errorMessage }));
140
+ process.exit(1);
141
+ }
142
+
143
+ if (plugins.length === 0) {
144
+ console.error(formatError({
145
+ message: "No lexicon detected",
146
+ hint: 'Run "chant init --lexicon <name>" to initialize a project, or add a lexicon to chant.config.ts',
147
+ }));
148
+ process.exit(1);
149
+ }
150
+
151
+ return plugins;
152
+ }
153
+
154
+ // ── Command registry ──────────────────────────────────────────────
155
+
156
+ const registry: CommandDef[] = [
157
+ // Primary commands
158
+ { name: "build", requiresPlugins: true, handler: runBuild },
159
+ { name: "lint", handler: runLint },
160
+ { name: "list", handler: runList },
161
+ { name: "import", handler: runImport },
162
+ { name: "init", handler: runInit },
163
+ { name: "init lexicon", handler: runInitLexicon },
164
+ { name: "update", handler: runUpdate },
165
+ { name: "doctor", handler: runDoctor },
166
+
167
+ // Dev subcommands
168
+ { name: "dev generate", requiresPlugins: true, handler: runDevGenerate },
169
+ { name: "dev publish", requiresPlugins: true, handler: runDevPublish },
170
+ { name: "dev rollback", requiresPlugins: true, handler: runDevRollback },
171
+
172
+ // Serve subcommands
173
+ { name: "serve lsp", requiresPlugins: true, handler: runServeLsp },
174
+ { name: "serve mcp", requiresPlugins: true, handler: runServeMcp },
175
+
176
+ // Fallback for unknown subcommands (must come after compound entries)
177
+ { name: "dev", handler: runDevUnknown },
178
+ { name: "serve", handler: runServeUnknown },
179
+ ];
180
+
181
+ /**
182
+ * Main entry point
183
+ */
184
+ async function main(): Promise<void> {
185
+ const args = parseArgs(process.argv.slice(2));
186
+
187
+ if (args.help || !args.command) {
188
+ printHelp();
189
+ process.exit(args.help ? 0 : 1);
190
+ }
191
+
192
+ const match = resolveCommand(args, registry);
193
+ if (!match) {
194
+ console.error(formatError({
195
+ message: `Unknown command: ${args.command}`,
196
+ hint: 'Run "chant --help" to see available commands',
197
+ }));
198
+ process.exit(1);
199
+ }
200
+
201
+ // For compound commands (e.g. "dev generate"), args.path is the subcommand,
202
+ // so the project path shifts to extraPositional. For simple commands, use args.path.
203
+ const projectPath = match.compound ? (args.extraPositional ?? ".") : args.path;
204
+ const plugins = match.def.requiresPlugins
205
+ ? await loadPluginsOrExit(projectPath)
206
+ : [];
207
+ const serializers = plugins.map((p) => p.serializer);
208
+ const ctx = { args, plugins, serializers };
209
+
210
+ process.exit(await match.def.handler(ctx));
211
+ }
212
+
213
+ // Only run main when executed directly, not when imported
214
+ if (import.meta.main) {
215
+ main().catch((err) => {
216
+ const verbose = process.argv.includes("--verbose") || process.argv.includes("-v");
217
+ if (verbose && err instanceof Error && err.stack) {
218
+ console.error(err.stack);
219
+ } else {
220
+ console.error(formatError({ message: err instanceof Error ? err.message : String(err) }));
221
+ }
222
+ process.exit(1);
223
+ });
224
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Get the chant context for MCP
3
+ * Returns lexicon-specific instructions and patterns
4
+ */
5
+ export function getContext(): string {
6
+ return `# chant Development Context
7
+
8
+ chant is a lexicon-agnostic declarative specification system for infrastructure as code.
9
+
10
+ ## Key Concepts
11
+
12
+ ### Declarable
13
+ Base interface for all infrastructure entities. Every resource you create must implement \`Declarable\`.
14
+
15
+ ### Lexicon
16
+ Lexicons define target platforms (e.g. AWS). Each lexicon:
17
+ - Has a unique name and rule prefix
18
+ - Implements \`serialize(entities)\` to generate platform-specific output
19
+
20
+ ### AttrRef
21
+ Deferred references to entity attributes. Use these for cross-resource references:
22
+ \`\`\`typescript
23
+ const bucket = new Bucket({ name: "data" });
24
+ // bucket.arn is an AttrRef that resolves at build time
25
+ \`\`\`
26
+
27
+ ### Intrinsics
28
+ Lexicon-provided functions resolved at build time:
29
+ - \`Sub\` - String interpolation with references
30
+ - \`Ref\` - Reference to another resource
31
+ - \`Json\` - JSON serialization
32
+
33
+ ## CLI Commands
34
+
35
+ ### Build
36
+ \`\`\`bash
37
+ chant build ./infra/ # Build infrastructure
38
+ chant build ./infra/ -o out.json # Output to file
39
+ \`\`\`
40
+
41
+ ### Lint
42
+ \`\`\`bash
43
+ chant lint ./infra/ # Check for issues
44
+ \`\`\`
45
+
46
+ ### Import
47
+ \`\`\`bash
48
+ chant import template.json # Convert external template
49
+ chant import template.json -o ./src/ # Custom output dir
50
+ \`\`\`
51
+
52
+ ## Best Practices
53
+
54
+ 1. **Flat Declarations**: Keep declarations at module level, avoid deep nesting
55
+ 2. **Use AttrRefs**: Reference other resources via AttrRef, not string interpolation
56
+ 3. **Lexicon-specific Rules**: Each lexicon has lint rules (WAW001, etc.)
57
+ 4. **Type Safety**: Leverage TypeScript for infrastructure definitions
58
+ `;
59
+ }