@dual-os/cli 0.0.0-stage → 0.11.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 (254) hide show
  1. package/LICENSE +4 -0
  2. package/README.md +15 -2
  3. package/dist/_virtual/_rolldown/runtime.js +13 -0
  4. package/dist/auth/credentials.d.ts +17 -0
  5. package/dist/auth/credentials.d.ts.map +1 -0
  6. package/dist/auth/credentials.js +57 -0
  7. package/dist/auth/credentials.js.map +1 -0
  8. package/dist/auth/destination.d.ts +22 -0
  9. package/dist/auth/destination.d.ts.map +1 -0
  10. package/dist/auth/destination.js +49 -0
  11. package/dist/auth/destination.js.map +1 -0
  12. package/dist/auth/http.d.ts +12 -0
  13. package/dist/auth/http.d.ts.map +1 -0
  14. package/dist/auth/http.js +34 -0
  15. package/dist/auth/http.js.map +1 -0
  16. package/dist/auth/login.d.ts +22 -0
  17. package/dist/auth/login.d.ts.map +1 -0
  18. package/dist/auth/login.js +98 -0
  19. package/dist/auth/login.js.map +1 -0
  20. package/dist/auth/protocol.d.ts +26 -0
  21. package/dist/auth/protocol.d.ts.map +1 -0
  22. package/dist/auth/protocol.js +31 -0
  23. package/dist/auth/protocol.js.map +1 -0
  24. package/dist/baseline.d.ts +18 -0
  25. package/dist/baseline.d.ts.map +1 -0
  26. package/dist/baseline.js +22 -0
  27. package/dist/baseline.js.map +1 -0
  28. package/dist/bin.d.ts +1 -0
  29. package/dist/bin.js +7 -0
  30. package/dist/bin.js.map +1 -0
  31. package/dist/cli.d.ts +19 -0
  32. package/dist/cli.d.ts.map +1 -0
  33. package/dist/cli.js +492 -0
  34. package/dist/cli.js.map +1 -0
  35. package/dist/create/choices.d.ts +58 -0
  36. package/dist/create/choices.d.ts.map +1 -0
  37. package/dist/create/choices.js +121 -0
  38. package/dist/create/choices.js.map +1 -0
  39. package/dist/create/deployment.d.ts +48 -0
  40. package/dist/create/deployment.d.ts.map +1 -0
  41. package/dist/create/deployment.js +66 -0
  42. package/dist/create/deployment.js.map +1 -0
  43. package/dist/create/scaffold.d.ts +57 -0
  44. package/dist/create/scaffold.d.ts.map +1 -0
  45. package/dist/create/scaffold.js +198 -0
  46. package/dist/create/scaffold.js.map +1 -0
  47. package/dist/create/source-reference.d.ts +39 -0
  48. package/dist/create/source-reference.d.ts.map +1 -0
  49. package/dist/create/source-reference.js +185 -0
  50. package/dist/create/source-reference.js.map +1 -0
  51. package/dist/create/template.d.ts +116 -0
  52. package/dist/create/template.d.ts.map +1 -0
  53. package/dist/create/template.js +211 -0
  54. package/dist/create/template.js.map +1 -0
  55. package/dist/diagnostics.d.ts +6 -0
  56. package/dist/diagnostics.d.ts.map +1 -0
  57. package/dist/diagnostics.js +18 -0
  58. package/dist/diagnostics.js.map +1 -0
  59. package/dist/help.d.ts +16 -0
  60. package/dist/help.d.ts.map +1 -0
  61. package/dist/help.js +73 -0
  62. package/dist/help.js.map +1 -0
  63. package/dist/openapi/bundle.d.ts +18 -0
  64. package/dist/openapi/bundle.d.ts.map +1 -0
  65. package/dist/openapi/bundle.js +201 -0
  66. package/dist/openapi/bundle.js.map +1 -0
  67. package/dist/openapi/discovery.d.ts +30 -0
  68. package/dist/openapi/discovery.d.ts.map +1 -0
  69. package/dist/openapi/discovery.js +325 -0
  70. package/dist/openapi/discovery.js.map +1 -0
  71. package/dist/openapi/emit.d.ts +52 -0
  72. package/dist/openapi/emit.d.ts.map +1 -0
  73. package/dist/openapi/emit.js +596 -0
  74. package/dist/openapi/emit.js.map +1 -0
  75. package/dist/openapi/graphql.d.ts +102 -0
  76. package/dist/openapi/graphql.d.ts.map +1 -0
  77. package/dist/openapi/graphql.js +294 -0
  78. package/dist/openapi/graphql.js.map +1 -0
  79. package/dist/openapi/identity.d.ts +88 -0
  80. package/dist/openapi/identity.d.ts.map +1 -0
  81. package/dist/openapi/identity.js +98 -0
  82. package/dist/openapi/identity.js.map +1 -0
  83. package/dist/openapi/mcp.d.ts +21 -0
  84. package/dist/openapi/mcp.d.ts.map +1 -0
  85. package/dist/openapi/mcp.js +133 -0
  86. package/dist/openapi/mcp.js.map +1 -0
  87. package/dist/openapi/operations.d.ts +165 -0
  88. package/dist/openapi/operations.d.ts.map +1 -0
  89. package/dist/openapi/operations.js +361 -0
  90. package/dist/openapi/operations.js.map +1 -0
  91. package/dist/openapi/overlay.d.ts +23 -0
  92. package/dist/openapi/overlay.d.ts.map +1 -0
  93. package/dist/openapi/overlay.js +115 -0
  94. package/dist/openapi/overlay.js.map +1 -0
  95. package/dist/openapi/plugin.d.ts +83 -0
  96. package/dist/openapi/plugin.d.ts.map +1 -0
  97. package/dist/openapi/plugin.js +468 -0
  98. package/dist/openapi/plugin.js.map +1 -0
  99. package/dist/openapi/polymorphism.d.ts +21 -0
  100. package/dist/openapi/polymorphism.d.ts.map +1 -0
  101. package/dist/openapi/polymorphism.js +96 -0
  102. package/dist/openapi/polymorphism.js.map +1 -0
  103. package/dist/openapi/postman.d.ts +21 -0
  104. package/dist/openapi/postman.d.ts.map +1 -0
  105. package/dist/openapi/postman.js +286 -0
  106. package/dist/openapi/postman.js.map +1 -0
  107. package/dist/openapi/schema.d.ts +178 -0
  108. package/dist/openapi/schema.d.ts.map +1 -0
  109. package/dist/openapi/schema.js +1234 -0
  110. package/dist/openapi/schema.js.map +1 -0
  111. package/dist/openapi/select.d.ts +22 -0
  112. package/dist/openapi/select.d.ts.map +1 -0
  113. package/dist/openapi/select.js +79 -0
  114. package/dist/openapi/select.js.map +1 -0
  115. package/dist/openapi/spec.d.ts +147 -0
  116. package/dist/openapi/spec.d.ts.map +1 -0
  117. package/dist/openapi/spec.js +263 -0
  118. package/dist/openapi/spec.js.map +1 -0
  119. package/dist/openapi/swagger.d.ts +166 -0
  120. package/dist/openapi/swagger.d.ts.map +1 -0
  121. package/dist/openapi/swagger.js +298 -0
  122. package/dist/openapi/swagger.js.map +1 -0
  123. package/dist/openapi/versions.d.ts +52 -0
  124. package/dist/openapi/versions.d.ts.map +1 -0
  125. package/dist/openapi/versions.js +120 -0
  126. package/dist/openapi/versions.js.map +1 -0
  127. package/dist/plugin/archive.d.ts +27 -0
  128. package/dist/plugin/archive.d.ts.map +1 -0
  129. package/dist/plugin/archive.js +170 -0
  130. package/dist/plugin/archive.js.map +1 -0
  131. package/dist/plugin/bundle.d.ts +27 -0
  132. package/dist/plugin/bundle.d.ts.map +1 -0
  133. package/dist/plugin/bundle.js +328 -0
  134. package/dist/plugin/bundle.js.map +1 -0
  135. package/dist/plugin/init.d.ts +18 -0
  136. package/dist/plugin/init.d.ts.map +1 -0
  137. package/dist/plugin/init.js +121 -0
  138. package/dist/plugin/init.js.map +1 -0
  139. package/dist/plugin/inspection.d.ts +24 -0
  140. package/dist/plugin/inspection.d.ts.map +1 -0
  141. package/dist/plugin/inspection.js +24 -0
  142. package/dist/plugin/inspection.js.map +1 -0
  143. package/dist/plugin/package.d.ts +26 -0
  144. package/dist/plugin/package.d.ts.map +1 -0
  145. package/dist/plugin/package.js +73 -0
  146. package/dist/plugin/package.js.map +1 -0
  147. package/dist/plugin/publication.d.ts +29 -0
  148. package/dist/plugin/publication.d.ts.map +1 -0
  149. package/dist/plugin/publication.js +65 -0
  150. package/dist/plugin/publication.js.map +1 -0
  151. package/dist/plugin/validation.d.ts +15 -0
  152. package/dist/plugin/validation.d.ts.map +1 -0
  153. package/dist/plugin/validation.js +119 -0
  154. package/dist/plugin/validation.js.map +1 -0
  155. package/dist/release/archives.d.ts +33 -0
  156. package/dist/release/archives.d.ts.map +1 -0
  157. package/dist/release/archives.js +81 -0
  158. package/dist/release/archives.js.map +1 -0
  159. package/dist/release/release-manifest.d.ts +182 -0
  160. package/dist/release/release-manifest.d.ts.map +1 -0
  161. package/dist/release/release-manifest.js +79 -0
  162. package/dist/release/release-manifest.js.map +1 -0
  163. package/dist/release/releases.d.ts +36 -0
  164. package/dist/release/releases.d.ts.map +1 -0
  165. package/dist/release/releases.js +122 -0
  166. package/dist/release/releases.js.map +1 -0
  167. package/dist/skills.d.ts +35 -0
  168. package/dist/skills.d.ts.map +1 -0
  169. package/dist/skills.js +257 -0
  170. package/dist/skills.js.map +1 -0
  171. package/package.json +73 -4
  172. package/skills/dual-fde/SKILL.md +38 -0
  173. package/skills/dual-fde/references/access.md +62 -0
  174. package/skills/dual-fde/references/author-plugins.md +54 -0
  175. package/skills/dual-fde/references/cli.md +97 -0
  176. package/skills/dual-fde/references/manage-plugins.md +78 -0
  177. package/skills/dual-fde/references/migrations-deployment.md +84 -0
  178. package/skills/dual-fde/references/openapi-plugins.md +47 -0
  179. package/skills/dual-fde/references/sdk-docs.md +56 -0
  180. package/skills/dual-fde/references/upgrades.md +115 -0
  181. package/skills/dual-fde/references/working-context.md +69 -0
  182. package/src/auth/credentials.ts +84 -0
  183. package/src/auth/destination.ts +83 -0
  184. package/src/auth/http.ts +39 -0
  185. package/src/auth/login.ts +177 -0
  186. package/src/auth/protocol.ts +37 -0
  187. package/src/baseline.ts +15 -0
  188. package/src/bin.ts +4 -0
  189. package/src/cli.ts +969 -0
  190. package/src/create/choices.ts +177 -0
  191. package/src/create/deployment.ts +96 -0
  192. package/src/create/scaffold.ts +334 -0
  193. package/src/create/source-reference.ts +212 -0
  194. package/src/create/template.ts +300 -0
  195. package/src/diagnostics.ts +8 -0
  196. package/src/help.ts +142 -0
  197. package/src/openapi/bundle.ts +290 -0
  198. package/src/openapi/discovery.ts +438 -0
  199. package/src/openapi/emit.ts +1031 -0
  200. package/src/openapi/graphql.ts +375 -0
  201. package/src/openapi/identity.ts +153 -0
  202. package/src/openapi/mcp.ts +165 -0
  203. package/src/openapi/operations.ts +781 -0
  204. package/src/openapi/overlay.ts +195 -0
  205. package/src/openapi/plugin.ts +856 -0
  206. package/src/openapi/polymorphism.ts +207 -0
  207. package/src/openapi/postman.ts +403 -0
  208. package/src/openapi/schema.ts +2138 -0
  209. package/src/openapi/select.ts +113 -0
  210. package/src/openapi/spec.ts +401 -0
  211. package/src/openapi/swagger.ts +422 -0
  212. package/src/openapi/versions.ts +174 -0
  213. package/src/plugin/archive.ts +244 -0
  214. package/src/plugin/bundle.ts +572 -0
  215. package/src/plugin/init.ts +178 -0
  216. package/src/plugin/inspection.ts +19 -0
  217. package/src/plugin/package.ts +140 -0
  218. package/src/plugin/publication.ts +128 -0
  219. package/src/plugin/validation.ts +213 -0
  220. package/src/release/archives.ts +133 -0
  221. package/src/release/release-manifest.ts +79 -0
  222. package/src/release/releases.ts +210 -0
  223. package/src/skills.ts +434 -0
  224. package/templates/compose/.env.compose.example.hbs +3 -0
  225. package/templates/compose/.env.host.example.hbs +4 -0
  226. package/templates/compose/Dockerfile.hbs +12 -0
  227. package/templates/compose/OPERATIONS.md.hbs +159 -0
  228. package/templates/compose/_dockerignore +14 -0
  229. package/templates/compose/compose.external.yaml +7 -0
  230. package/templates/compose/compose.host-linux.yaml +5 -0
  231. package/templates/compose/compose.host.yaml.hbs +15 -0
  232. package/templates/compose/compose.yaml.hbs +85 -0
  233. package/templates/compose/release.env.hbs +2 -0
  234. package/templates/default/.env.example.hbs +79 -0
  235. package/templates/default/README.md.hbs +150 -0
  236. package/templates/default/_gitignore +11 -0
  237. package/templates/default/packages/customer-plugin/src/Customer.ts.hbs +17 -0
  238. package/templates/default/packages/customer-plugin/tsconfig.json +15 -0
  239. package/templates/default/packages/execution-layer/src/App.ts.hbs +7 -0
  240. package/templates/default/packages/execution-layer/src/Host.ts.hbs +31 -0
  241. package/templates/default/packages/execution-layer/src/Telemetry.ts.hbs +23 -0
  242. package/templates/default/packages/execution-layer/src/api.ts.hbs +21 -0
  243. package/templates/default/packages/execution-layer/src/bootstrap.ts +22 -0
  244. package/templates/default/packages/execution-layer/src/migrate.ts +11 -0
  245. package/templates/default/packages/execution-layer/src/registry-check.ts +29 -0
  246. package/templates/default/packages/execution-layer/src/registry-sync.ts +14 -0
  247. package/templates/default/packages/execution-layer/src/worker.ts.hbs +16 -0
  248. package/templates/default/packages/execution-layer/test/app.host-test.ts.hbs +58 -0
  249. package/templates/default/packages/execution-layer/test/app.test.ts.hbs +16 -0
  250. package/templates/default/tsconfig.json +5 -0
  251. package/templates/default/vitest.config.ts +5 -0
  252. package/templates/default/vitest.host.config.ts +6 -0
  253. package/templates/opensandbox/OPEN-SANDBOX.md +25 -0
  254. package/templates/opensandbox/docker/agent-sandbox.Dockerfile +41 -0
