@constructive-io/graphql-codegen 2.32.0 → 3.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 (270) hide show
  1. package/README.md +429 -1691
  2. package/cli/index.d.ts +5 -2
  3. package/cli/index.js +98 -581
  4. package/cli/shared.d.ts +35 -0
  5. package/cli/shared.js +106 -0
  6. package/{esm/cli → core}/codegen/barrel.d.ts +1 -1
  7. package/{cli → core}/codegen/barrel.js +1 -4
  8. package/{esm/cli → core}/codegen/index.d.ts +15 -5
  9. package/{cli → core}/codegen/index.js +44 -24
  10. package/{cli → core}/codegen/invalidation.d.ts +2 -2
  11. package/{esm/cli → core}/codegen/mutation-keys.d.ts +2 -2
  12. package/{cli → core}/codegen/orm/client-generator.js +2 -3
  13. package/{esm/cli → core}/codegen/orm/index.d.ts +9 -2
  14. package/{cli → core}/codegen/orm/index.js +3 -2
  15. package/{cli → core}/codegen/query-keys.d.ts +2 -2
  16. package/core/codegen/shared/index.d.ts +39 -0
  17. package/core/codegen/shared/index.js +118 -0
  18. package/core/config/index.d.ts +5 -0
  19. package/core/config/index.js +13 -0
  20. package/core/config/loader.d.ts +18 -0
  21. package/{cli/commands/init.js → core/config/loader.js} +7 -94
  22. package/core/config/resolver.d.ts +46 -0
  23. package/core/config/resolver.js +104 -0
  24. package/core/database/index.d.ts +43 -0
  25. package/core/database/index.js +85 -0
  26. package/core/generate.d.ts +22 -0
  27. package/core/generate.js +192 -0
  28. package/core/index.d.ts +13 -1
  29. package/core/index.js +22 -2
  30. package/{cli → core}/introspect/fetch-schema.js +58 -9
  31. package/core/introspect/source/api-schemas.d.ts +44 -0
  32. package/core/introspect/source/api-schemas.js +122 -0
  33. package/core/introspect/source/database.d.ts +32 -0
  34. package/core/introspect/source/database.js +91 -0
  35. package/core/introspect/source/index.d.ts +112 -0
  36. package/core/introspect/source/index.js +173 -0
  37. package/core/introspect/source/pgpm-module.d.ts +83 -0
  38. package/core/introspect/source/pgpm-module.js +200 -0
  39. package/core/output/index.d.ts +4 -0
  40. package/core/output/index.js +9 -0
  41. package/core/output/writer.d.ts +38 -0
  42. package/core/output/writer.js +156 -0
  43. package/{cli/commands/shared.d.ts → core/pipeline/index.d.ts} +5 -3
  44. package/{cli/commands/shared.js → core/pipeline/index.js} +4 -0
  45. package/{cli → core}/watch/orchestrator.d.ts +25 -3
  46. package/{cli → core}/watch/orchestrator.js +35 -27
  47. package/{cli → core}/watch/types.d.ts +1 -1
  48. package/esm/cli/index.d.ts +5 -2
  49. package/esm/cli/index.js +97 -547
  50. package/esm/cli/shared.d.ts +35 -0
  51. package/esm/cli/shared.js +101 -0
  52. package/{cli → esm/core}/codegen/barrel.d.ts +1 -1
  53. package/esm/{cli → core}/codegen/barrel.js +1 -4
  54. package/{cli → esm/core}/codegen/index.d.ts +15 -5
  55. package/esm/{cli → core}/codegen/index.js +44 -24
  56. package/esm/{cli → core}/codegen/invalidation.d.ts +2 -2
  57. package/{cli → esm/core}/codegen/mutation-keys.d.ts +2 -2
  58. package/esm/{cli → core}/codegen/orm/client-generator.js +2 -3
  59. package/{cli → esm/core}/codegen/orm/index.d.ts +9 -2
  60. package/esm/{cli → core}/codegen/orm/index.js +3 -2
  61. package/esm/{cli → core}/codegen/query-keys.d.ts +2 -2
  62. package/esm/core/codegen/shared/index.d.ts +39 -0
  63. package/esm/core/codegen/shared/index.js +79 -0
  64. package/esm/core/config/index.d.ts +5 -0
  65. package/esm/core/config/index.js +5 -0
  66. package/esm/core/config/loader.d.ts +18 -0
  67. package/esm/core/config/loader.js +71 -0
  68. package/esm/core/config/resolver.d.ts +46 -0
  69. package/esm/core/config/resolver.js +100 -0
  70. package/esm/core/database/index.d.ts +43 -0
  71. package/esm/core/database/index.js +48 -0
  72. package/esm/core/generate.d.ts +22 -0
  73. package/esm/core/generate.js +186 -0
  74. package/esm/core/index.d.ts +13 -1
  75. package/esm/core/index.js +20 -1
  76. package/esm/{cli → core}/introspect/fetch-schema.js +55 -9
  77. package/esm/core/introspect/source/api-schemas.d.ts +44 -0
  78. package/esm/core/introspect/source/api-schemas.js +117 -0
  79. package/esm/core/introspect/source/database.d.ts +32 -0
  80. package/esm/core/introspect/source/database.js +87 -0
  81. package/esm/core/introspect/source/index.d.ts +112 -0
  82. package/esm/core/introspect/source/index.js +154 -0
  83. package/esm/core/introspect/source/pgpm-module.d.ts +83 -0
  84. package/esm/core/introspect/source/pgpm-module.js +194 -0
  85. package/esm/core/output/index.d.ts +4 -0
  86. package/esm/core/output/index.js +4 -0
  87. package/esm/core/output/writer.d.ts +38 -0
  88. package/esm/core/output/writer.js +119 -0
  89. package/esm/{cli/commands/shared.d.ts → core/pipeline/index.d.ts} +5 -3
  90. package/esm/{cli/commands/shared.js → core/pipeline/index.js} +1 -0
  91. package/esm/{cli → core}/watch/orchestrator.d.ts +25 -3
  92. package/esm/{cli → core}/watch/orchestrator.js +35 -27
  93. package/esm/{cli → core}/watch/types.d.ts +1 -1
  94. package/esm/index.d.ts +8 -3
  95. package/esm/index.js +9 -3
  96. package/esm/types/config.d.ts +101 -138
  97. package/esm/types/config.js +8 -35
  98. package/esm/types/index.d.ts +2 -2
  99. package/esm/types/index.js +1 -1
  100. package/index.d.ts +8 -3
  101. package/index.js +18 -8
  102. package/package.json +18 -11
  103. package/types/config.d.ts +101 -138
  104. package/types/config.js +9 -38
  105. package/types/index.d.ts +2 -2
  106. package/types/index.js +3 -3
  107. package/cli/commands/generate-orm.d.ts +0 -53
  108. package/cli/commands/generate-orm.js +0 -292
  109. package/cli/commands/generate.d.ts +0 -66
  110. package/cli/commands/generate.js +0 -431
  111. package/cli/commands/index.d.ts +0 -9
  112. package/cli/commands/index.js +0 -14
  113. package/cli/commands/init.d.ts +0 -35
  114. package/cli/introspect/source/index.d.ts +0 -48
  115. package/cli/introspect/source/index.js +0 -72
  116. package/esm/cli/commands/generate-orm.d.ts +0 -53
  117. package/esm/cli/commands/generate-orm.js +0 -289
  118. package/esm/cli/commands/generate.d.ts +0 -66
  119. package/esm/cli/commands/generate.js +0 -393
  120. package/esm/cli/commands/index.d.ts +0 -9
  121. package/esm/cli/commands/index.js +0 -6
  122. package/esm/cli/commands/init.d.ts +0 -35
  123. package/esm/cli/commands/init.js +0 -158
  124. package/esm/cli/introspect/source/index.d.ts +0 -48
  125. package/esm/cli/introspect/source/index.js +0 -54
  126. /package/{cli → core}/codegen/babel-ast.d.ts +0 -0
  127. /package/{cli → core}/codegen/babel-ast.js +0 -0
  128. /package/{cli → core}/codegen/client.d.ts +0 -0
  129. /package/{cli → core}/codegen/client.js +0 -0
  130. /package/{cli → core}/codegen/custom-mutations.d.ts +0 -0
  131. /package/{cli → core}/codegen/custom-mutations.js +0 -0
  132. /package/{cli → core}/codegen/custom-queries.d.ts +0 -0
  133. /package/{cli → core}/codegen/custom-queries.js +0 -0
  134. /package/{cli → core}/codegen/gql-ast.d.ts +0 -0
  135. /package/{cli → core}/codegen/gql-ast.js +0 -0
  136. /package/{cli → core}/codegen/invalidation.js +0 -0
  137. /package/{cli → core}/codegen/mutation-keys.js +0 -0
  138. /package/{cli → core}/codegen/mutations.d.ts +0 -0
  139. /package/{cli → core}/codegen/mutations.js +0 -0
  140. /package/{cli → core}/codegen/orm/barrel.d.ts +0 -0
  141. /package/{cli → core}/codegen/orm/barrel.js +0 -0
  142. /package/{cli → core}/codegen/orm/client-generator.d.ts +0 -0
  143. /package/{cli → core}/codegen/orm/client.d.ts +0 -0
  144. /package/{cli → core}/codegen/orm/client.js +0 -0
  145. /package/{cli → core}/codegen/orm/custom-ops-generator.d.ts +0 -0
  146. /package/{cli → core}/codegen/orm/custom-ops-generator.js +0 -0
  147. /package/{cli → core}/codegen/orm/input-types-generator.d.ts +0 -0
  148. /package/{cli → core}/codegen/orm/input-types-generator.js +0 -0
  149. /package/{cli → core}/codegen/orm/model-generator.d.ts +0 -0
  150. /package/{cli → core}/codegen/orm/model-generator.js +0 -0
  151. /package/{cli → core}/codegen/orm/query-builder.d.ts +0 -0
  152. /package/{cli → core}/codegen/orm/query-builder.js +0 -0
  153. /package/{cli → core}/codegen/orm/query-builder.ts +0 -0
  154. /package/{cli → core}/codegen/orm/select-types.d.ts +0 -0
  155. /package/{cli → core}/codegen/orm/select-types.js +0 -0
  156. /package/{cli → core}/codegen/queries.d.ts +0 -0
  157. /package/{cli → core}/codegen/queries.js +0 -0
  158. /package/{cli → core}/codegen/query-keys.js +0 -0
  159. /package/{cli → core}/codegen/scalars.d.ts +0 -0
  160. /package/{cli → core}/codegen/scalars.js +0 -0
  161. /package/{cli → core}/codegen/schema-gql-ast.d.ts +0 -0
  162. /package/{cli → core}/codegen/schema-gql-ast.js +0 -0
  163. /package/{cli → core}/codegen/schema-types-generator.d.ts +0 -0
  164. /package/{cli → core}/codegen/schema-types-generator.js +0 -0
  165. /package/{cli → core}/codegen/type-resolver.d.ts +0 -0
  166. /package/{cli → core}/codegen/type-resolver.js +0 -0
  167. /package/{cli → core}/codegen/types.d.ts +0 -0
  168. /package/{cli → core}/codegen/types.js +0 -0
  169. /package/{cli → core}/codegen/utils.d.ts +0 -0
  170. /package/{cli → core}/codegen/utils.js +0 -0
  171. /package/{cli → core}/introspect/fetch-schema.d.ts +0 -0
  172. /package/{cli → core}/introspect/index.d.ts +0 -0
  173. /package/{cli → core}/introspect/index.js +0 -0
  174. /package/{cli → core}/introspect/infer-tables.d.ts +0 -0
  175. /package/{cli → core}/introspect/infer-tables.js +0 -0
  176. /package/{cli → core}/introspect/schema-query.d.ts +0 -0
  177. /package/{cli → core}/introspect/schema-query.js +0 -0
  178. /package/{cli → core}/introspect/source/endpoint.d.ts +0 -0
  179. /package/{cli → core}/introspect/source/endpoint.js +0 -0
  180. /package/{cli → core}/introspect/source/file.d.ts +0 -0
  181. /package/{cli → core}/introspect/source/file.js +0 -0
  182. /package/{cli → core}/introspect/source/types.d.ts +0 -0
  183. /package/{cli → core}/introspect/source/types.js +0 -0
  184. /package/{cli → core}/introspect/transform-schema.d.ts +0 -0
  185. /package/{cli → core}/introspect/transform-schema.js +0 -0
  186. /package/{cli → core}/introspect/transform.d.ts +0 -0
  187. /package/{cli → core}/introspect/transform.js +0 -0
  188. /package/{cli → core}/watch/cache.d.ts +0 -0
  189. /package/{cli → core}/watch/cache.js +0 -0
  190. /package/{cli → core}/watch/debounce.d.ts +0 -0
  191. /package/{cli → core}/watch/debounce.js +0 -0
  192. /package/{cli → core}/watch/hash.d.ts +0 -0
  193. /package/{cli → core}/watch/hash.js +0 -0
  194. /package/{cli → core}/watch/index.d.ts +0 -0
  195. /package/{cli → core}/watch/index.js +0 -0
  196. /package/{cli → core}/watch/poller.d.ts +0 -0
  197. /package/{cli → core}/watch/poller.js +0 -0
  198. /package/{cli → core}/watch/types.js +0 -0
  199. /package/esm/{cli → core}/codegen/babel-ast.d.ts +0 -0
  200. /package/esm/{cli → core}/codegen/babel-ast.js +0 -0
  201. /package/esm/{cli → core}/codegen/client.d.ts +0 -0
  202. /package/esm/{cli → core}/codegen/client.js +0 -0
  203. /package/esm/{cli → core}/codegen/custom-mutations.d.ts +0 -0
  204. /package/esm/{cli → core}/codegen/custom-mutations.js +0 -0
  205. /package/esm/{cli → core}/codegen/custom-queries.d.ts +0 -0
  206. /package/esm/{cli → core}/codegen/custom-queries.js +0 -0
  207. /package/esm/{cli → core}/codegen/gql-ast.d.ts +0 -0
  208. /package/esm/{cli → core}/codegen/gql-ast.js +0 -0
  209. /package/esm/{cli → core}/codegen/invalidation.js +0 -0
  210. /package/esm/{cli → core}/codegen/mutation-keys.js +0 -0
  211. /package/esm/{cli → core}/codegen/mutations.d.ts +0 -0
  212. /package/esm/{cli → core}/codegen/mutations.js +0 -0
  213. /package/esm/{cli → core}/codegen/orm/barrel.d.ts +0 -0
  214. /package/esm/{cli → core}/codegen/orm/barrel.js +0 -0
  215. /package/esm/{cli → core}/codegen/orm/client-generator.d.ts +0 -0
  216. /package/esm/{cli → core}/codegen/orm/client.d.ts +0 -0
  217. /package/esm/{cli → core}/codegen/orm/client.js +0 -0
  218. /package/esm/{cli → core}/codegen/orm/custom-ops-generator.d.ts +0 -0
  219. /package/esm/{cli → core}/codegen/orm/custom-ops-generator.js +0 -0
  220. /package/esm/{cli → core}/codegen/orm/input-types-generator.d.ts +0 -0
  221. /package/esm/{cli → core}/codegen/orm/input-types-generator.js +0 -0
  222. /package/esm/{cli → core}/codegen/orm/model-generator.d.ts +0 -0
  223. /package/esm/{cli → core}/codegen/orm/model-generator.js +0 -0
  224. /package/esm/{cli → core}/codegen/orm/query-builder.d.ts +0 -0
  225. /package/esm/{cli → core}/codegen/orm/query-builder.js +0 -0
  226. /package/esm/{cli → core}/codegen/orm/select-types.d.ts +0 -0
  227. /package/esm/{cli → core}/codegen/orm/select-types.js +0 -0
  228. /package/esm/{cli → core}/codegen/queries.d.ts +0 -0
  229. /package/esm/{cli → core}/codegen/queries.js +0 -0
  230. /package/esm/{cli → core}/codegen/query-keys.js +0 -0
  231. /package/esm/{cli → core}/codegen/scalars.d.ts +0 -0
  232. /package/esm/{cli → core}/codegen/scalars.js +0 -0
  233. /package/esm/{cli → core}/codegen/schema-gql-ast.d.ts +0 -0
  234. /package/esm/{cli → core}/codegen/schema-gql-ast.js +0 -0
  235. /package/esm/{cli → core}/codegen/schema-types-generator.d.ts +0 -0
  236. /package/esm/{cli → core}/codegen/schema-types-generator.js +0 -0
  237. /package/esm/{cli → core}/codegen/type-resolver.d.ts +0 -0
  238. /package/esm/{cli → core}/codegen/type-resolver.js +0 -0
  239. /package/esm/{cli → core}/codegen/types.d.ts +0 -0
  240. /package/esm/{cli → core}/codegen/types.js +0 -0
  241. /package/esm/{cli → core}/codegen/utils.d.ts +0 -0
  242. /package/esm/{cli → core}/codegen/utils.js +0 -0
  243. /package/esm/{cli → core}/introspect/fetch-schema.d.ts +0 -0
  244. /package/esm/{cli → core}/introspect/index.d.ts +0 -0
  245. /package/esm/{cli → core}/introspect/index.js +0 -0
  246. /package/esm/{cli → core}/introspect/infer-tables.d.ts +0 -0
  247. /package/esm/{cli → core}/introspect/infer-tables.js +0 -0
  248. /package/esm/{cli → core}/introspect/schema-query.d.ts +0 -0
  249. /package/esm/{cli → core}/introspect/schema-query.js +0 -0
  250. /package/esm/{cli → core}/introspect/source/endpoint.d.ts +0 -0
  251. /package/esm/{cli → core}/introspect/source/endpoint.js +0 -0
  252. /package/esm/{cli → core}/introspect/source/file.d.ts +0 -0
  253. /package/esm/{cli → core}/introspect/source/file.js +0 -0
  254. /package/esm/{cli → core}/introspect/source/types.d.ts +0 -0
  255. /package/esm/{cli → core}/introspect/source/types.js +0 -0
  256. /package/esm/{cli → core}/introspect/transform-schema.d.ts +0 -0
  257. /package/esm/{cli → core}/introspect/transform-schema.js +0 -0
  258. /package/esm/{cli → core}/introspect/transform.d.ts +0 -0
  259. /package/esm/{cli → core}/introspect/transform.js +0 -0
  260. /package/esm/{cli → core}/watch/cache.d.ts +0 -0
  261. /package/esm/{cli → core}/watch/cache.js +0 -0
  262. /package/esm/{cli → core}/watch/debounce.d.ts +0 -0
  263. /package/esm/{cli → core}/watch/debounce.js +0 -0
  264. /package/esm/{cli → core}/watch/hash.d.ts +0 -0
  265. /package/esm/{cli → core}/watch/hash.js +0 -0
  266. /package/esm/{cli → core}/watch/index.d.ts +0 -0
  267. /package/esm/{cli → core}/watch/index.js +0 -0
  268. /package/esm/{cli → core}/watch/poller.d.ts +0 -0
  269. /package/esm/{cli → core}/watch/poller.js +0 -0
  270. /package/esm/{cli → core}/watch/types.js +0 -0
