@supatype/cli 0.8.0 → 0.9.0

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 (209) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/bin/supatype.js +16 -1
  3. package/dist/after-hooks-health.d.ts +19 -0
  4. package/dist/after-hooks-health.d.ts.map +1 -0
  5. package/dist/after-hooks-health.js +69 -0
  6. package/dist/after-hooks-health.js.map +1 -0
  7. package/dist/app-vite-env.d.ts +5 -1
  8. package/dist/app-vite-env.d.ts.map +1 -1
  9. package/dist/app-vite-env.js +9 -15
  10. package/dist/app-vite-env.js.map +1 -1
  11. package/dist/augmentation-generator.d.ts +5 -0
  12. package/dist/augmentation-generator.d.ts.map +1 -1
  13. package/dist/augmentation-generator.js +13 -4
  14. package/dist/augmentation-generator.js.map +1 -1
  15. package/dist/binary-cache.d.ts +2 -1
  16. package/dist/binary-cache.d.ts.map +1 -1
  17. package/dist/binary-cache.js +2 -4
  18. package/dist/binary-cache.js.map +1 -1
  19. package/dist/byte-size.d.ts +9 -0
  20. package/dist/byte-size.d.ts.map +1 -0
  21. package/dist/byte-size.js +36 -0
  22. package/dist/byte-size.js.map +1 -0
  23. package/dist/cache-clean.d.ts +0 -2
  24. package/dist/cache-clean.d.ts.map +1 -1
  25. package/dist/cache-clean.js +1 -27
  26. package/dist/cache-clean.js.map +1 -1
  27. package/dist/cache-pins.d.ts.map +1 -1
  28. package/dist/cache-pins.js +1 -18
  29. package/dist/cache-pins.js.map +1 -1
  30. package/dist/cache-root.d.ts +7 -0
  31. package/dist/cache-root.d.ts.map +1 -0
  32. package/dist/cache-root.js +11 -0
  33. package/dist/cache-root.js.map +1 -0
  34. package/dist/cli-artifacts.d.ts.map +1 -1
  35. package/dist/cli-artifacts.js +2 -1
  36. package/dist/cli-artifacts.js.map +1 -1
  37. package/dist/cli-install-method.d.ts.map +1 -1
  38. package/dist/cli-install-method.js +7 -1
  39. package/dist/cli-install-method.js.map +1 -1
  40. package/dist/cli-launcher.d.ts +21 -0
  41. package/dist/cli-launcher.d.ts.map +1 -0
  42. package/dist/cli-launcher.js +112 -0
  43. package/dist/cli-launcher.js.map +1 -0
  44. package/dist/cli-old-versions.d.ts +44 -0
  45. package/dist/cli-old-versions.d.ts.map +1 -0
  46. package/dist/cli-old-versions.js +83 -0
  47. package/dist/cli-old-versions.js.map +1 -0
  48. package/dist/cli-program-path.d.ts +20 -0
  49. package/dist/cli-program-path.d.ts.map +1 -0
  50. package/dist/cli-program-path.js +41 -0
  51. package/dist/cli-program-path.js.map +1 -0
  52. package/dist/cli-standalone.d.ts +2 -3
  53. package/dist/cli-standalone.d.ts.map +1 -1
  54. package/dist/cli-standalone.js +51 -24
  55. package/dist/cli-standalone.js.map +1 -1
  56. package/dist/cli-version-embedded.js +1 -1
  57. package/dist/commands/cache.d.ts.map +1 -1
  58. package/dist/commands/cache.js +32 -7
  59. package/dist/commands/cache.js.map +1 -1
  60. package/dist/commands/deploy.js +1 -1
  61. package/dist/commands/deploy.js.map +1 -1
  62. package/dist/commands/dev.d.ts.map +1 -1
  63. package/dist/commands/dev.js +14 -1
  64. package/dist/commands/dev.js.map +1 -1
  65. package/dist/commands/doctor.d.ts +32 -0
  66. package/dist/commands/doctor.d.ts.map +1 -1
  67. package/dist/commands/doctor.js +66 -1
  68. package/dist/commands/doctor.js.map +1 -1
  69. package/dist/commands/hooks.d.ts.map +1 -1
  70. package/dist/commands/hooks.js +23 -15
  71. package/dist/commands/hooks.js.map +1 -1
  72. package/dist/commands/init.d.ts +25 -6
  73. package/dist/commands/init.d.ts.map +1 -1
  74. package/dist/commands/init.js +315 -286
  75. package/dist/commands/init.js.map +1 -1
  76. package/dist/commands/keys.d.ts +10 -10
  77. package/dist/commands/keys.d.ts.map +1 -1
  78. package/dist/commands/keys.js +32 -32
  79. package/dist/commands/keys.js.map +1 -1
  80. package/dist/commands/link-helpers.d.ts.map +1 -1
  81. package/dist/commands/link-helpers.js +0 -2
  82. package/dist/commands/link-helpers.js.map +1 -1
  83. package/dist/commands/push.d.ts +2 -0
  84. package/dist/commands/push.d.ts.map +1 -1
  85. package/dist/commands/push.js +38 -9
  86. package/dist/commands/push.js.map +1 -1
  87. package/dist/commands/self-host.d.ts.map +1 -1
  88. package/dist/commands/self-host.js +14 -17
  89. package/dist/commands/self-host.js.map +1 -1
  90. package/dist/commands/self-update.d.ts.map +1 -1
  91. package/dist/commands/self-update.js +9 -3
  92. package/dist/commands/self-update.js.map +1 -1
  93. package/dist/compose-kong.d.ts +4 -0
  94. package/dist/compose-kong.d.ts.map +1 -1
  95. package/dist/compose-kong.js +13 -3
  96. package/dist/compose-kong.js.map +1 -1
  97. package/dist/config.d.ts +9 -1
  98. package/dist/config.d.ts.map +1 -1
  99. package/dist/config.js +18 -3
  100. package/dist/config.js.map +1 -1
  101. package/dist/database-url-owner.d.ts +5 -3
  102. package/dist/database-url-owner.d.ts.map +1 -1
  103. package/dist/database-url-owner.js +7 -4
  104. package/dist/database-url-owner.js.map +1 -1
  105. package/dist/db-port.d.ts +2 -0
  106. package/dist/db-port.d.ts.map +1 -1
  107. package/dist/db-port.js +1 -1
  108. package/dist/db-port.js.map +1 -1
  109. package/dist/db-preflight.d.ts +32 -3
  110. package/dist/db-preflight.d.ts.map +1 -1
  111. package/dist/db-preflight.js +211 -86
  112. package/dist/db-preflight.js.map +1 -1
  113. package/dist/dev-compose.d.ts +5 -0
  114. package/dist/dev-compose.d.ts.map +1 -1
  115. package/dist/dev-compose.js +101 -45
  116. package/dist/dev-compose.js.map +1 -1
  117. package/dist/dev-log-bus.d.ts +8 -0
  118. package/dist/dev-log-bus.d.ts.map +1 -1
  119. package/dist/dev-log-bus.js +13 -0
  120. package/dist/dev-log-bus.js.map +1 -1
  121. package/dist/dev-ports.d.ts +5 -0
  122. package/dist/dev-ports.d.ts.map +1 -1
  123. package/dist/dev-ports.js +7 -3
  124. package/dist/dev-ports.js.map +1 -1
  125. package/dist/dev-session.d.ts +15 -0
  126. package/dist/dev-session.d.ts.map +1 -1
  127. package/dist/dev-session.js +24 -1
  128. package/dist/dev-session.js.map +1 -1
  129. package/dist/dev-shutdown.d.ts.map +1 -1
  130. package/dist/dev-shutdown.js +15 -2
  131. package/dist/dev-shutdown.js.map +1 -1
  132. package/dist/diff-output.d.ts +7 -1
  133. package/dist/diff-output.d.ts.map +1 -1
  134. package/dist/diff-output.js +91 -2
  135. package/dist/diff-output.js.map +1 -1
  136. package/dist/docker-runtime.d.ts +2 -0
  137. package/dist/docker-runtime.d.ts.map +1 -1
  138. package/dist/docker-runtime.js +7 -1
  139. package/dist/docker-runtime.js.map +1 -1
  140. package/dist/engine-client.d.ts.map +1 -1
  141. package/dist/engine-client.js +3 -0
  142. package/dist/engine-client.js.map +1 -1
  143. package/dist/engine-ownership-gate.d.ts +3 -1
  144. package/dist/engine-ownership-gate.d.ts.map +1 -1
  145. package/dist/engine-ownership-gate.js +5 -0
  146. package/dist/engine-ownership-gate.js.map +1 -1
  147. package/dist/env-file.d.ts +4 -2
  148. package/dist/env-file.d.ts.map +1 -1
  149. package/dist/env-file.js +6 -6
  150. package/dist/env-file.js.map +1 -1
  151. package/dist/frontend-env.d.ts +43 -0
  152. package/dist/frontend-env.d.ts.map +1 -0
  153. package/dist/frontend-env.js +81 -0
  154. package/dist/frontend-env.js.map +1 -0
  155. package/dist/hook-actor.d.ts +17 -0
  156. package/dist/hook-actor.d.ts.map +1 -0
  157. package/dist/hook-actor.js +37 -0
  158. package/dist/hook-actor.js.map +1 -0
  159. package/dist/hook-columns.d.ts +30 -0
  160. package/dist/hook-columns.d.ts.map +1 -0
  161. package/dist/hook-columns.js +77 -0
  162. package/dist/hook-columns.js.map +1 -0
  163. package/dist/hook-events.d.ts +11 -0
  164. package/dist/hook-events.d.ts.map +1 -0
  165. package/dist/hook-events.js +11 -0
  166. package/dist/hook-events.js.map +1 -0
  167. package/dist/hooks-generator.d.ts.map +1 -1
  168. package/dist/hooks-generator.js +87 -21
  169. package/dist/hooks-generator.js.map +1 -1
  170. package/dist/init-dependency-versions.d.ts.map +1 -1
  171. package/dist/init-dependency-versions.js +11 -1
  172. package/dist/init-dependency-versions.js.map +1 -1
  173. package/dist/init-package-json.d.ts +38 -1
  174. package/dist/init-package-json.d.ts.map +1 -1
  175. package/dist/init-package-json.js +150 -39
  176. package/dist/init-package-json.js.map +1 -1
  177. package/dist/model-hooks.d.ts +5 -1
  178. package/dist/model-hooks.d.ts.map +1 -1
  179. package/dist/model-hooks.js +12 -2
  180. package/dist/model-hooks.js.map +1 -1
  181. package/dist/project-config.d.ts +4 -0
  182. package/dist/project-config.d.ts.map +1 -1
  183. package/dist/project-config.js +6 -0
  184. package/dist/project-config.js.map +1 -1
  185. package/dist/resolve-target.d.ts +2 -0
  186. package/dist/resolve-target.d.ts.map +1 -1
  187. package/dist/resolve-target.js +8 -5
  188. package/dist/resolve-target.js.map +1 -1
  189. package/dist/self-host-compose.d.ts +16 -3
  190. package/dist/self-host-compose.d.ts.map +1 -1
  191. package/dist/self-host-compose.js +62 -10
  192. package/dist/self-host-compose.js.map +1 -1
  193. package/dist/shell-ports.d.ts.map +1 -1
  194. package/dist/shell-ports.js +2 -8
  195. package/dist/shell-ports.js.map +1 -1
  196. package/dist/tsx-runner.d.ts.map +1 -1
  197. package/dist/tsx-runner.js +2 -1
  198. package/dist/tsx-runner.js.map +1 -1
  199. package/dist/type-extractor.d.ts.map +1 -1
  200. package/dist/type-extractor.js +20 -11
  201. package/dist/type-extractor.js.map +1 -1
  202. package/dist/ui/dev/DevDashboard.d.ts.map +1 -1
  203. package/dist/ui/dev/DevDashboard.js +9 -3
  204. package/dist/ui/dev/DevDashboard.js.map +1 -1
  205. package/dist/ui/flows/prompt-fields.d.ts +11 -6
  206. package/dist/ui/flows/prompt-fields.d.ts.map +1 -1
  207. package/dist/ui/flows/prompt-fields.js +11 -8
  208. package/dist/ui/flows/prompt-fields.js.map +1 -1
  209. package/package.json +16 -13
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Which columns an after-hook's rows carry, per hooked table.
3
+ *
4
+ * After-hooks are fed from the database's change log, which no row or field rule filters, so the
5
+ * row a hook receives would otherwise hold every column. A column with a field `read` rule is left
6
+ * out unless the hook declares `columns: "all"`, so a hook that forwards a row to email or chat
7
+ * cannot leak a column the person who saved it could never see. The listener also needs each
8
+ * table's key to name a delivery.
9
+ *
10
+ * One function computes all of it, for the generated hook types and for whatever the server is sent,
11
+ * so the two cannot disagree about which columns a hook sees.
12
+ */
13
+ import { isJsonObject } from "@supatype/shared";
14
+ import { isNonEmptyObject, objectAt } from "./ast-read.js";
15
+ import { astModelsOf, columnNameOf, resolveTableName } from "./augmentation-generator.js";
16
+ export { isAfterHookEvent } from "./hook-events.js";
17
+ /** Per table that declares a hook, keyed by the table as Postgres keeps it. */
18
+ export function hookTableColumns(ast) {
19
+ const out = {};
20
+ for (const model of astModelsOf(ast)) {
21
+ if (!isNonEmptyObject(objectAt(model, "annotations", "platform", "hooks")))
22
+ continue;
23
+ out[resolveTableName(model)] = modelHookColumns(model);
24
+ }
25
+ return out;
26
+ }
27
+ /** {@link HookTableColumns} for one model, whether or not it declares a hook. */
28
+ export function modelHookColumns(model) {
29
+ const hooks = objectAt(model, "annotations", "platform", "hooks");
30
+ return {
31
+ restrictedColumns: restrictedColumnsOf(model),
32
+ primaryKey: primaryKeyOf(model),
33
+ columns: {
34
+ afterChange: declaredColumns(hooks?.["afterChange"]),
35
+ afterDelete: declaredColumns(hooks?.["afterDelete"]),
36
+ },
37
+ };
38
+ }
39
+ /** The restricted columns `event`'s rows leave out: none when it declared `columns: "all"`. */
40
+ export function hiddenColumns(table, event) {
41
+ return table.columns[event] === "all" ? [] : table.restrictedColumns;
42
+ }
43
+ function declaredColumns(entry) {
44
+ return isJsonObject(entry) && entry["columns"] === "all" ? "all" : "safe";
45
+ }
46
+ /**
47
+ * Columns with a field `read` rule. A `write` rule alone hides nothing from a reader, so it does not
48
+ * restrict a hook either. A rule on a relation names its foreign key column, which is what a row
49
+ * carries.
50
+ */
51
+ function restrictedColumnsOf(model) {
52
+ const rules = objectAt(model, "annotations", "platform", "access", "fields");
53
+ if (rules === undefined)
54
+ return [];
55
+ return Object.entries(rules)
56
+ .filter(([, rule]) => isJsonObject(rule) && rule["read"] !== undefined)
57
+ .flatMap(([name]) => columnOf(model, name));
58
+ }
59
+ /** The key columns, in the order the fields are declared. */
60
+ function primaryKeyOf(model) {
61
+ return Object.entries(model.fields)
62
+ .filter(([, meta]) => meta["primaryKey"] === true)
63
+ .flatMap(([name]) => columnOf(model, name));
64
+ }
65
+ /**
66
+ * The column `name` names, as the generated Row type names it, or none for a relation that keeps
67
+ * its key on the other table. A name that is no declared field is already a column (a field rule on
68
+ * a relation is written against its foreign key).
69
+ */
70
+ function columnOf(model, name) {
71
+ const meta = model.fields[name];
72
+ if (meta === undefined)
73
+ return [name];
74
+ const column = columnNameOf(name, meta);
75
+ return column === null ? [] : [column];
76
+ }
77
+ //# sourceMappingURL=hook-columns.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hook-columns.js","sourceRoot":"","sources":["../src/hook-columns.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,YAAY,EAAkD,MAAM,kBAAkB,CAAA;AAC/F,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAC1D,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,gBAAgB,EAAiB,MAAM,6BAA6B,CAAA;AAGxG,OAAO,EAAE,gBAAgB,EAAuB,MAAM,kBAAkB,CAAA;AAUxE,+EAA+E;AAC/E,MAAM,UAAU,gBAAgB,CAAC,GAAY;IAC3C,MAAM,GAAG,GAAqC,EAAE,CAAA;IAChD,KAAK,MAAM,KAAK,IAAI,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC;QACrC,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,KAAK,EAAE,aAAa,EAAE,UAAU,EAAE,OAAO,CAAC,CAAC;Y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,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,KAAK,SAAS,CAAC;SACtE,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAA;AAC/C,CAAC;AAED,6DAA6D;AAC7D,SAAS,YAAY,CAAC,KAAe;IACnC,OAAO,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC;SAChC,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,YAAY,CAAC,KAAK,IAAI,CAAC;SACjD,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAAA;AAC/C,CAAC;AAED;;;;GAIG;AACH,SAAS,QAAQ,CAAC,KAAe,EAAE,IAAY;IAC7C,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;IAC/B,IAAI,IAAI,KAAK,SAAS;QAAE,OAAO,CAAC,IAAI,CAAC,CAAA;IACrC,MAAM,MAAM,GAAG,YAAY,CAAC,IAAI,EAAE,IAAI,CAAC,CAAA;IACvC,OAAO,MAAM,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAA;AACxC,CAAC"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The model hook events, in one module with no imports, so the schema extractor and the hook
3
+ * generators can share them without the extractor loading the generators' dependencies.
4
+ */
5
+ /** Every event a model's `hooks` may declare. */
6
+ export declare const HOOK_EVENTS: readonly ["beforeChange", "afterChange", "beforeDelete", "afterDelete"];
7
+ /** The after-hook events, the only ones `columns` applies to. */
8
+ export type AfterHookEvent = "afterChange" | "afterDelete";
9
+ /** Whether a hook event runs after the write, from the change log, rather than before it. */
10
+ export declare function isAfterHookEvent(event: string): event is AfterHookEvent;
11
+ //# sourceMappingURL=hook-events.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hook-events.d.ts","sourceRoot":"","sources":["../src/hook-events.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,iDAAiD;AACjD,eAAO,MAAM,WAAW,yEAA0E,CAAA;AAElG,iEAAiE;AACjE,MAAM,MAAM,cAAc,GAAG,aAAa,GAAG,aAAa,CAAA;AAE1D,6FAA6F;AAC7F,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,cAAc,CAEvE"}
@@ -0,0 +1,11 @@
1
+ /**
2
+ * The model hook events, in one module with no imports, so the schema extractor and the hook
3
+ * generators can share them without the extractor loading the generators' dependencies.
4
+ */
5
+ /** Every event a model's `hooks` may declare. */
6
+ export const HOOK_EVENTS = ["beforeChange", "afterChange", "beforeDelete", "afterDelete"];
7
+ /** Whether a hook event runs after the write, from the change log, rather than before it. */
8
+ export function isAfterHookEvent(event) {
9
+ return event === "afterChange" || event === "afterDelete";
10
+ }
11
+ //# sourceMappingURL=hook-events.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hook-events.js","sourceRoot":"","sources":["../src/hook-events.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,iDAAiD;AACjD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,cAAc,EAAE,aAAa,EAAE,cAAc,EAAE,aAAa,CAAU,CAAA;AAKlG,6FAA6F;AAC7F,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,OAAO,KAAK,KAAK,aAAa,IAAI,KAAK,KAAK,aAAa,CAAA;AAC3D,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"hooks-generator.d.ts","sourceRoot":"","sources":["../src/hooks-generator.ts"],"names":[],"mappings":"AAuCA;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAE/D;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAmYtD"}
1
+ {"version":3,"file":"hooks-generator.d.ts","sourceRoot":"","sources":["../src/hooks-generator.ts"],"names":[],"mappings":"AA+DA;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAG/D;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,GAAG,MAAM,CAEtD"}
@@ -11,6 +11,7 @@
11
11
  import { isNonEmptyObject, objectAt } from "./ast-read.js";