@@ -0,0 +1,177 @@
1
+ import { Array, Schema, String } from "effect";
2
+
3
+ import { toASCII } from "tr46";
4
+
5
+ import { Baseline } from "../baseline.ts";
6
+
7
+ // Generator inputs enter source/config files, so reject Caddy/YAML fragments,
8
+ // credentials, paths and ports in the hostname. Only local HTTP is generated.
9
+ export const Hostname = Schema.String.check(
10
+ Schema.isMaxLength(253),
11
+ Schema.isPattern(
12
+ /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/,
13
+ ),
14
+ Schema.makeFilter(
15
+ hostname => {
16
+ // Node can pass invalid A-labels through where Bun rejects them. Use
17
+ // nontransitional URL IDNA rules, not stricter registration rules (which
18
+ // would reject ordinary ab--cd labels). Keep one unchanged ASCII value.
19
+ if (toASCII(hostname, { checkBidi: true, checkJoiners: true }) !== hostname) return false;
20
+ // The actual HTTP parser also rejects numeric-ending domains and rewrites
21
+ // IPv4 aliases. Canonical IPv4 and ordinary DNS names remain supported.
22
+ const origin = `http://${hostname}`;
23
+ return URL.canParse(origin) && new URL(origin).hostname === hostname;
24
+ },
25
+ { message: "Expected a canonical HTTP hostname with valid IDNA labels" },
26
+ ),
27
+ );
28
+ export const Port = Schema.Int.check(Schema.isBetween({ maximum: 65535, minimum: 1 }));
29
+ // Display text is encoded as a string literal when emitted into source. Identifiers
30
+ // deliberately use a smaller alphabet suitable for npm, runtime IDs and config.
31
+ export const DisplayName = Schema.String.check(
32
+ Schema.isMinLength(1),
33
+ Schema.isMaxLength(120),
34
+ Schema.makeFilter(
35
+ value => String.trim(value) === value && !/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]/u.test(value),
36
+ ),
37
+ );
38
+ export const Slug = Schema.String.check(
39
+ Schema.isMaxLength(80),
40
+ Schema.isPattern(/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/),
41
+ );
42
+ export const PackageScope = Schema.String.check(
43
+ Schema.isMaxLength(81),
44
+ Schema.isPattern(/^@[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/),
45
+ Schema.makeFilter(scope => !Array.contains(["@dual", "@dual-os", "@effect", "@types"], scope), {
46
+ message: "Use a project scope, not @dual, @dual-os, @effect or @types",
47
+ }),
48
+ );
49
+ export const PackageName = Schema.String.check(
50
+ Schema.isMaxLength(214),
51
+ Schema.isPattern(/^(?:@[a-z][a-z0-9]*(?:-[a-z0-9]+)*\/)?[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/),
52
+ Schema.makeFilter(
53
+ name => !Object.hasOwn(Baseline.versions, name) && !/^@(dual|effect|types)\//.test(name),
54
+ { message: "Root package name must not shadow the runtime or declaration packages" },
55
+ ),
56
+ );
57
+ export const Identity = Schema.Struct({
58
+ appId: Slug,
59
+ name: DisplayName,
60
+ packageName: PackageName,
61
+ packageScope: PackageScope,
62
+ pluginId: Slug,
63
+ slug: Slug,
64
+ });
65
+ export type Identity = typeof Identity.Type;
66
+
67
+ /** A suggestion only: explicit slugs are validated, never silently rewritten. */
68
+ export const suggestSlug = (name: string): string => {
69
+ const slug = String.replace(
70
+ /-+$/g,
71
+ "",
72
+ )(
73
+ String.slice(
74
+ 0,
75
+ 80,
76
+ )(
77
+ String.replace(
78
+ /^-+|-+$/g,
79
+ "",
80
+ )(
81
+ String.replace(
82
+ /[^a-z0-9]+/g,
83
+ "-",
84
+ )(String.toLowerCase(String.replace(/\p{M}/gu, "")(String.normalize("NFKD")(name)))),
85
+ ),
86
+ ),
87
+ );
88
+ return /^[a-z]/.test(slug) ? slug : "customer";
89
+ };
90
+ export const identity = (
91
+ input: { readonly [Key in keyof Identity]?: Identity[Key] | undefined } = {},
92
+ ): Identity => {
93
+ const name = input.name ?? "Customer";
94
+ const slug = input.slug ?? suggestSlug(name);
95
+ return {
96
+ appId: input.appId ?? slug,
97
+ name,
98
+ packageName: input.packageName ?? `dual-${slug}`,
99
+ packageScope: input.packageScope ?? `@${slug}`,
100
+ pluginId: input.pluginId ?? slug,
101
+ slug,
102
+ };
103
+ };
104
+
105
+ /** Explicit ports win; defaults avoid all explicit ports, including later ones. */
106
+ export const ports = (input: {
107
+ readonly apiPort?: number | undefined;
108
+ readonly httpPort?: number | undefined;
109
+ readonly postgresPort?: number | undefined;
110
+ }) => {
111
+ const free = (preferred: number, occupied: readonly number[]): number =>
112
+ Array.contains(occupied, preferred)
113
+ ? free(preferred === 65535 ? 1024 : preferred + 1, occupied)
114
+ : preferred;
115
+ const apiPort =
116
+ input.apiPort ??
117
+ free(3000, [
118
+ ...(input.httpPort === undefined ? [] : [input.httpPort]),
119
+ ...(input.postgresPort === undefined ? [] : [input.postgresPort]),
120
+ ]);
121
+ const httpPort =
122
+ input.httpPort ??
123
+ free(3001, [apiPort, ...(input.postgresPort === undefined ? [] : [input.postgresPort])]);
124
+ const postgresPort = input.postgresPort ?? free(55432, [apiPort, httpPort]);
125
+ return { apiPort, httpPort, postgresPort };
126
+ };
127
+ const distinctPorts = Schema.makeFilter(
128
+ (host: { readonly apiPort: number; readonly httpPort: number; readonly postgresPort: number }) =>
129
+ new Set([host.apiPort, host.httpPort, host.postgresPort]).size === 3,
130
+ { message: "API, HTTP proxy and PostgreSQL host ports must be distinct" },
131
+ );
132
+ export const Host = Schema.Struct({
133
+ ...Identity.fields,
134
+ apiPort: Port,
135
+ database: Schema.Literal("postgresql"),
136
+ email: Schema.Literals(["logging-only-no-delivery", "smtp"]),
137
+ fileStorage: Schema.Literals(["local", "s3"]),
138
+ hostname: Hostname,
139
+ httpPort: Port,
140
+ postgresPort: Port,
141
+ preset: Schema.Literals(["customer", "playground"]),
142
+ runtime: Schema.Literal("bun"),
143
+ telemetry: Schema.Literals(["opt-in", "disabled"]),
144
+ workspace: Schema.Literals(["just-bash", "opensandbox"]),
145
+ }).check(
146
+ distinctPorts,
147
+ Schema.makeFilter(
148
+ host =>
149
+ host.packageName !== `${host.packageScope}/plugin` &&
150
+ host.packageName !== `${host.packageScope}/execution-layer`,
151
+ { message: "Root and workspace package names must be distinct" },
152
+ ),
153
+ );
154
+ export type Host = typeof Host.Type;
155
+ export const defaults: Host = {
156
+ ...identity(),
157
+ apiPort: 3000,
158
+ database: "postgresql",
159
+ email: "logging-only-no-delivery",
160
+ fileStorage: "local",
161
+ hostname: "localhost",
162
+ httpPort: 3001,
163
+ postgresPort: 55432,
164
+ preset: "customer",
165
+ runtime: "bun",
166
+ telemetry: "opt-in",
167
+ workspace: "just-bash",
168
+ };
169
+
170
+ /** Resolve once, then record every choice; replay never re-applies defaults. */
171
+ export const forPreset = (preset: Host["preset"]): Host => ({
172
+ ...defaults,
173
+ preset,
174
+ workspace: preset === "playground" ? "opensandbox" : "just-bash",
175
+ });
176
+
177
+ export * as Choices from "./choices.ts";
@@ -0,0 +1,96 @@
1
+ import { Option, Schema, String } from "effect";
2
+
3
+ import semver from "semver";
4
+
5
+ import { Baseline } from "../baseline.ts";
6
+
7
+ const Digest = Schema.String.check(
8
+ Schema.isBetweenLength(71, 71),
9
+ Schema.isPattern(/^sha256:[a-f0-9]{64}$/),
10
+ );
11
+
12
+ // Docker distribution/reference grammar: separators belong between nonempty
13
+ // alphanumeric runs. Only a registry may contain a port; repositories are lowercase.
14
+ const pathComponent = "[a-z0-9]+(?:(?:[._]|__|-+)[a-z0-9]+)*";
15
+ const domainComponent = "[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?";
16
+ const host = `(?:${domainComponent}(?:\\.${domainComponent})*|\\[[a-fA-F0-9:]+\\])`;
17
+ const registry = `${host}(?::[0-9]+)?`;
18
+ // Docker adds its default registry/library namespace before checking name length.
19
+ // Validate the normalized length while preserving the user's immutable reference.
20
+ const normalizedNameLength = (name: string): number => {
21
+ const slash = Option.getOrElse(String.indexOf("/")(name), () => -1);
22
+ const first = slash === -1 ? name : String.slice(0, slash)(name);
23
+ const explicitRegistry =
24
+ slash !== -1 &&
25
+ (first === "localhost" ||
26
+ String.includes(".")(first) ||
27
+ String.includes(":")(first) ||
28
+ first !== String.toLowerCase(first));
29
+ const domain = explicitRegistry && first !== "index.docker.io" ? first : "docker.io";
30
+ const path = explicitRegistry ? String.slice(slash + 1)(name) : name;
31
+ const repository = domain === "docker.io" && !path.includes("/") ? `library/${path}` : path;
32
+ return `${domain}/${repository}`.length;
33
+ };
34
+ const Name = Schema.String.check(
35
+ Schema.isMaxLength(255),
36
+ Schema.isTrimmed(),
37
+ Schema.isPattern(new RegExp(`^(?:${registry}/)?${pathComponent}(?:/${pathComponent})*$`)),
38
+ Schema.makeFilter(name => normalizedNameLength(name) <= 255, {
39
+ message: "Docker-normalized repository name must not exceed 255 characters",
40
+ }),
41
+ );
42
+ const Tag = Schema.String.check(
43
+ Schema.isMaxLength(128),
44
+ Schema.isTrimmed(),
45
+ Schema.isPattern(/^[a-zA-Z0-9_][a-zA-Z0-9_.-]*$/),
46
+ );
47
+ export const Image = Schema.Union([
48
+ Digest,
49
+ Schema.TemplateLiteral([Name, "@", Digest]),
50
+ Schema.TemplateLiteral([Name, ":", Tag, "@", Digest]),
51
+ ]);
52
+ // Release pairs are portable; CLI/template compatibility is a separate manifest gate.
53
+ export const CurrentPair = Schema.Struct({
54
+ commit: Schema.String.check(Schema.isPattern(/^[a-f0-9]{40}$/)),
55
+ coreVersion: Schema.String.check(Schema.makeFilter(value => semver.valid(value) === value)),
56
+ imageDigest: Digest,
57
+ webImage: Image,
58
+ }).check(
59
+ Schema.makeFilter(
60
+ pair =>
61
+ pair.webImage ===
62
+ `registry.datapizza.tech/dualos/web:core-${pair.coreVersion}-${pair.commit}@${pair.imageDigest}`,
63
+ {
64
+ message: "Release image must match the official repository, core version, commit and digest",
65
+ },
66
+ ),
67
+ );
68
+
69
+ // Legacy explicit mappings remain readable.
70
+ export const Pair = Schema.Struct({
71
+ commit: Schema.String.check(Schema.isPattern(/^[a-f0-9]{40}$/)),
72
+ coreVersion: Schema.Literal(Baseline.versions["@dual/core"]),
73
+ imageDigest: Digest,
74
+ webImage: Schema.optionalKey(Image),
75
+ }).check(
76
+ Schema.makeFilter(
77
+ pair => pair.webImage === undefined || String.endsWith(`@${pair.imageDigest}`)(pair.webImage),
78
+ ),
79
+ );
80
+ export const Selection = Schema.Struct({ pair: Pair, webImage: Image }).check(
81
+ Schema.makeFilter(
82
+ ({ pair, webImage }) =>
83
+ (webImage === pair.imageDigest || String.endsWith(`@${pair.imageDigest}`)(webImage)) &&
84
+ (pair.webImage === undefined || pair.webImage === webImage),
85
+ ),
86
+ );
87
+ export type Selection = typeof Selection.Type;
88
+ export const ResolvedSelection = Schema.Union([
89
+ Selection,
90
+ Schema.Struct({ pair: CurrentPair, webImage: Image }).check(
91
+ Schema.makeFilter(({ pair, webImage }) => pair.webImage === webImage),
92
+ ),
93
+ ]);
94
+ export type ResolvedSelection = typeof ResolvedSelection.Type;
95
+
96
+ export * as Deployment from "./deployment.ts";
@@ -0,0 +1,334 @@
1
+ import { lstat, mkdir, mkdtemp, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
2
+ import { tmpdir } from "node:os";
3
+ import { dirname, join, resolve } from "node:path";
4
+
5
+ import { Array, Context, Effect, Layer, Record, Redacted, Schema, Stream, String } from "effect";
6
+
7
+ import { ChildProcess, ChildProcessSpawner } from "effect/process";
8
+
9
+ import { Archives } from "../release/archives.ts";
10
+ import { Releases } from "../release/releases.ts";
11
+
12
+ import { Choices } from "./choices.ts";
13
+ import { Deployment } from "./deployment.ts";
14
+ import { SourceReference } from "./source-reference.ts";
15
+ import { Template } from "./template.ts";
16
+
17
+ export const Request = Schema.Struct({
18
+ bare: Schema.Boolean,
19
+ cliVersion: Schema.NonEmptyString,
20
+ destination: Schema.NonEmptyString.check(
21
+ Schema.isPattern(/^[^\p{Cc}]+$/u),
22
+ Schema.isPattern(/\S/, { message: "Destination must not be blank" }),
23
+ ),
24
+ host: Choices.Host,
25
+ manifest: Schema.optional(Releases.Version),
26
+ release: Schema.optional(Releases.Selector),
27
+ releasePair: Schema.optional(Schema.NonEmptyString),
28
+ replay: Schema.optional(Schema.NonEmptyString),
29
+ skipInstall: Schema.Boolean,
30
+ webImage: Schema.optional(Deployment.Image),
31
+ });
32
+ export type Request = typeof Request.Type;
33
+
34
+ export class ScaffoldError extends Schema.TaggedError<ScaffoldError>()("ScaffoldError", {
35
+ cause: Schema.Redacted(Schema.Defect()),
36
+ operation: Schema.Literals([
37
+ "options",
38
+ "replay",
39
+ "destination",
40
+ "access",
41
+ "write",
42
+ "install",
43
+ "pair",
44
+ ]),
45
+ }) {}
46
+ const failure = (operation: ScaffoldError["operation"], cause: unknown) =>
47
+ new ScaffoldError({ cause: Redacted.make(cause), operation });
48
+ // Node filesystem promises cannot be cancelled. Finish each operation before
49
+ // interruption releases its staging/probe directory; no mutation may outlive it.
50
+ const attempt = <A>(operation: ScaffoldError["operation"], run: () => Promise<A>) =>
51
+ Effect.tryPromise({ catch: cause => failure(operation, cause), try: run }).pipe(
52
+ Effect.uninterruptible,
53
+ );
54
+
55
+ const FileError = Schema.Struct({ code: Schema.String });
56
+ const isMissing = Schema.is(FileError.check(Schema.makeFilter(error => error.code === "ENOENT")));
57
+
58
+ const emptyDestination = Effect.fn("Scaffold.emptyDestination")(function* (destination: string) {
59
+ const stat = yield* Effect.tryPromise(() => lstat(destination)).pipe(
60
+ Effect.catchIf(
61
+ cause => isMissing(cause.cause),
62
+ () => Effect.void,
63
+ ),
64
+ Effect.mapError(cause => failure("destination", cause.cause)),
65
+ Effect.uninterruptible,
66
+ );
67
+ if (stat === undefined) return;
68
+ if (
69
+ !stat.isDirectory() ||
70
+ Array.isArrayNonEmpty(yield* attempt("destination", () => readdir(destination)))
71
+ ) {
72
+ return yield* failure(
73
+ "destination",
74
+ new Error("Destination must be absent or an empty directory, not a symlink"),
75
+ );
76
+ }
77
+ });
78
+
79
+ export type Interface = {
80
+ readonly create: (
81
+ request: Request,
82
+ ) => Effect.Effect<
83
+ {
84
+ readonly installed: boolean;
85
+ readonly destination: string;
86
+ readonly deployment: Deployment.ResolvedSelection | null;
87
+ },
88
+ ScaffoldError
89
+ >;
90
+ };
91
+ export class Service extends Context.Service<Service, Interface>()("@dual-os/cli/Scaffold") {}
92
+
93
+ export const layer = Layer.effect(
94
+ Service,
95
+ Effect.gen(function* () {
96
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
97
+ const archives = yield* Archives.Service;
98
+ const sourceReference = yield* SourceReference.Service;
99
+ const releases = yield* Releases.Service;
100
+
101
+ const probeVersions = Effect.fn("Scaffold.probeVersions")(function* (
102
+ cwd: string,
103
+ versions: Readonly<Record<string, string>>,
104
+ ) {
105
+ for (const [name, version] of Record.toEntries(versions)) {
106
+ if (!String.startsWith("@dual/")(name) && name !== "@dual-os/sdk") continue;
107
+ yield* Effect.scoped(
108
+ Effect.gen(function* () {
109
+ const handle = yield* spawner.spawn(
110
+ ChildProcess.make(
111
+ "bun",
112
+ ["info", `${name}@${version}`, "version", "--json", "--no-cache"],
113
+ { cwd, stderr: "ignore", stdin: "ignore", stdout: "pipe" },
114
+ ),
115
+ );
116
+ const output = yield* Stream.mkString(Stream.decodeText(handle.stdout));
117
+ const code = yield* handle.exitCode;
118
+ if (code !== 0) return yield* failure("access", { exitCode: code, package: name });
119
+ yield* Schema.decodeUnknownEffect(Schema.fromJsonString(Schema.Literal(version)))(
120
+ output,
121
+ );
122
+ }),
123
+ ).pipe(
124
+ Effect.timeout("30 seconds"),
125
+ Effect.mapError(cause => failure("access", cause)),
126
+ );
127
+ }
128
+ });
129
+
130
+ const preflight = Effect.fn("Scaffold.preflight")(
131
+ (versions: Readonly<Record<string, string>>, exactReplay: boolean) =>
132
+ Effect.acquireUseRelease(
133
+ attempt("access", () => mkdtemp(join(tmpdir(), "dual-registry-probe-"))),
134
+ cwd =>
135
+ Effect.gen(function* () {
136
+ // Bun 1.4.2 info requires a package project even for metadata reads.
137
+ // Keep the probe independent of the destination and launch directory.
138
+ // Inherit environment and user-level Bun/npm config; never copy auth.
139
+ yield* attempt("access", () =>
140
+ writeFile(join(cwd, "package.json"), '{"private":true}\n', {
141
+ flag: "wx",
142
+ mode: 0o600,
143
+ }),
144
+ );
145
+ if (exactReplay) {
146
+ // Metadata availability is not artifact availability. Fetch every
147
+ // exact direct artifact, including public baseline pins and CLI,
148
+ // without installing/resolving dependencies or running scripts.
149
+ for (const [name, version] of Record.toEntries(versions)) {
150
+ yield* archives
151
+ .pack(`${name}@${version}`, cwd)
152
+ .pipe(Effect.mapError(cause => failure("access", cause)));
153
+ }
154
+ } else {
155
+ yield* probeVersions(cwd, versions);
156
+ // This new optional artifact may not have been published with the
157
+ // fixed baseline. Metadata alone must not authorize a scaffold.
158
+ const adapter = versions["@dual/workspace-opensandbox"];
159
+ if (adapter !== undefined) {
160
+ yield* archives
161
+ .pack(`@dual/workspace-opensandbox@${adapter}`, cwd)
162
+ .pipe(Effect.mapError(cause => failure("access", cause)));
163
+ }
164
+ }
165
+ }),
166
+ cwd => attempt("access", () => rm(cwd, { force: true, recursive: true })),
167
+ ),
168
+ );
169
+
170
+ const create = Effect.fn("Scaffold.create")(function* (request: Request) {
171
+ const input = yield* Schema.decodeUnknownEffect(Request)(request).pipe(
172
+ Effect.mapError(cause => failure("options", cause)),
173
+ );
174
+ if (
175
+ input.skipInstall &&
176
+ !input.bare &&
177
+ input.replay === undefined &&
178
+ input.release === undefined &&
179
+ input.manifest === undefined
180
+ ) {
181
+ return yield* failure("options", "Offline generation requires --bare --skip-install");
182
+ }
183
+ if (
184
+ (input.release !== undefined && input.manifest !== undefined) ||
185
+ ((input.release !== undefined || input.manifest !== undefined) &&
186
+ (input.releasePair !== undefined ||
187
+ input.webImage !== undefined ||
188
+ input.replay !== undefined)) ||
189
+ (input.replay !== undefined &&
190
+ (input.releasePair !== undefined || input.webImage !== undefined))
191
+ ) {
192
+ return yield* failure(
193
+ "options",
194
+ "Release selectors cannot be combined with explicit pairs or replay",
195
+ );
196
+ }
197
+ const destination = resolve(input.destination);
198
+ yield* emptyDestination(destination);
199
+ const replayPath = input.replay;
200
+ const recorded =
201
+ replayPath === undefined
202
+ ? Template.record({ cli: input.cliVersion }, input.bare, input.host)
203
+ : yield* Schema.decodeUnknownEffect(Schema.fromJsonString(Template.Replay), {
204
+ onExcessProperty: "error",
205
+ })(yield* attempt("replay", () => readFile(replayPath, "utf8"))).pipe(
206
+ Effect.mapError(cause => failure("replay", cause)),
207
+ );
208
+ if (replayPath !== undefined && recorded.cliVersion !== input.cliVersion) {
209
+ return yield* failure("replay", "Invoke the exact recorded CLI version");
210
+ }
211
+ const verified =
212
+ replayPath !== undefined && recorded.release !== null
213
+ ? yield* releases
214
+ .verify(recorded.release)
215
+ .pipe(Effect.mapError(cause => failure("replay", cause)))
216
+ : recorded.release;
217
+ if ((input.releasePair === undefined) !== (input.webImage === undefined)) {
218
+ return yield* failure("pair", "Both pair and immutable web image are required");
219
+ }
220
+ const pairPath = input.releasePair;
221
+ const pairSelection =
222
+ pairPath !== undefined && input.webImage !== undefined
223
+ ? yield* Schema.decodeUnknownEffect(Deployment.Selection)({
224
+ pair: yield* Schema.decodeUnknownEffect(Schema.fromJsonString(Deployment.Pair))(
225
+ yield* attempt("pair", () => readFile(pairPath, "utf8")),
226
+ ).pipe(Effect.mapError(cause => failure("pair", cause))),
227
+ webImage: input.webImage,
228
+ }).pipe(Effect.mapError(cause => failure("pair", cause)))
229
+ : recorded.deployment;
230
+ const resolveRelease =
231
+ input.replay === undefined &&
232
+ input.releasePair === undefined &&
233
+ (input.release !== undefined ||
234
+ input.manifest !== undefined ||
235
+ !(input.bare && input.skipInstall));
236
+ const release = resolveRelease
237
+ ? yield* releases
238
+ .resolveForCreate(
239
+ input.manifest === undefined
240
+ ? { core: input.release ?? "latest" }
241
+ : { manifest: input.manifest },
242
+ { cli: input.cliVersion, template: Template.revision },
243
+ )
244
+ .pipe(Effect.mapError(cause => failure("pair", cause)))
245
+ : verified;
246
+ const deployment =
247
+ resolveRelease && release !== null
248
+ ? { pair: release.manifest.pair, webImage: release.manifest.pair.webImage }
249
+ : pairSelection;
250
+ if (input.replay !== undefined && deployment !== null) {
251
+ const image = deployment.webImage;
252
+ const args = String.startsWith("sha256:")(image)
253
+ ? ["image", "inspect", image]
254
+ : ["manifest", "inspect", image];
255
+ const code = yield* spawner
256
+ .exitCode(
257
+ ChildProcess.make("docker", args, {
258
+ stderr: "ignore",
259
+ stdin: "ignore",
260
+ stdout: "ignore",
261
+ }),
262
+ )
263
+ .pipe(
264
+ Effect.timeout("30 seconds"),
265
+ Effect.mapError(cause => failure("pair", cause)),
266
+ );
267
+ if (code !== 0) return yield* failure("pair", "Exact web artifact unavailable");
268
+ }
269
+ const replay = yield* Schema.decodeUnknownEffect(Template.Replay, {
270
+ onExcessProperty: "error",
271
+ })(
272
+ input.replay === undefined
273
+ ? Template.record({ cli: input.cliVersion }, input.bare, input.host, deployment, release)
274
+ : recorded,
275
+ ).pipe(
276
+ Effect.mapError(cause => failure(input.replay === undefined ? "options" : "replay", cause)),
277
+ );
278
+ if (!input.skipInstall || input.replay !== undefined) {
279
+ const versions = { ...replay.packages };
280
+ // The manifest records the complete release, but this host only needs
281
+ // the optional adapter when it actually selects OpenSandbox.
282
+ if (replay.choices.workspace !== "opensandbox") {
283
+ delete versions["@dual/workspace-opensandbox"];
284
+ }
285
+ if (input.replay !== undefined) versions["@dual-os/cli"] = replay.cliVersion;
286
+ yield* preflight(versions, input.replay !== undefined);
287
+ }
288
+ // Recheck after registry I/O. Generate completely in a private sibling before
289
+ // renaming: no partially written scaffold and no overwrite of customer files.
290
+ yield* emptyDestination(destination);
291
+ const rendered = yield* attempt("write", () => Template.files(replay));
292
+ const content = { ...rendered, ...SourceReference.files };
293
+ yield* attempt("write", () => mkdir(dirname(destination), { recursive: true }));
294
+ yield* Effect.acquireUseRelease(
295
+ attempt("write", () => mkdtemp(join(dirname(destination), ".dual-create-"))),
296
+ staging =>
297
+ Effect.gen(function* () {
298
+ for (const [path, body] of Record.toEntries(content)) {
299
+ yield* attempt("write", async () => {
300
+ const target = join(staging, path);
301
+ await mkdir(dirname(target), { recursive: true });
302
+ await writeFile(target, body, { flag: "wx", mode: 0o644 });
303
+ });
304
+ }
305
+ yield* sourceReference
306
+ .register(staging)
307
+ .pipe(Effect.mapError(cause => failure("write", cause)));
308
+ yield* emptyDestination(destination);
309
+ yield* attempt("write", () => rename(staging, destination));
310
+ }),
311
+ staging => attempt("write", () => rm(staging, { force: true, recursive: true })),
312
+ );
313
+ if (!input.skipInstall) {
314
+ // No shell and no echoed child output: package managers may print tokens,
315
+ // registry URLs, lifecycle output or environment-derived credentials.
316
+ const code = yield* spawner
317
+ .exitCode(
318
+ ChildProcess.make("bun", ["install"], {
319
+ cwd: destination,
320
+ stderr: "ignore",
321
+ stdin: "ignore",
322
+ stdout: "ignore",
323
+ }),
324
+ )
325
+ .pipe(Effect.mapError(cause => failure("install", cause)));
326
+ if (code !== 0) return yield* failure("install", { exitCode: code });
327
+ }
328
+ return { deployment: replay.deployment, destination, installed: !input.skipInstall };
329
+ });
330
+ return Service.of({ create });
331
+ }),
332
+ ).pipe(Layer.provide(SourceReference.layer));
333
+
334
+ export * as Scaffold from "./scaffold.ts";