package/esm/cli/index.js CHANGED
@@ -1,587 +1,137 @@
1
1
  #!/usr/bin/env node
2
2
  /**
3
3
  * CLI entry point for graphql-codegen
4
+ *
5
+ * This is a thin wrapper around the core generate() function.
6
+ * All business logic is in the core modules.
4
7
  */
5
- import { CLI, cliExitWithError, extractFirst, getPackageJson } from 'inquirerer';
6
- import { initCommand, findConfigFile, loadConfigFile } from './commands/init';
7
- import { generateReactQuery } from './commands/generate';
8
- import { generateOrm } from './commands/generate-orm';
9
- import { startWatch } from './watch';
10
- import { isMultiConfig, mergeConfig, resolveConfig, } from '../types/config';
11
- const usageText = `
12
- graphql-codegen - CLI for generating GraphQL SDK from PostGraphile endpoints or schema files
8
+ import { CLI, getPackageJson } from 'inquirerer';
9
+ import { generate } from '../core/generate';
10
+ import { findConfigFile, loadConfigFile } from '../core/config';
11
+ import { codegenQuestions, printResult } from './shared';
12
+ const usage = `
13
+ graphql-codegen - GraphQL SDK generator for Constructive databases
13
14
 
14
15
  Usage:
15
- graphql-codegen <command> [options]
16
+ graphql-codegen [options]
16
17
 