12
12
  import { fieldsWithCallerDefaults } from "./owner-default.js";
13
13
  import { astModelsOf, generateInsertType, generateRowType, generateUpdateType, resolveTableName, } from "./augmentation-generator.js";
14
+ import { hiddenColumns, modelHookColumns } from "./hook-columns.js";
14
15
  /** Tables that declare at least one hook, sorted for a stable file. */
15
16
  function hookedModels(ast) {
16
17
  return astModelsOf(ast)
@@ -21,9 +22,30 @@ function hookedModels(ast) {
21
22
  return isNonEmptyObject(platform?.["hooks"]) || isNonEmptyObject(platform?.["validate"]);
22
23
  })
23
24
  // Fields as the engine writes them: an owner auth user id defaults to the caller (C9).
24
- .map((model) => ({ table: resolveTableName(model), fields: fieldsWithCallerDefaults(model) }))
25
+ .map((model) => ({
26
+ table: resolveTableName(model),
27
+ fields: fieldsWithCallerDefaults(model),
28
+ columns: modelHookColumns(model),
29
+ }))
25
30
  .sort((a, b) => a.table.localeCompare(b.table));
26
31
  }
32
+ /**
33
+ * One table's entry in `HookTables`. `AfterHookHidden` names, per after-hook, the restricted columns
34
+ * its rows leave out (`never` when it declared `columns: "all"`), so the types match what the server
35
+ * sends rather than claiming a column the hook never receives.
36
+ */
37
+ function tableEntry(model) {
38
+ const hidden = (event) => {
39
+ const names = hiddenColumns(model.columns, event);
40
+ return names.length === 0 ? "never" : names.map((name) => JSON.stringify(name)).join(" | ");
41
+ };
42
+ return ` ${JSON.stringify(model.table)}: {
43
+ Row: ${indent(denoSafe(generateRowType(model.fields)), 4)}
44
+ Insert: ${indent(denoSafe(generateInsertType(model.fields)), 4)}
45
+ Update: ${indent(denoSafe(generateUpdateType(model.fields)), 4)}
46
+ AfterHookHidden: { afterChange: ${hidden("afterChange")}; afterDelete: ${hidden("afterDelete")} }
47
+ }`;
48
+ }
27
49
  /**
28
50
  * The module source, or `null` when no model declares a hook or a field validator.
29
51
  *
@@ -31,21 +53,18 @@ function hookedModels(ast) {
31
53
  * that have no hooks, a generated file nobody imports is a file somebody eventually edits.
32
54
  */