17
- Commands:
18
- init Initialize a new graphql-codegen configuration file
19
- generate Generate SDK from GraphQL endpoint or schema file
20
- generate-orm Generate Prisma-like ORM client from GraphQL endpoint or schema file
21
- introspect Introspect a GraphQL endpoint or schema file and print table info
18
+ Source Options (choose one):
19
+ -c, --config <path> Path to config file
20
+ -e, --endpoint <url> GraphQL endpoint URL
21
+ -s, --schema-file <path> Path to GraphQL schema file
22
22
 
23
- Options:
24
- --help, -h Show this help message
25
- --version, -v Show version number
23
+ Database Options:
24
+ --schemas <list> Comma-separated PostgreSQL schemas
25
+ --api-names <list> Comma-separated API names (mutually exclusive with --schemas)
26
26
 
27
- Run 'graphql-codegen <command> --help' for more information on a command.
28
- `;
29
- const initUsageText = `
30
- graphql-codegen init - Initialize a new graphql-codegen configuration file
31
-
32
- Usage:
33
- graphql-codegen init [options]
34
-
35
- Options:
36
- --directory, -d <dir> Target directory for the config file (default: .)
37
- --force, -f Force overwrite existing config
38
- --endpoint, -e <url> GraphQL endpoint URL to pre-populate
39
- --output, -o <dir> Output directory to pre-populate (default: ./generated)
40
- --help, -h Show this help message
41
- `;
42
- const generateUsageText = `
43
- graphql-codegen generate - Generate SDK from GraphQL endpoint or schema file
44
-
45
- Usage:
46
- graphql-codegen generate [options]
47
-
48
- Options:
49
- --config, -c <path> Path to config file
50
- --target, -t <name> Target name in config file
51
- --endpoint, -e <url> GraphQL endpoint URL (overrides config)
52
- --schema, -s <path> Path to GraphQL schema file (.graphql)
53
- --output, -o <dir> Output directory (overrides config)
54
- --authorization, -a <header> Authorization header value
55
- --verbose, -v Verbose output
56
- --dry-run Dry run - show what would be generated without writing files
57
- --watch, -w Watch mode - poll endpoint for schema changes (in-memory)
58
- --poll-interval <ms> Polling interval in milliseconds (default: 3000)
59
- --debounce <ms> Debounce delay before regenerating (default: 800)
60
- --touch <file> File to touch on schema change
61
- --no-clear Do not clear terminal on regeneration
62
- --help, -h Show this help message
63
- `;
64
- const generateOrmUsageText = `
65
- graphql-codegen generate-orm - Generate Prisma-like ORM client from GraphQL endpoint or schema file
66
-
67
- Usage:
68
- graphql-codegen generate-orm [options]
69
-
70
- Options:
71
- --config, -c <path> Path to config file
72
- --target, -t <name> Target name in config file
73
- --endpoint, -e <url> GraphQL endpoint URL (overrides config)
74
- --schema, -s <path> Path to GraphQL schema file (.graphql)
75
- --output, -o <dir> Output directory (overrides config)
76
- --authorization, -a <header> Authorization header value
77
- --verbose, -v Verbose output
78
- --dry-run Dry run - show what would be generated without writing files
79
- --skip-custom-operations Skip custom operations (only generate table CRUD)
80
- --watch, -w Watch mode - poll endpoint for schema changes (in-memory)
81
- --poll-interval <ms> Polling interval in milliseconds (default: 3000)
82
- --debounce <ms> Debounce delay before regenerating (default: 800)
83
- --touch <file> File to touch on schema change
84
- --no-clear Do not clear terminal on regeneration
85
- --help, -h Show this help message
86
- `;
87
- const introspectUsageText = `
88
- graphql-codegen introspect - Introspect a GraphQL endpoint or schema file and print table info
89
-
90
- Usage:
91
- graphql-codegen introspect [options]
27
+ Generator Options:
28
+ --react-query Generate React Query hooks
29
+ --orm Generate ORM client
30
+ -o, --output <dir> Output directory
31
+ -t, --target <name> Target name (for multi-target configs)
32
+ -a, --authorization <token> Authorization header value
33
+ --dry-run Preview without writing files
34
+ -v, --verbose Show detailed output
92
35
 
93
- Options:
94
- --endpoint, -e <url> GraphQL endpoint URL
95
- --schema, -s <path> Path to GraphQL schema file (.graphql)
96
- --authorization, -a <header> Authorization header value
97
- --json Output as JSON
98
- --help, -h Show this help message
36
+ -h, --help Show this help message
37
+ --version Show version number
99
38
  `;
100
- /**
101
- * Format duration in a human-readable way
102
- * - Under 1 second: show milliseconds (e.g., "123ms")
103
- * - Over 1 second: show seconds with 2 decimal places (e.g., "1.23s")
104
- */
105
- function formatDuration(ms) {
106
- if (ms < 1000) {
107
- return `${Math.round(ms)}ms`;
108
- }
109
- return `${(ms / 1000).toFixed(2)}s`;
110
- }
111
- /**
112
- * Load configuration for watch mode, merging CLI options with config file
113
- */
114
- async function loadWatchConfig(options) {
115
- let configPath = options.config;
116
- if (!configPath) {
117
- configPath = findConfigFile() ?? undefined;
118
- }
119
- let baseConfig = {};
120
- if (configPath) {
121
- const loadResult = await loadConfigFile(configPath);
122
- if (!loadResult.success) {
123
- console.error('x', loadResult.error);
124
- return null;
125
- }
126
- baseConfig = loadResult.config;
127
- }
128
- if (isMultiConfig(baseConfig)) {
129
- if (!options.target) {
130
- console.error('x Watch mode requires --target when using multiple targets.');
131
- return null;
132
- }
133
- if (!baseConfig.targets[options.target]) {
134
- console.error(`x Target "${options.target}" not found in config file.`);
135
- return null;
136
- }
137
- }
138
- else if (options.target) {
139
- console.error('x Config file does not define targets. Remove --target.');
140
- return null;
141
- }
142
- const sourceOverrides = {};
143
- if (options.endpoint) {
144
- sourceOverrides.endpoint = options.endpoint;
145
- sourceOverrides.schema = undefined;
146
- }
147
- const watchOverrides = {
148
- watch: {
149
- ...(options.pollInterval !== undefined && {
150
- pollInterval: options.pollInterval,
151
- }),
152
- ...(options.debounce !== undefined && { debounce: options.debounce }),
153
- ...(options.touch !== undefined && { touchFile: options.touch }),
154
- ...(options.clear !== undefined && { clearScreen: options.clear }),
155
- },
156
- };
157
- let mergedTarget;
158
- if (isMultiConfig(baseConfig)) {
159
- const defaults = baseConfig.defaults ?? {};
160
- const targetConfig = baseConfig.targets[options.target];
161
- mergedTarget = mergeConfig(defaults, targetConfig);
162
- }
163
- else {
164
- mergedTarget = baseConfig;
165
- }
166
- mergedTarget = mergeConfig(mergedTarget, sourceOverrides);
167
- mergedTarget = mergeConfig(mergedTarget, watchOverrides);
168
- if (!mergedTarget.endpoint) {
169
- console.error('x No endpoint specified. Watch mode only supports live endpoints.');
170
- return null;
171
- }
172
- if (mergedTarget.schema) {
173
- console.error('x Watch mode is only supported with an endpoint, not schema.');
174
- return null;
175
- }
176
- return resolveConfig(mergedTarget);
177
- }
178
- /**
179
- * Init command handler
180
- */
181
- async function handleInit(argv) {
182
- if (argv.help || argv.h) {
183
- console.log(initUsageText);
184
- process.exit(0);
185
- }
186
- const startTime = performance.now();
187
- const result = await initCommand({
188
- directory: argv.directory || argv.d || '.',
189
- force: !!(argv.force || argv.f),
190
- endpoint: argv.endpoint || argv.e,
191
- output: argv.output || argv.o || './generated',
192
- });
193
- const duration = formatDuration(performance.now() - startTime);
194
- if (result.success) {
195
- console.log('[ok]', result.message, `(${duration})`);
196
- }
197
- else {
198
- console.error('x', result.message, `(${duration})`);
199
- process.exit(1);
200
- }
201
- }
202
- /**
203
- * Generate command handler
204
- */
205
- async function handleGenerate(argv) {
206
- if (argv.help || argv.h) {
207
- console.log(generateUsageText);
39
+ export const commands = async (argv, prompter, _options) => {
40
+ if (argv.version) {
41
+ const pkg = getPackageJson(__dirname);
42
+ console.log(pkg.version);
208
43
  process.exit(0);
209
44
  }
210
- const startTime = performance.now();
211
- const config = argv.config || argv.c;
212
- const target = argv.target || argv.t;
213
- const endpoint = argv.endpoint || argv.e;
214
- const schema = argv.schema || argv.s;
215
- const output = argv.output || argv.o;
216
- const authorization = argv.authorization || argv.a;
217
- const verbose = !!(argv.verbose || argv.v);
218
- const dryRun = !!(argv['dry-run'] || argv.dryRun);
219
- const watch = !!(argv.watch || argv.w);
220
- const pollInterval = argv['poll-interval'] !== undefined
221
- ? parseInt(argv['poll-interval'], 10)
222
- : undefined;
223
- const debounce = argv.debounce !== undefined
224
- ? parseInt(argv.debounce, 10)
225
- : undefined;
226
- const touch = argv.touch;
227
- const clear = argv.clear !== false;
228
- if (endpoint && schema) {
229
- console.error('x Cannot use both --endpoint and --schema. Choose one source.');
230
- process.exit(1);
231
- }
232
- if (watch) {
233
- if (schema) {
234
- console.error('x Watch mode is only supported with --endpoint, not --schema.');
235
- process.exit(1);
236
- }
237
- const watchConfig = await loadWatchConfig({
238
- config,
239
- target,
240
- endpoint,
241
- output,
242
- pollInterval,
243
- debounce,
244
- touch,
245
- clear,
246
- });
247
- if (!watchConfig) {
248
- process.exit(1);
249
- }
250
- await startWatch({
251
- config: watchConfig,
252
- generatorType: 'generate',
253
- verbose,
254
- authorization,
255
- configPath: config,
256
- target,
257
- outputDir: output,
258
- });
259
- return;
260
- }
261
- const result = await generateReactQuery({
262
- config,
263
- target,
264
- endpoint,
265
- schema,
266
- output,
267
- authorization,
268
- verbose,
269
- dryRun,
270
- });
271
- const duration = formatDuration(performance.now() - startTime);
272
- const targetResults = result.targets ?? [];
273
- const hasNamedTargets = targetResults.length > 0 &&
274
- (targetResults.length > 1 || targetResults[0]?.name !== 'default');
275
- if (hasNamedTargets) {
276
- console.log(result.success ? '[ok]' : 'x', result.message);
277
- targetResults.forEach((t) => {
278
- const status = t.success ? '[ok]' : 'x';
279
- console.log(`\n${status} ${t.message}`);
280
- if (t.tables && t.tables.length > 0) {
281
- console.log(' Tables:');
282
- t.tables.forEach((table) => console.log(` - ${table}`));
283
- }
284
- if (t.filesWritten && t.filesWritten.length > 0) {
285
- console.log(' Files written:');
286
- t.filesWritten.forEach((file) => console.log(` - ${file}`));
287
- }
288
- if (!t.success && t.errors) {
289
- t.errors.forEach((error) => console.error(` - ${error}`));
290
- }
291
- });
292
- if (!result.success) {
293
- process.exit(1);
294
- }
295
- return;
296
- }
297
- if (result.success) {
298
- console.log('[ok]', result.message, `(${duration})`);
299
- if (result.tables && result.tables.length > 0) {
300
- console.log('\nTables:');
301
- result.tables.forEach((t) => console.log(` - ${t}`));
302
- }
303
- if (result.filesWritten && result.filesWritten.length > 0) {
304
- console.log('\nFiles written:');
305
- result.filesWritten.forEach((f) => console.log(` - ${f}`));
306
- }
307
- }
308
- else {
309
- console.error('x', result.message, `(${duration})`);
310
- if (result.errors) {
311
- result.errors.forEach((e) => console.error(' -', e));
312
- }
313
- process.exit(1);
314
- }
315
- }
316
- /**
317
- * Generate ORM command handler
318
- */
319
- async function handleGenerateOrm(argv) {
320
45
  if (argv.help || argv.h) {
321
- console.log(generateOrmUsageText);
46
+ console.log(usage);
322
47
  process.exit(0);
323
48
  }
324
- const startTime = performance.now();
325
- const config = argv.config || argv.c;
326
- const target = argv.target || argv.t;
327
- const endpoint = argv.endpoint || argv.e;
328
- const schema = argv.schema || argv.s;
329
- const output = argv.output || argv.o;
330
- const authorization = argv.authorization || argv.a;
331
- const verbose = !!(argv.verbose || argv.v);
332
- const dryRun = !!(argv['dry-run'] || argv.dryRun);
333
- const skipCustomOperations = !!(argv['skip-custom-operations'] || argv.skipCustomOperations);
334
- const watch = !!(argv.watch || argv.w);
335
- const pollInterval = argv['poll-interval'] !== undefined
336
- ? parseInt(argv['poll-interval'], 10)
337
- : undefined;
338
- const debounce = argv.debounce !== undefined
339
- ? parseInt(argv.debounce, 10)
340
- : undefined;
341
- const touch = argv.touch;
342
- const clear = argv.clear !== false;
343
- if (endpoint && schema) {
344
- console.error('x Cannot use both --endpoint and --schema. Choose one source.');
345
- process.exit(1);
346
- }
347
- if (watch) {
348
- if (schema) {
349
- console.error('x Watch mode is only supported with --endpoint, not --schema.');
350
- process.exit(1);
351
- }
352
- const watchConfig = await loadWatchConfig({
353
- config,
354
- target,
355
- endpoint,
356
- output,
357
- pollInterval,
358
- debounce,
359
- touch,
360
- clear,
361
- });
362
- if (!watchConfig) {
49
+ const configPath = (argv.config || argv.c || findConfigFile());
50
+ const targetName = (argv.target || argv.t);
51
+ // If config file exists, load and run
52
+ if (configPath) {
53
+ const loaded = await loadConfigFile(configPath);
54
+ if (!loaded.success) {
55
+ console.error('x', loaded.error);
363
56
  process.exit(1);
364
57
  }
365
- await startWatch({
366
- config: watchConfig,
367
- generatorType: 'generate-orm',
368
- verbose,
369
- authorization,
370
- configPath: config,
371
- target,
372
- outputDir: output,
373
- skipCustomOperations,
374
- });
375
- return;
376
- }
377
- const result = await generateOrm({
378
- config,
379
- target,
380
- endpoint,
381
- schema,
382
- output,
383
- authorization,
384
- verbose,
385
- dryRun,
386
- skipCustomOperations,
387
- });
388
- const duration = formatDuration(performance.now() - startTime);
389
- const targetResults = result.targets ?? [];
390
- const hasNamedTargets = targetResults.length > 0 &&
391
- (targetResults.length > 1 || targetResults[0]?.name !== 'default');
392
- if (hasNamedTargets) {
393
- console.log(result.success ? '[ok]' : 'x', result.message);
394
- targetResults.forEach((t) => {
395
- const status = t.success ? '[ok]' : 'x';
396
- console.log(`\n${status} ${t.message}`);
397
- if (t.tables && t.tables.length > 0) {
398
- console.log(' Tables:');
399
- t.tables.forEach((table) => console.log(` - ${table}`));
58
+ const config = loaded.config;
59
+ // Check if it's a multi-target config (no source fields at top level)
60
+ const isMulti = !('endpoint' in config || 'schemaFile' in config || 'db' in config);
61
+ if (isMulti) {
62
+ // Multi-target: simple for loop over targets
63
+ const targets = config;
64
+ const names = targetName ? [targetName] : Object.keys(targets);
65
+ if (targetName && !targets[targetName]) {
66
+ console.error('x', `Target "${targetName}" not found. Available: ${Object.keys(targets).join(', ')}`);
67
+ process.exit(1);
400
68
  }
401
- if (t.customQueries && t.customQueries.length > 0) {
402
- console.log(' Custom Queries:');
403
- t.customQueries.forEach((query) => console.log(` - ${query}`));
69
+ let hasError = false;
70
+ for (const name of names) {
71
+ console.log(`\n[${name}]`);
72
+ const result = await generate(targets[name]);
73
+ printResult(result);
74
+ if (!result.success)
75
+ hasError = true;
404
76
  }
405
- if (t.customMutations && t.customMutations.length > 0) {
406
- console.log(' Custom Mutations:');
407
- t.customMutations.forEach((mutation) => console.log(` - ${mutation}`));
408
- }
409
- if (t.filesWritten && t.filesWritten.length > 0) {
410
- console.log(' Files written:');
411
- t.filesWritten.forEach((file) => console.log(` - ${file}`));
412
- }
413
- if (!t.success && t.errors) {
414
- t.errors.forEach((error) => console.error(` - ${error}`));
415
- }
416
- });
417
- if (!result.success) {
77
+ prompter.close();
78
+ if (hasError)
79
+ process.exit(1);
80
+ return argv;
81
+ }
82
+ // Single config
83
+ const result = await generate(config);
84
+ printResult(result);
85
+ if (!result.success)
418
86
  process.exit(1);
419
- }
420
- return;
421
- }
422
- if (result.success) {
423
- console.log('[ok]', result.message, `(${duration})`);
424
- if (result.tables && result.tables.length > 0) {
425
- console.log('\nTables:');
426
- result.tables.forEach((t) => console.log(` - ${t}`));
427
- }
428
- if (result.customQueries && result.customQueries.length > 0) {
429
- console.log('\nCustom Queries:');
430
- result.customQueries.forEach((q) => console.log(` - ${q}`));
431
- }
432
- if (result.customMutations && result.customMutations.length > 0) {
433
- console.log('\nCustom Mutations:');
434
- result.customMutations.forEach((m) => console.log(` - ${m}`));
435
- }
436
- if (result.filesWritten && result.filesWritten.length > 0) {
437
- console.log('\nFiles written:');
438
- result.filesWritten.forEach((f) => console.log(` - ${f}`));
439
- }
440
- }
441
- else {
442
- console.error('x', result.message, `(${duration})`);
443
- if (result.errors) {
444
- result.errors.forEach((e) => console.error(' -', e));
445
- }
446
- process.exit(1);
447
- }
448
- }
449
- /**
450
- * Introspect command handler
451
- */
452
- async function handleIntrospect(argv) {
453
- if (argv.help || argv.h) {
454
- console.log(introspectUsageText);
455
- process.exit(0);
456
- }
457
- const startTime = performance.now();
458
- const endpoint = argv.endpoint || argv.e;
459
- const schema = argv.schema || argv.s;
460
- const authorization = argv.authorization || argv.a;
461
- const json = !!argv.json;
462
- if (!endpoint && !schema) {
463
- console.error('x Either --endpoint or --schema must be provided.');
464
- process.exit(1);
465
- }
466
- if (endpoint && schema) {
467
- console.error('x Cannot use both --endpoint and --schema. Choose one source.');
468
- process.exit(1);
469
- }
470
- const { createSchemaSource } = await import('./introspect/source');
471
- const { inferTablesFromIntrospection } = await import('./introspect/infer-tables');
472
- try {
473
- const source = createSchemaSource({
474
- endpoint,
475
- schema,
476
- authorization,
477
- });
478
- console.log('Fetching schema from', source.describe(), '...');
479
- const { introspection } = await source.fetch();
480
- const tables = inferTablesFromIntrospection(introspection);
481
- const duration = formatDuration(performance.now() - startTime);
482
- if (json) {
483
- console.log(JSON.stringify(tables, null, 2));
484
- }
485
- else {
486
- console.log(`\n[ok] Found ${tables.length} tables (${duration}):\n`);
487
- tables.forEach((table) => {
488
- const fieldCount = table.fields.length;
489
- const relationCount = table.relations.belongsTo.length +
490
- table.relations.hasOne.length +
491
- table.relations.hasMany.length +
492
- table.relations.manyToMany.length;
493
- console.log(` ${table.name} (${fieldCount} fields, ${relationCount} relations)`);
494
- });
495
- }
496
- }
497
- catch (err) {
498
- const duration = formatDuration(performance.now() - startTime);
499
- console.error('x Failed to introspect schema:', err instanceof Error ? err.message : err, `(${duration})`);
500
- process.exit(1);
501
- }
502
- }
503
- const createCommandMap = () => {
504
- return {
505
- init: handleInit,
506
- generate: handleGenerate,
507
- 'generate-orm': handleGenerateOrm,
508
- introspect: handleIntrospect,
509
- };
510
- };
511
- export const commands = async (argv, prompter, _options) => {
512
- if (argv.version || argv.v) {
513
- const pkg = getPackageJson(__dirname);
514
- console.log(pkg.version);
515
- process.exit(0);
516
- }
517
- const { first: command, newArgv } = extractFirst(argv);
518
- if ((argv.help || argv.h) && !command) {
519
- console.log(usageText);
520
- process.exit(0);
521
- }
522
- if (command === 'help') {
523
- console.log(usageText);
524
- process.exit(0);
525
- }
526
- const commandMap = createCommandMap();
527
- if (!command) {
528
- const answer = await prompter.prompt(argv, [
529
- {
530
- type: 'autocomplete',
531
- name: 'command',
532
- message: 'What do you want to do?',
533
- options: Object.keys(commandMap),
534
- },
535
- ]);
536
- const selectedCommand = answer.command;
537
- const commandFn = commandMap[selectedCommand];
538
- if (commandFn) {
539
- await commandFn(newArgv);
540
- }
541
87
  prompter.close();
542
88
  return argv;
543
89
  }
544
- const commandFn = commandMap[command];
545
- if (!commandFn) {
546
- console.log(usageText);
547
- await cliExitWithError(`Unknown command: ${command}`);
548
- }
549
- await commandFn(newArgv);
90
+ // No config file - prompt for options using shared questions
91
+ const answers = await prompter.prompt(argv, codegenQuestions);
92
+ // Build db config if schemas or apiNames provided
93
+ const db = (answers.schemas || answers.apiNames) ? {
94
+ schemas: answers.schemas,
95
+ apiNames: answers.apiNames,
96
+ } : undefined;
97
+ const result = await generate({
98
+ endpoint: answers.endpoint,
99
+ schemaFile: answers.schemaFile,
100
+ db,
101
+ output: answers.output,
102
+ authorization: answers.authorization,
103
+ reactQuery: answers.reactQuery,
104
+ orm: answers.orm,
105
+ dryRun: answers.dryRun,
106
+ verbose: answers.verbose,
107
+ });
108
+ printResult(result);
550
109
  prompter.close();
551
110
  return argv;
552
111
  };
553
112
  export const options = {
554
113
  minimistOpts: {
555
114
  alias: {
556
- v: 'version',
557
115
  h: 'help',
558
116
  c: 'config',
559
- t: 'target',
560
117
  e: 'endpoint',
561
- s: 'schema',
118
+ s: 'schema-file',
562
119
  o: 'output',
120
+ t: 'target',
563
121
  a: 'authorization',
564
- d: 'directory',
565
- f: 'force',
566
- w: 'watch',
567
- },
568
- boolean: ['help', 'version', 'force', 'verbose', 'dry-run', 'watch', 'json', 'skip-custom-operations', 'clear'],
569
- string: ['config', 'target', 'endpoint', 'schema', 'output', 'authorization', 'directory', 'touch', 'poll-interval', 'debounce'],
570
- default: {
571
- clear: true,
122
+ v: 'verbose',
572
123
  },
124
+ boolean: [
125
+ 'help', 'version', 'verbose', 'dry-run', 'react-query', 'orm', 'keep-db',
126
+ ],
127
+ string: [
128
+ 'config', 'endpoint', 'schema-file', 'output', 'target', 'authorization',
129
+ 'pgpm-module-path', 'pgpm-workspace-path', 'pgpm-module-name',
130
+ 'schemas', 'api-names',
131
+ ],
573
132
  },
574
133
  };
575
134
  if (require.main === module) {
576
- if (process.argv.includes('--version') || process.argv.includes('-v')) {
577
- const pkg = getPackageJson(__dirname);
578
- console.log(pkg.version);
579
- process.exit(0);
580
- }
581
- const app = new CLI(commands, options);
582
- app.run().then(() => {
583
- }).catch((error) => {
584
- console.error('Unexpected error:', error);
585
- process.exit(1);
586
- });
135
+ const cli = new CLI(commands, options);
136
+ cli.run();
587
137
  }