33
55
  export function generateHooksModule(ast) {
34
- return hookedModels(ast).length === 0 ? null : hooksModuleSource(ast);
56
+ const models = hookedModels(ast);
57
+ return models.length === 0 ? null : moduleSource(models);
35
58
  }
36
59
  /**
37
60
  * The module source for `ast`, with no table when no model declares a hook: what a handler just
38
61
  * scaffolded imports, so it loads before the push that wires it writes the tables.
39
62
  */
40
63
  export function hooksModuleSource(ast) {
41
- const models = hookedModels(ast);
42
- const tableEntries = models
43
- .map((model) => ` ${JSON.stringify(model.table)}: {
44
- Row: ${indent(denoSafe(generateRowType(model.fields)), 4)}
45
- Insert: ${indent(denoSafe(generateInsertType(model.fields)), 4)}
46
- Update: ${indent(denoSafe(generateUpdateType(model.fields)), 4)}
47
- }`)
48
- .join("\n");
64
+ return moduleSource(hookedModels(ast));
65
+ }
66
+ function moduleSource(models) {
67
+ const tableEntries = models.map(tableEntry).join("\n");
49
68
  return `// Generated by supatype CLI, do not edit manually.
50
69
  //
51
70
  // Typed handlers for the models that declare \`hooks\`. Import from a function beside it:
@@ -107,7 +126,10 @@ export type HookedTable = keyof HookTables & string
107
126
 
108
127
  interface HookBase {
109
128
  readonly table: string
110
- /** Claims from the JWT the server already verified. \`null\` for an anonymous caller. */
129
+ /**
130
+ * Claims from the JWT the server already verified. \`null\` for an anonymous caller, and for an
131
+ * after-hook on a write made outside the API (SQL, a migration), which has no caller.
132
+ */
111
133
  readonly user: { readonly sub: string; readonly role: string; readonly email?: string } | null
112
134
  /** Correlates this hook with the request in server logs. */
113
135
  readonly requestId: string
@@ -116,10 +138,11 @@ interface HookBase {
116
138
  /**
117
139
  * The rows this request is about to overwrite, fetched only if you ask.
118
140
  *
119
- * Resolved by the server from the in-flight request's own filter, as the caller's own role, so it
120
- * cannot show you rows the caller could not read, and you cannot widen the filter. Capped:
121
- * \`truncated\` is true when there were more, in which case acting on what you got means acting on a
122
- * prefix you did not choose.
141
+ * The rows the write will change: the server selects them by the request's own filter plus the
142
+ * caller's own update or delete rule, so you cannot widen the filter. They are returned as stored,
143
+ * with no field masking, so do not echo them into a rejection message, which reaches the caller.
144
+ * Capped: \`truncated\` is true when there were more, in which case acting on what you got means
145
+ * acting on a prefix you did not choose.
123
146
  */
124
147
  export type PreviousRows<T extends HookedTable> = () => Promise<{
125
148
  rows: HookTables[T]["Row"][]
@@ -142,15 +165,54 @@ export type BeforeDeleteContext<T extends HookedTable> = HookBase & {
142
165
  readonly previous: PreviousRows<T>
143
166
  }
144
167
 
145
- export type AfterChangeContext<T extends HookedTable> = HookBase & {
146
- readonly operation: "insert" | "update"
147
- /** Written rows: present only when the caller sent \`Prefer: return=representation\`. */
148
- readonly rows?: HookTables[T]["Row"][]
168
+ /**
169
+ * A row as an after-hook receives it: every column with a field \`read\` rule is left out, unless
170
+ * the hook declared \`columns: "all"\`.
171
+ */
172
+ export type AfterChangeRow<T extends HookedTable> = Omit<
173
+ HookTables[T]["Row"],
174
+ HookTables[T]["AfterHookHidden"]["afterChange"]
175
+ >
176
+ export type AfterDeleteRow<T extends HookedTable> = Omit<
177
+ HookTables[T]["Row"],
178
+ HookTables[T]["AfterHookHidden"]["afterDelete"]
179
+ >
180
+
181
+ /**
182
+ * An after-hook runs once the write has committed, for every writer: the API, Studio, SQL and
183
+ * functions alike. One call carries every row one transaction changed in one table with one
184
+ * operation, so a bulk insert arrives as a single call.
185
+ *
186
+ * Values are in the JSON shape PostgREST returns for the same row.
187
+ *
188
+ * Delivery is at least once, so the same call can arrive twice.
189
+ */
190
+ interface AfterHookBase extends HookBase {
191
+ /**
192
+ * \`commitLSN:schema.table:operation\`: the same for every delivery of one call. Keep the ones you
193
+ * have handled and drop a repeat.
194
+ */
195
+ readonly deliveryId: string
149
196
  }
150
197
 
151
- export type AfterDeleteContext<T extends HookedTable> = HookBase & {
198
+ export type AfterChangeContext<T extends HookedTable> =
199
+ | (AfterHookBase & {
200
+ readonly operation: "insert"
201
+ /** Every row the transaction inserted into this table. */
202
+ readonly rows: AfterChangeRow<T>[]
203
+ })
204
+ | (AfterHookBase & {
205
+ readonly operation: "update"
206
+ /** Every row the transaction updated in this table, as it is now. */
207
+ readonly rows: AfterChangeRow<T>[]
208
+ /** Aligned by index: \`old[i]\` is \`rows[i]\` as it was before the update. */
209
+ readonly old: AfterChangeRow<T>[]
210
+ })
211
+
212
+ export type AfterDeleteContext<T extends HookedTable> = AfterHookBase & {
152
213
  readonly operation: "delete"
153
- readonly filter: string
214
+ /** Every row the transaction deleted from this table, as it was. */
215
+ readonly old: AfterDeleteRow<T>[]
154
216
  }
155
217
 
156
218
  /** Reject with a message, or with a status of your choosing. */
@@ -288,16 +350,20 @@ type AnyHandler = (ctx: never) => unknown
288
350
  * A hook's context from the server's payload. \`previousPath\` is the server callback **path** for
289
351
  * pre-write rows, absent when there are none to fetch: a path rather than a URL because the server
290
352
  * does not know its own in-network address, while this worker is already told how to reach the stack.
353
+ * \`old\` and \`deliveryId\` come only to an after-hook.
291
354
  */
292
355
  function hookContext(wire: Record<string, unknown>, signature: string | null): Record<string, unknown> {
293
356
  const filter = text(wire, "filter")
294
357
  const previousPath = text(wire, "previousPath")
358
+ const deliveryId = text(wire, "deliveryId")
295
359
  return {
296
360
  table: text(wire, "table") ?? "",
297
361
  operation: text(wire, "operation") ?? "",
298
362
  user: wire["user"] ?? null,
299
363
  requestId: text(wire, "requestId") ?? "",
364
+ ...(deliveryId !== undefined && { deliveryId }),
300
365
  ...(wire["rows"] !== undefined && { rows: wire["rows"] }),
366
+ ...(wire["old"] !== undefined && { old: wire["old"] }),
301
367
  ...(wire["patch"] !== undefined && { patch: wire["patch"] }),
302
368
  ...(filter !== undefined && { filter }),
303
369
  ...(previousPath !== undefined && { previous: () => fetchPrevious(previousPath, signature) }),
@@ -1 +1 @@
1
- {"version":3,"file":"hooks-generator.js","sourceRoot":"","sources":["../src/hooks-generator.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,OAAO,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAC1D,OAAO,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAA;AAC7D,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,eAAe,EACf,kBAAkB,EAClB,gBAAgB,GACjB,MAAM,6BAA6B,CAAA;AAOpC,uEAAuE;AACvE,SAAS,YAAY,CAAC,GAAY;IAChC,OAAO,WAAW,CAAC,GAAG,CAAC;SACpB,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE;QAChB,MAAM,QAAQ,GAAG,QAAQ,CAAC,KAAK,EAAE,aAAa,EAAE,UAAU,CAAC,CAAA;QAC3D,yFAAyF;QACzF,4EAA4E;QAC5E,OAAO,gBAAgB,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,CAAC,IAAI,gBAAgB,CAAC,QAAQ,EAAE,CAAC,UAAU,CAAC,CAAC,CAAA;IAC1F,CAAC,CAAC;QACF,uFAAuF;SACtF,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,gBAAgB,CAAC,KAAK,CAAC,EAAE,MAAM,EAAE,wBAAwB,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;SAC7F,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAA;AACnD,CAAC;AAED;;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SACV,UAAU,CAAC,UAAU,EAAE,eAAe,CAAC;SACvC,UAAU,CACT,yDAAyD,EACzD,eAAe,CAChB,CAAA;AACL,CAAC;AAED,kGAAkG;AAClG,SAAS,MAAM,CAAC,MAAc,EAAE,MAAc;IAC5C,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;IAC9B,OAAO,MAAM;SACV,KAAK,CAAC,IAAI,CAAC;SACX,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,GAAG,GAAG,GAAG,IAAI,EAAE,CAAC,CAAC;SACpD,IAAI,CAAC,IAAI,CAAC,CAAA;AACf,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"init-dependency-versions.d.ts","sourceRoot":"","sources":["../src/init-dependency-versions.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,sBAAsB;IACrC,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,+EAA+E;IAC/E,MAAM,EAAE,MAAM,CAAA;CACf;AAaD,mEAAmE;AACnE,wBAAgB,8BAA8B,IAAI,sBAAsB,CAGvE;AAiDD;;;;GAIG;AACH,wBAAsB,6BAA6B,IAAI,OAAO,CAAC,sBAAsB,CAAC,CAiBrF"}
1
+ {"version":3,"file":"init-dependency-versions.d.ts","sourceRoot":"","sources":["../src/init-dependency-versions.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,sBAAsB;IACrC,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,+EAA+E;IAC/E,MAAM,EAAE,MAAM,CAAA;CACf;AAaD,mEAAmE;AACnE,wBAAgB,8BAA8B,IAAI,sBAAsB,CAGvE;AAiDD;;;;GAIG;AACH,wBAAsB,6BAA6B,IAAI,OAAO,CAAC,sBAAsB,CAAC,CAiBrF"}
@@ -1,4 +1,5 @@
1
1
  import { cliPackageVersion } from "./cli-package-version.js";
2
+ import semver from "semver";
2
3
  import { isRecordOf, isShape, isString, isUnknown, optional } from "@supatype/shared";
3
4
  /** The part of an npm registry document read here. */
4
5
  const isNpmRegistryPackage = isShape({
@@ -70,7 +71,16 @@ export async function resolveInitDependencyVersions() {
70
71
  return {
71
72
  cli: resolvePackageVersion(cliMeta, runningCli),
72
73
  types: resolvePackageVersion(typesMeta, runningCli),
73
- client: resolvePackageVersion(clientMeta, runningCli),
74
+ client: notOlderThan(resolvePackageVersion(clientMeta, runningCli), runningCli),
74
75
  };
75
76
  }
77
+ /**
78
+ * The client carries `supatype-prepare`, the scaffold's `prepare`, and is released with the CLI,
79
+ * so one older than the running CLI may predate it. npm's latest is such a client while this CLI's
80
+ * own release is still publishing, or failed to: then this CLI's version, which npm will not have
81
+ * yet, fails the install on that version, where npm's latest failed it on a missing command.
82
+ */
83
+ function notOlderThan(version, floor) {
84
+ return semver.valid(version) !== null && semver.valid(floor) !== null && semver.lt(version, floor) ? floor : version;
85
+ }
76
86
  //# sourceMappingURL=init-dependency-versions.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"init-dependency-versions.js","sourceRoot":"","sources":["../src/init-dependency-versions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAA;AAC5D,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAc,MAAM,kBAAkB,CAAA;AAcjG,sDAAsD;AACtD,MAAM,oBAAoB,GAA8B,OAAO,CAAC;IAC9D,QAAQ,EAAE,QAAQ,CAAC,UAAU,CAAC,SAAS,CAAC,CAAC;IACzC,WAAW,EAAE,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;CAC/D,CAAC,CAAA;AAEF,mEAAmE;AACnE,MAAM,UAAU,8BAA8B;IAC5C,MAAM,OA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I,CAAC,SAAS,IAAI,CAAC,UAAU,EAAE,CAAC;QAC1C,OAAO,8BAA8B,EAAE,CAAA;IACzC,CAAC;IAED,OAAO;QACL,GAAG,EAAE,qBAAqB,CAAC,OAAO,EAAE,UAAU,CAAC;QAC/C,KAAK,EAAE,qBAAqB,CAAC,SAAS,EAAE,UAAU,CAAC;QACnD,MAAM,EAAE,YAAY,CAAC,qBAAqB,CAAC,UAAU,EAAE,UAAU,CAAC,EAAE,UAAU,CAAC;KAChF,CAAA;AACH,CAAC;AAED;;;;;GAKG;AACH,SAAS,YAAY,CAAC,OAAe,EAAE,KAAa;IAClD,OAAO,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,IAAI,IAAI,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,OAAO,CAAA;AACtH,CAAC"}
@@ -9,13 +9,50 @@ export interface MergePackageJsonOptions {
9
9
  }
10
10
  type PackageJson = {
11
11
  name?: string;
12
+ version?: string;
12
13
  private?: boolean;
13
14
  type?: string;
14
15
  scripts?: Record<string, string>;
15
16
  dependencies?: Record<string, string>;
16
17
  devDependencies?: Record<string, string>;
17
18
  };
18
- /** Merge Supatype deps/scripts into an existing package.json object. */
19
+ /**
20
+ * `supatype generate` when the CLI is installed, and nothing when it is not: a bin of
21
+ * `@supatype/client` (bin/supatype-prepare.js there says how it finds the CLI). The client is a
22
+ * production dependency, so the bin is there on a production install, where the CLI is not.
23
+ *
24
+ * It was once a script written into the project's package.json in full, which no upgrade could
25
+ * fix: each project kept the copy it was made with.
26
+ */
27
+ export declare const PREPARE_COMMAND = "supatype-prepare";
28
+ /**
29
+ * The scripts every Supatype project gets, written by `init` into a new package.json and merged
30
+ * into an existing one, so the two cannot drift: the merge once ran `tsx seed.ts`, which loads the
31
+ * seed module without calling it, while new projects ran `supatype seed`.
32
+ */
33
+ export declare const CORE_SCRIPTS: {
34
+ readonly dev: "supatype dev";
35
+ readonly push: "supatype push";
36
+ readonly seed: "supatype seed";
37
+ readonly prepare: "supatype-prepare";
38
+ };
39
+ /** The Vite major that current `@vitejs/plugin-react` requires as a peer. */
40
+ export declare const VITE_RANGE = "^8";
41
+ /** The package.json `init` writes for a new project. */
42
+ export declare function scaffoldPackageJson(opts: MergePackageJsonOptions, deps: InitDependencyVersions): string;
43
+ /**
44
+ * `prepare` as a Supatype project needs it: {@link PREPARE_COMMAND} in place of the one earlier
45
+ * releases of `init` wrote, after a `prepare` of the project's own (husky, say) that does not run
46
+ * either already, and on its own when there is none. A project's own `prepare` used to be kept as
47
+ * it was, so a fresh clone never generated the gitignored seed builder.
48
+ */
49
+ export declare function supatypePrepare(existing: string | undefined): string;
50
+ /**
51
+ * The script among `scripts` that runs `supatype dev`: `supatype:dev` when the merge put it beside
52
+ * a dev script of the project's own, else `dev`.
53
+ */
54
+ export declare function supatypeDevScript(scripts: unknown): string;
55
+ /** Merge Supatype deps/scripts into an existing package.json object, keeping what it already has. */
19
56
  export declare function mergeSupatypePackageJson(pkg: PackageJson, opts: MergePackageJsonOptions, deps: InitDependencyVersions): PackageJson;
20
57
  export {};
21
58
  //# sourceMappingURL=init-package-json.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"init-package-json.d.ts","sourceRoot":"","sources":["../src/init-package-json.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAA;AAE3E,MAAM,WAAW,uBAAuB;IACtC,WAAW,EAAE,MAAM,CAAA;IACnB,GAAG,EAAE;QAAE,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAA;IAC1C,aAAa,EAAE,OAAO,CAAA;CACvB;AAED,KAAK,WAAW,GAAG;IACjB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,OAAO,CAAC,EAAE,OAAO,CA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,UAAU,OAAO,CAAA;AAgC9B,wDAAwD;AACxD,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,uBAAuB,EAAE,IAAI,EAAE,sBAAsB,GAAG,MAAM,CAWvG;AAiBD;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAIpE;AAMD;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAG1D;AAiCD,qGAAqG;AACrG,wBAAgB,wBAAwB,CACtC,GAAG,EAAE,WAAW,EAChB,IAAI,EAAE,uBAAuB,EAC7B,IAAI,EAAE,sBAAsB,GAC3B,WAAW,CAoBb"}
@@ -1,48 +1,159 @@
1
- const CORE_SCRIPTS = {
1
+ import semver from "semver";
2
+ import { isJsonObject } from "@supatype/shared";
3
+ /**
4
+ * `supatype generate` when the CLI is installed, and nothing when it is not: a bin of
5
+ * `@supatype/client` (bin/supatype-prepare.js there says how it finds the CLI). The client is a
6
+ * production dependency, so the bin is there on a production install, where the CLI is not.
7
+ *
8
+ * It was once a script written into the project's package.json in full, which no upgrade could
9
+ * fix: each project kept the copy it was made with.
10
+ */
11
+ export const PREPARE_COMMAND = "supatype-prepare";
12
+ /**
13
+ * The scripts every Supatype project gets, written by `init` into a new package.json and merged
14
+ * into an existing one, so the two cannot drift: the merge once ran `tsx seed.ts`, which loads the
15
+ * seed module without calling it, while new projects ran `supatype seed`.
16
+ */
17
+ export const CORE_SCRIPTS = {
2
18
  dev: "supatype dev",
3
19
  push: "supatype push",
4
- seed: "tsx seed.ts",
20
+ seed: "supatype seed",
21
+ prepare: PREPARE_COMMAND,
5
22
  };
6
- /** Merge Supatype deps/scripts into an existing package.json object. */
23
+ /**
24
+ * Vite serves the app in development, and `build` writes the `./dist` that `app.static_dir` serves
25
+ * in production. Without `build` nothing ever produced `./dist`.
26
+ */
27
+ const VITE_SCRIPTS = { vite: "vite", build: "vite build" };
28
+ const FUNCTIONS_SCRIPTS = { functions: "supatype functions serve" };
29
+ /** The Vite major that current `@vitejs/plugin-react` requires as a peer. */
30
+ export const VITE_RANGE = "^8";
31
+ /**
32
+ * Development-time only, so devDependencies. schema/index.ts imports @supatype/types with
33
+ * `import type`, which is erased at build; supatype.config.ts imports defineConfig, which only the
34
+ * CLI evaluates; seed.ts is run by `supatype seed`. Nothing `supatype push` or `supatype generate`
35
+ * writes refers to either package. The generated client imports @supatype/client at runtime, so
36
+ * that one is a real dependency.
37
+ *
38
+ * In dependencies they were installed by `npm install --omit=dev`, so every production install of
39
+ * a Supatype app pulled the CLI to support three files that never run in production.
40
+ *
41
+ * No tsx: the CLI's program runs the project's TypeScript itself. TypeScript stays for the editor
42
+ * and for `tsc`, and `defineConfig`'s module imports it.
43
+ */
44
+ function toolDependencies(deps, opts) {
45
+ return {
46
+ "@supatype/cli": `^${deps.cli}`,
47
+ "@supatype/types": `^${deps.types}`,
48
+ typescript: "^5",
49
+ ...(opts.app.viteDevUrl ? { vite: VITE_RANGE } : {}),
50
+ };
51
+ }
52
+ function projectScripts(opts) {
53
+ return {
54
+ ...CORE_SCRIPTS,
55
+ ...(opts.app.viteDevUrl ? VITE_SCRIPTS : {}),
56
+ ...(opts.helloFunction ? FUNCTIONS_SCRIPTS : {}),
57
+ };
58
+ }
59
+ /** The package.json `init` writes for a new project. */
60
+ export function scaffoldPackageJson(opts, deps) {
61
+ const pkg = {
62
+ name: opts.projectName,
63
+ version: NEW_PROJECT_VERSION,
64
+ private: true,
65
+ type: "module",
66
+ scripts: projectScripts(opts),
67
+ dependencies: { [CLIENT]: clientRange(undefined, deps.client) },
68
+ devDependencies: toolDependencies(deps, opts),
69
+ };
70
+ return `${JSON.stringify(pkg, null, 2)}\n`;
71
+ }
72
+ /** Each of `wanted` that none of `present` already names. */
73
+ function absentFrom(wanted, ...present) {
74
+ return Object.fromEntries(Object.entries(wanted).filter(([key]) => present.every((p) => p?.[key] === undefined)));
75
+ }
76
+ const CLIENT = "@supatype/client";
77
+ /** The `prepare` earlier releases of `init` wrote, which fails every production install. */
78
+ const EARLIER_PREPARE = "supatype generate";
79
+ /** The version `init` writes for a new project, or adds to an existing one that has none. */
80
+ const NEW_PROJECT_VERSION = "0.1.0";
81
+ /**
82
+ * `prepare` as a Supatype project needs it: {@link PREPARE_COMMAND} in place of the one earlier
83
+ * releases of `init` wrote, after a `prepare` of the project's own (husky, say) that does not run
84
+ * either already, and on its own when there is none. A project's own `prepare` used to be kept as
85
+ * it was, so a fresh clone never generated the gitignored seed builder.
86
+ */
87
+ export function supatypePrepare(existing) {
88
+ if (existing === undefined || existing.trim() === "" || existing.trim() === EARLIER_PREPARE)
89
+ return PREPARE_COMMAND;
90
+ if (existing.includes(PREPARE_COMMAND) || existing.includes(EARLIER_PREPARE))
91
+ return existing;
92
+ return `${existing} && ${PREPARE_COMMAND}`;
93
+ }
94
+ function withPrepare(scripts) {
95
+ return { ...scripts, prepare: supatypePrepare(scripts?.["prepare"]) };
96
+ }
97
+ /**
98
+ * The script among `scripts` that runs `supatype dev`: `supatype:dev` when the merge put it beside
99
+ * a dev script of the project's own, else `dev`.
100
+ */
101
+ export function supatypeDevScript(scripts) {
102
+ const named = (key) => isJsonObject(scripts) && scripts[key] === CORE_SCRIPTS.dev;
103
+ return !named("dev") && named("supatype:dev") ? "supatype:dev" : "dev";
104
+ }
105
+ /**
106
+ * The project's @supatype/client range, as `prepare` needs it: at least `floor`, the client `init`
107
+ * would pick, since an older one may predate `supatype-prepare`. A range whose lowest version, as
108
+ * npm's own `semver` reads it, is at or above the floor is kept. So is a spec that is not a range,
109
+ * such as `workspace:*`, a path, a git URL or a tag. Any other range has its lower end raised and
110
+ * keeps its upper end, so a project on a newer client than the floor is not moved down to it:
111
+ * `>=0.5.0` becomes `>=0.5.0 >=0.9.0`. A range with nothing left at or above the floor becomes
112
+ * `^floor`.
113
+ */
114
+ function clientRange(listed, floor) {
115
+ if (listed === undefined)
116
+ return `^${floor}`;
117
+ if (semver.validRange(listed) === null || semver.valid(floor) === null)
118
+ return listed;
119
+ const lowest = semver.minVersion(listed);
120
+ if (lowest !== null && semver.gte(lowest, floor))
121
+ return listed;
122
+ const raised = listed
123
+ .split("||")
124
+ .map((alternative) => `${alternative.trim()} >=${floor}`)
125
+ .filter((alternative) => semver.minVersion(alternative) !== null);
126
+ return raised.length > 0 ? raised.join(" || ") : `^${floor}`;
127
+ }
128
+ /**
129
+ * The client in `dependencies` at a range `prepare` can run with, and out of `devDependencies`: a
130
+ * production install leaves those out, and `supatype-prepare` with them.
131
+ */
132
+ function withClient(pkg, floor) {
133
+ const { [CLIENT]: listedDev, ...devDependencies } = pkg.devDependencies ?? {};
134
+ const range = clientRange(pkg.dependencies?.[CLIENT] ?? listedDev, floor);
135
+ return { dependencies: { ...pkg.dependencies, [CLIENT]: range }, devDependencies };
136
+ }
137
+ /** Merge Supatype deps/scripts into an existing package.json object, keeping what it already has. */
7
138
  export function mergeSupatypePackageJson(pkg, opts, deps) {
8
- const next = { ...pkg };
9
- if (!next.name)
10
- next.name = opts.projectName;
11
- if (next.private === undefined)
12
- next.private = true;
13
- if (!next.type)
14
- next.type = "module";
15
- const scripts = { ...(next.scripts ?? {}) };
16
- for (const [key, value] of Object.entries(CORE_SCRIPTS)) {
17
- if (scripts[key] === undefined)
18
- scripts[key] = value;
19
- }
139
+ const scripts = { ...withPrepare(pkg.scripts), ...absentFrom(projectScripts(opts), pkg.scripts) };
20
140
  if (scripts.dev !== CORE_SCRIPTS.dev && scripts["supatype:dev"] === undefined) {
21
141
  scripts["supatype:dev"] = CORE_SCRIPTS.dev;
22
142
  }
23
- if (opts.app.viteDevUrl && scripts.vite === undefined) {
24
- scripts.vite = "vite";
25
- }
26
- if (opts.helloFunction && scripts.functions === undefined) {
27
- scripts.functions = "supatype functions serve";
28
- }
29
- next.scripts = scripts;
30
- const dependencies = { ...(next.dependencies ?? {}) };
31
- if (dependencies["@supatype/cli"] === undefined)
32
- dependencies["@supatype/cli"] = `^${deps.cli}`;
33
- if (dependencies["@supatype/types"] === undefined)
34
- dependencies["@supatype/types"] = `^${deps.types}`;
35
- if (dependencies["@supatype/client"] === undefined)
36
- dependencies["@supatype/client"] = `^${deps.client}`;
37
- next.dependencies = dependencies;
38
- const devDependencies = { ...(next.devDependencies ?? {}) };
39
- if (devDependencies.tsx === undefined)
40
- devDependencies.tsx = "^4.19.2";
41
- if (devDependencies.typescript === undefined)
42
- devDependencies.typescript = "^5";
43
- if (opts.app.viteDevUrl && devDependencies.vite === undefined)
44
- devDependencies.vite = "^6";
45
- next.devDependencies = devDependencies;
46
- return next;
143
+ const { dependencies, devDependencies } = withClient(pkg, deps.client);
144
+ return {
145
+ ...pkg,
146
+ name: pkg.name || opts.projectName,
147
+ version: pkg.version || NEW_PROJECT_VERSION,
148
+ private: pkg.private ?? true,
149
+ type: pkg.type || "module",
150
+ scripts,
151
+ dependencies,
152
+ // A tool the project already lists in either section stays where it is.
153
+ devDependencies: {
154
+ ...devDependencies,
155
+ ...absentFrom(toolDependencies(deps, opts), pkg.dependencies, pkg.devDependencies),
156
+ },
157
+ };
47
158
  }
48
159
  //# sourceMappingURL=init-package-json.js.map