@dual-os/cli 0.0.0-stage → 0.11.0-next.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,572 @@
1
+ import { createRequire, isBuiltin } from "node:module";
2
+
3
+ import type { PlatformError } from "effect";
4
+ import {
5
+ Array,
6
+ Effect,
7
+ FileSystem,
8
+ MutableHashMap,
9
+ MutableList,
10
+ Order,
11
+ Path,
12
+ Predicate,
13
+ Record,
14
+ Schema,
15
+ } from "effect";
16
+
17
+ import * as acorn from "acorn";
18
+ import { ChildProcess, ChildProcessSpawner } from "effect/process";
19
+
20
+ import { ReleaseManifest } from "#release/release-manifest";
21
+
22
+ import { PluginPackage } from "./package.ts";
23
+
24
+ const Import = Schema.Struct({ path: Schema.String, external: Schema.optionalKey(Schema.Boolean) });
25
+ const Metafile = Schema.Struct({
26
+ inputs: Schema.Record(Schema.String, Schema.Struct({ imports: Schema.Array(Import) })),
27
+ outputs: Schema.Record(
28
+ Schema.String,
29
+ Schema.Struct({
30
+ imports: Schema.Array(Import),
31
+ inputs: Schema.Record(Schema.String, Schema.Struct({ bytesInOutput: Schema.Number })),
32
+ entryPoint: Schema.optionalKey(Schema.String),
33
+ }),
34
+ ),
35
+ });
36
+ const Metadata = Schema.StructWithRest(
37
+ Schema.Struct({
38
+ name: Schema.optionalKey(Schema.String),
39
+ version: ReleaseManifest.ExactVersion,
40
+ license: Schema.optionalKey(Schema.String),
41
+ exports: Schema.optionalKey(Schema.Json),
42
+ }),
43
+ [Schema.Record(Schema.String, Schema.Json)],
44
+ );
45
+
46
+ function targets(value: Schema.Json): string[] {
47
+ if (typeof value === "string") return [value];
48
+ if (value === null) return [];
49
+ if (typeof value !== "object")
50
+ throw PluginPackage.PackageError.make({ message: "Unsupported package exports" });
51
+ return Array.flatMap(Object.values(value), targets);
52
+ }
53
+
54
+ function pattern(expression: acorn.Expression | acorn.SpreadElement): string {
55
+ if (expression.type === "Literal" && typeof expression.value === "string")
56
+ return expression.value;
57
+ if (
58
+ expression.type === "BinaryExpression" &&
59
+ expression.operator === "+" &&
60
+ expression.left.type !== "PrivateIdentifier"
61
+ )
62
+ return pattern(expression.left) + pattern(expression.right);
63
+ if (expression.type === "TemplateLiteral")
64
+ return Array.join(
65
+ Array.map(expression.quasis, part => part.value.cooked ?? part.value.raw),
66
+ "*",
67
+ );
68
+ return "*";
69
+ }
70
+
71
+ function isNode(value: unknown): value is acorn.AnyNode {
72
+ return (
73
+ Predicate.isObject(value) &&
74
+ Predicate.isString(value["type"]) &&
75
+ Predicate.isNumber(value["start"]) &&
76
+ Predicate.isNumber(value["end"])
77
+ );
78
+ }
79
+
80
+ // Acorn owns the node shapes; traversing the AST also handles chained import().then() calls.
81
+ function references(code: string) {
82
+ const tree = acorn.parse(code, {
83
+ ecmaVersion: "latest",
84
+ sourceType: "module",
85
+ allowReturnOutsideFunction: true,
86
+ });
87
+ const found = MutableList.make<{
88
+ kind: "import" | "require" | "resolve" | "asset";
89
+ path: string;
90
+ }>();
91
+ const visit = (
92
+ expression: acorn.AnyNode,
93
+ parent?: acorn.AnyNode,
94
+ grandparent?: acorn.AnyNode,
95
+ ): void => {
96
+ if (expression.type === "ImportExpression")
97
+ MutableList.append(found, { kind: "import" as const, path: pattern(expression.source) });
98
+ if (
99
+ expression.type === "CallExpression" &&
100
+ expression.callee.type === "Identifier" &&
101
+ Array.contains(["require", "__require"], expression.callee.name) &&
102
+ expression.arguments[0]
103
+ )
104
+ MutableList.append(found, {
105
+ kind: "require" as const,
106
+ path: pattern(expression.arguments[0]),
107
+ });
108
+ if (
109
+ expression.type === "CallExpression" &&
110
+ expression.callee.type === "MemberExpression" &&
111
+ expression.callee.object.type === "MetaProperty" &&
112
+ expression.callee.object.meta.name === "import" &&
113
+ expression.callee.property.type === "Identifier" &&
114
+ Array.contains(["resolve", "require"], expression.callee.property.name) &&
115
+ expression.arguments[0]
116
+ )
117
+ MutableList.append(found, {
118
+ kind: "resolve" as const,
119
+ path: pattern(expression.arguments[0]),
120
+ });
121
+ if (
122
+ expression.type === "ImportSpecifier" &&
123
+ expression.imported.type === "Identifier" &&
124
+ expression.imported.name === "createRequire" &&
125
+ parent?.type === "ImportDeclaration" &&
126
+ Array.contains(["node:module", "module"], parent.source.value)
127
+ )
128
+ MutableList.append(found, { kind: "resolve" as const, path: "createRequire" });
129
+ if (
130
+ ((expression.type === "ImportNamespaceSpecifier" ||
131
+ expression.type === "ImportDefaultSpecifier") &&
132
+ parent?.type === "ImportDeclaration" &&
133
+ Array.contains(["node:module", "module"], parent.source.value)) ||
134
+ (expression.type === "ImportExpression" &&
135
+ Array.contains(["node:module", "module"], pattern(expression.source))) ||
136
+ (expression.type === "CallExpression" &&
137
+ expression.callee.type === "Identifier" &&
138
+ Array.contains(["require", "__require"], expression.callee.name) &&
139
+ expression.arguments[0] &&
140
+ Array.contains(["node:module", "module"], pattern(expression.arguments[0])))
141
+ )
142
+ MutableList.append(found, { kind: "resolve" as const, path: "module namespace" });
143
+ if (expression.type === "NewExpression") {
144
+ const base = expression.arguments[1];
145
+ if (
146
+ expression.callee.type === "Identifier" &&
147
+ expression.callee.name === "URL" &&
148
+ base?.type === "MemberExpression" &&
149
+ base.object.type === "MetaProperty" &&
150
+ base.object.meta.name === "import" &&
151
+ base.object.property.name === "meta" &&
152
+ base.property.type === "Identifier" &&
153
+ base.property.name === "url" &&
154
+ expression.arguments[0]
155
+ ) {
156
+ const receiver = parent?.type === "MemberExpression" ? parent : expression;
157
+ const call = receiver === parent ? grandparent : parent;
158
+ const method = call?.type === "CallExpression" ? call.callee : undefined;
159
+ const name =
160
+ method?.type === "Identifier"
161
+ ? method.name
162
+ : method?.type === "MemberExpression" && method.property.type === "Identifier"
163
+ ? method.property.name
164
+ : "";
165
+ const writing =
166
+ call?.type === "CallExpression" &&
167
+ call.arguments[0] === receiver &&
168
+ Array.contains(
169
+ [
170
+ "writeFile",
171
+ "writeFileSync",
172
+ "writeFileString",
173
+ "appendFile",
174
+ "appendFileSync",
175
+ "makeDirectory",
176
+ "mkdir",
177
+ "mkdirSync",
178
+ ],
179
+ name,
180
+ );
181
+ if (!writing)
182
+ MutableList.append(found, {
183
+ kind: "asset" as const,
184
+ path: pattern(expression.arguments[0]),
185
+ });
186
+ }
187
+ }
188
+ const fields: unknown[] = Object.values(expression);
189
+ for (const field of fields) {
190
+ if (isNode(field)) visit(field, expression, parent);
191
+ else if (Array.isArray(field))
192
+ for (const child of field) if (isNode(child)) visit(child, expression, parent);
193
+ }
194
+ };
195
+ visit(tree);
196
+ return MutableList.toArray(found);
197
+ }
198
+
199
+ export const make = Effect.gen(function* () {
200
+ const fs = yield* FileSystem.FileSystem;
201
+ const path = yield* Path.Path;
202
+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
203
+ return Effect.fn("PluginBundle.build")(function* (
204
+ root: string,
205
+ prepared: PluginPackage.Prepared,
206
+ ) {
207
+ const json = Effect.fnUntraced(function* (file: string) {
208
+ return yield* Schema.decodeEffect(Schema.fromJsonString(Metadata))(
209
+ yield* fs.readFileString(file),
210
+ );
211
+ });
212
+ const manifest = yield* json(path.join(root, "package.json"));
213
+ const require = createRequire(prepared.entrypoint);
214
+ const sdkRoot = path.dirname(require.resolve("@dual-os/sdk/package.json"));
215
+ const sdk = yield* json(path.join(sdkRoot, "package.json"));
216
+ const sdkRunner = yield* fs.realPath(require.resolve("@dual-os/sdk/Runner"));
217
+ const input = path.join(root, ".dual/bundle-input");
218
+ const output = path.join(root, ".dual/bundle-output");
219
+ for (const directory of [input, output]) {
220
+ if (yield* fs.exists(directory)) yield* fs.remove(directory, { recursive: true });
221
+ yield* fs.makeDirectory(directory, { recursive: true });
222
+ }
223
+ const safe = Effect.fnUntraced(function* (name: string) {
224
+ const relative = path.relative(root, path.resolve(root, name));
225
+ if (
226
+ relative === "" ||
227
+ relative.startsWith("..") ||
228
+ path.isAbsolute(relative) ||
229
+ Array.some(
230
+ relative.split(path.sep),
231
+ part =>
232
+ ["node_modules", ".dual", ".env", ".npmrc", ".netrc"].includes(part) ||
233
+ part.startsWith(".env."),
234
+ )
235
+ )
236
+ return yield* PluginPackage.PackageError.make({ message: `Unsafe bundle path: ${name}` });
237
+ const source = path.resolve(root, name);
238
+ const real = yield* fs.realPath(source);
239
+ if (!real.startsWith(`${root}${path.sep}`))
240
+ return yield* PluginPackage.PackageError.make({
241
+ message: `Bundle file escapes the package: ${name}`,
242
+ });
243
+ return { source, relative };
244
+ });
245
+ const copy = Effect.fnUntraced(function* (name: string, destination: string) {
246
+ const file = yield* safe(name);
247
+ if ((yield* fs.stat(file.source)).type !== "File")
248
+ return yield* PluginPackage.PackageError.make({
249
+ message: `Bundle export must be a file: ${name}`,
250
+ });
251
+ const bytes = yield* fs.readFile(file.source);
252
+ if (/\.[cm]?tsx?$/u.test(name) && !/\.d\.[cm]?ts$/u.test(name))
253
+ return yield* PluginPackage.PackageError.make({
254
+ message: `Source exports/assets cannot be shipped in a JavaScript bundle: ${name}`,
255
+ });
256
+ const prefix = Buffer.from(bytes.subarray(0, 4));
257
+ if (
258
+ /\.(?:node|dll|dylib|exe|so(?:\.[0-9.]+)?)$|(?:^|\/)binding\.gyp$/iu.test(name) ||
259
+ (prefix.length === 4 &&
260
+ [
261
+ 0x7f454c46, 0xfeedface, 0xcefaedfe, 0xfeedfacf, 0xcffaedfe, 0xcafebabe, 0xbebafeca,
262
+ ].includes(prefix.readUInt32BE(0))) ||
263
+ (prefix.length >= 2 && prefix.readUInt16BE(0) === 0x4d5a)
264
+ )
265
+ return yield* PluginPackage.PackageError.make({
266
+ message: `Native bundle content is unsupported: ${name}`,
267
+ });
268
+ yield* fs.makeDirectory(path.dirname(path.join(destination, file.relative)), {
269
+ recursive: true,
270
+ });
271
+ yield* fs.copyFile(file.source, path.join(destination, file.relative));
272
+ });
273
+ const publicFiles = MutableList.make<string>();
274
+ for (const target of yield* Effect.try(() => targets(manifest.exports ?? {}))) {
275
+ const matches = yield* fs.glob(target, { root });
276
+ if (Array.isArrayEmpty(matches))
277
+ return yield* PluginPackage.PackageError.make({
278
+ message: `Missing public export: ${target}`,
279
+ });
280
+ MutableList.appendAll(publicFiles, matches);
281
+ }
282
+ const declared = Array.dedupe([
283
+ ...MutableList.toArray(publicFiles),
284
+ path.relative(root, prepared.entrypoint),
285
+ ...Array.map(prepared.assets, file => path.relative(root, file)),
286
+ ]);
287
+ // Static helpers can live above any public entrypoint; only the bundler's reachable graph ships.
288
+ const declarations = MutableList.make<string>();
289
+ for (const file of yield* fs.glob("**/*", {
290
+ root,
291
+ exclude: ["node_modules/**", ".dual/**", ".git/**"],
292
+ })) {
293
+ const source = path.join(root, file);
294
+ if ((yield* fs.stat(source)).type !== "File") continue;
295
+ if (/\.d\.[cm]?ts(?:\.map)?$/u.test(file)) {
296
+ yield* copy(file, output);
297
+ MutableList.append(declarations, file);
298
+ }
299
+ if (
300
+ /\.(?:[cm]?js|json|map)$/.test(file) &&
301
+ !Array.some(file.split("/"), part => part.startsWith("."))
302
+ )
303
+ yield* copy(path.relative(root, source), input);
304
+ }
305
+ for (const file of declared) {
306
+ yield* copy(file, input);
307
+ if (!/\.(?:[cm]?js)$/.test(file)) yield* copy(file, output);
308
+ }
309
+ for (const file of ["README.md", "LICENSE", "LICENSE.md", "LICENSE.txt"]) {
310
+ if (yield* fs.exists(path.join(root, file))) yield* copy(file, output);
311
+ }
312
+ yield* fs.writeFileString(
313
+ path.join(input, "package.json"),
314
+ yield* fs.readFileString(path.join(root, "package.json")),
315
+ );
316
+ // Resolve the same installed graph as the source package, including workspace hoisting.
317
+ const modules = Effect.fnUntraced(function* (
318
+ directory: string,
319
+ ): Effect.fn.Return<string, PlatformError.PlatformError | PluginPackage.PackageError> {
320
+ if (yield* fs.exists(path.join(directory, "node_modules")))
321
+ return path.join(directory, "node_modules");
322
+ const parent = path.dirname(directory);
323
+ if (parent === directory)
324
+ return yield* PluginPackage.PackageError.make({
325
+ message: "Install build dependencies from a frozen lockfile first",
326
+ });
327
+ return yield* modules(parent);
328
+ });
329
+ yield* fs.symlink(yield* modules(root), path.join(input, "node_modules"));
330
+ const runnerName = Effect.fnUntraced(function* (
331
+ index: number,
332
+ ): Effect.fn.Return<string, PlatformError.PlatformError> {
333
+ const name = index === 0 ? "Runner.js" : `Runner-${index}.js`;
334
+ return (yield* fs.exists(path.join(input, name))) ? yield* runnerName(index + 1) : name;
335
+ });
336
+ const runner = yield* runnerName(0);
337
+ yield* fs.writeFileString(path.join(input, runner), 'import "@dual-os/sdk/Runner";\n');
338
+ const entries = Array.dedupe([
339
+ ...Array.filter(declared, file => /\.(?:[cm]?js)$/.test(file)),
340
+ runner,
341
+ ]);
342
+ const metaPath = path.join(root, ".dual/bundle-metafile.json");
343
+ const code = yield* spawner.exitCode(
344
+ ChildProcess.make(
345
+ "bun",
346
+ [
347
+ "build",
348
+ "--no-env-file",
349
+ "--target=bun",
350
+ "--format=esm",
351
+ "--splitting",
352
+ "--keep-names",
353
+ "--sourcemap=linked",
354
+ `--metafile=${metaPath}`,
355
+ "--root",
356
+ input,
357
+ "--outdir",
358
+ output,
359
+ ...Array.map(entries, file => path.resolve(input, file)),
360
+ ],
361
+ { cwd: input, stdin: "ignore", stdout: "ignore", stderr: "inherit" },
362
+ ),
363
+ );
364
+ if (code !== 0)
365
+ return yield* PluginPackage.PackageError.make({
366
+ message: "Self-contained plugin compilation failed; no installation fallback is available",
367
+ });
368
+ const meta = yield* Schema.decodeEffect(Schema.fromJsonString(Metafile))(
369
+ yield* fs.readFileString(metaPath),
370
+ );
371
+ const emitted = Record.fromEntries(
372
+ Array.flatMap(Object.entries(meta.outputs), entry =>
373
+ entry[1].entryPoint
374
+ ? [
375
+ [
376
+ path.relative(input, path.resolve(input, entry[1].entryPoint)),
377
+ entry[0].replace(/^\.\//u, ""),
378
+ ] as const,
379
+ ]
380
+ : [],
381
+ ),
382
+ );
383
+ const rewrite = (value: Schema.Json): Schema.Json => {
384
+ if (typeof value === "string")
385
+ return emitted[path.normalize(value)]
386
+ ? `./${emitted[path.normalize(value)]}`
387
+ : value.replace(/\.[cm]js$/u, ".js");
388
+ if (value === null || typeof value !== "object") return value;
389
+ if (Array.isArray<Schema.Json>(value)) return Array.map(value, rewrite);
390
+ return Record.map(value, rewrite);
391
+ };
392
+ const packages = MutableHashMap.empty<
393
+ string,
394
+ { name: string; version: string; license: string; licenses: string[] }
395
+ >();
396
+ const contributing = Array.dedupe(
397
+ Array.flatMap(Object.values(meta.outputs), result =>
398
+ Array.map(
399
+ Array.filter(Object.entries(result.inputs), entry => entry[1].bytesInOutput > 0),
400
+ entry => entry[0],
401
+ ),
402
+ ),
403
+ );
404
+ for (const file of contributing) {
405
+ const source = yield* fs.realPath(path.resolve(input, file));
406
+ const body = yield* fs.readFileString(source);
407
+ for (const reference of /\.[cm]?js$/.test(source)
408
+ ? yield* Effect.try(() => references(body))
409
+ : []) {
410
+ if (
411
+ reference.kind === "resolve" &&
412
+ Array.contains(["createRequire", "module namespace"], reference.path)
413
+ )
414
+ continue;
415
+ if (reference.kind === "import" && source === sdkRunner) continue;
416
+ if (isBuiltin(reference.path) || reference.path.startsWith("bun:")) continue;
417
+ if (reference.kind === "asset" && /^(?!file:)[a-z][a-z\d+.-]*:/iu.test(reference.path))
418
+ continue;
419
+ if (reference.kind === "resolve")
420
+ return yield* PluginPackage.PackageError.make({
421
+ message: `Runtime package resolution cannot be bundled in ${file}: ${reference.path}; use an import instead`,
422
+ });
423
+ if (reference.kind !== "asset" && !reference.path.includes("*")) continue;
424
+ if (reference.kind === "require")
425
+ return yield* PluginPackage.PackageError.make({
426
+ message: `Unresolved computed require in ${file}; use finite imports and declare dual.plugin.assets`,
427
+ });
428
+ if (!reference.path.startsWith("."))
429
+ return yield* PluginPackage.PackageError.make({
430
+ message: `Unresolved dynamic import/resource in ${file}: ${reference.path}. Declare finite relative files in dual.plugin.assets.`,
431
+ });
432
+ const matches = yield* fs.glob(reference.path, { root: path.dirname(source) });
433
+ if (Array.isArrayEmpty(matches))
434
+ return yield* PluginPackage.PackageError.make({
435
+ message: `Missing dynamic import/resource in ${file}: ${reference.path}`,
436
+ });
437
+ if (reference.path.includes("*"))
438
+ for (const match of matches) {
439
+ const relative = path.relative(input, path.resolve(path.dirname(source), match));
440
+ if (!Array.some(declared, name => path.normalize(name) === relative))
441
+ return yield* PluginPackage.PackageError.make({
442
+ message: `Declare dynamic file in dual.plugin.assets: ${relative}`,
443
+ });
444
+ }
445
+ }
446
+ if (source.startsWith(`${input}${path.sep}`)) continue;
447
+ const owner = Effect.fnUntraced(function* (
448
+ directory: string,
449
+ ): Effect.fn.Return<string, PlatformError.PlatformError | PluginPackage.PackageError> {
450
+ if (yield* fs.exists(path.join(directory, "package.json"))) return directory;
451
+ const parent = path.dirname(directory);
452
+ if (parent === directory)
453
+ return yield* PluginPackage.PackageError.make({
454
+ message: `Dependency has no package manifest: ${source}`,
455
+ });
456
+ return yield* owner(parent);
457
+ });
458
+ const directory = yield* owner(path.dirname(source));
459
+ if (MutableHashMap.has(packages, directory)) continue;
460
+ const pkg = yield* json(path.join(directory, "package.json"));
461
+ if (!pkg.name || !pkg.license)
462
+ return yield* PluginPackage.PackageError.make({
463
+ message: `Dependency lacks name/license metadata: ${directory}`,
464
+ });
465
+ const licenseFiles = (yield* fs.glob("{LICENSE,LICENCE,COPYING,NOTICE}{,.md,.txt}", {
466
+ root: directory,
467
+ })).sort();
468
+ if (Array.isArrayEmpty(licenseFiles))
469
+ return yield* PluginPackage.PackageError.make({
470
+ message: `Dependency attribution missing: ${pkg.name}@${pkg.version}`,
471
+ });
472
+ const licenses = MutableList.make<string>();
473
+ for (const license of licenseFiles) {
474
+ if ((yield* fs.stat(path.join(directory, license))).size === 0n)
475
+ return yield* PluginPackage.PackageError.make({
476
+ message: `Empty dependency attribution: ${pkg.name}@${pkg.version}: ${license}`,
477
+ });
478
+ const target = `licenses/${pkg.name.replaceAll("/", "_")}@${pkg.version}/${license}`;
479
+ yield* fs.makeDirectory(path.dirname(path.join(output, target)), { recursive: true });
480
+ yield* fs.copyFile(path.join(directory, license), path.join(output, target));
481
+ MutableList.append(licenses, target);
482
+ }
483
+ MutableHashMap.set(packages, directory, {
484
+ name: pkg.name,
485
+ version: pkg.version,
486
+ license: pkg.license,
487
+ licenses: MutableList.toArray(licenses),
488
+ });
489
+ }
490
+ for (const [file, metadata] of Object.entries(meta.outputs)) {
491
+ for (const imported of metadata.imports) {
492
+ if (isBuiltin(imported.path) || imported.path.startsWith("bun:")) continue;
493
+ if (
494
+ imported.external ||
495
+ !imported.path.startsWith(".") ||
496
+ !(yield* fs.exists(path.resolve(output, imported.path)))
497
+ )
498
+ return yield* PluginPackage.PackageError.make({
499
+ message: `Unbundled dependency in ${file}: ${imported.path}`,
500
+ });
501
+ }
502
+ if (!/\.[cm]?js$/.test(file)) continue;
503
+ const outputCode = yield* fs.readFileString(path.resolve(output, file));
504
+ for (const reference of yield* Effect.try(() => references(outputCode))) {
505
+ if (
506
+ reference.kind === "resolve" &&
507
+ Array.contains(["createRequire", "module namespace"], reference.path)
508
+ )
509
+ return yield* PluginPackage.PackageError.make({
510
+ message: `Runtime package resolution cannot be bundled in ${file}: ${reference.path}; use static named imports instead`,
511
+ });
512
+ if (
513
+ reference.kind === "require" &&
514
+ !isBuiltin(reference.path) &&
515
+ !reference.path.startsWith("bun:")
516
+ )
517
+ return yield* PluginPackage.PackageError.make({
518
+ message: `Unbundled require in ${file}: ${reference.path}; no installation fallback is available`,
519
+ });
520
+ if (
521
+ reference.kind === "import" &&
522
+ reference.path.startsWith(".") &&
523
+ reference.path.includes("*") &&
524
+ Array.isArrayEmpty(
525
+ yield* fs.glob(reference.path, { root: path.resolve(output, path.dirname(file)) }),
526
+ )
527
+ )
528
+ return yield* PluginPackage.PackageError.make({
529
+ message: `Bundle dynamic import is missing after splitting: ${file}: ${reference.path}`,
530
+ });
531
+ if (reference.kind !== "asset") continue;
532
+ if (/^(?!file:)[a-z][a-z\d+.-]*:/iu.test(reference.path)) continue;
533
+ if (!(yield* fs.exists(path.resolve(output, path.dirname(file), reference.path))))
534
+ return yield* PluginPackage.PackageError.make({
535
+ message: `Bundle resource is missing after splitting: ${file}: ${reference.path}`,
536
+ });
537
+ }
538
+ }
539
+ return {
540
+ output,
541
+ sdkVersion: sdk.version,
542
+ runner: emitted[runner] ?? runner,
543
+ manifest: { ...manifest, exports: rewrite(manifest.exports ?? {}) },
544
+ entrypoint:
545
+ emitted[path.relative(root, prepared.entrypoint)] ??
546
+ path.relative(root, prepared.entrypoint),
547
+ assets: Array.dedupe([
548
+ ...Array.map(prepared.assets, file => {
549
+ const relative = path.relative(root, file);
550
+ return emitted[relative] ?? relative;
551
+ }),
552
+ ...MutableList.toArray(declarations),
553
+ ...Array.filter(MutableList.toArray(publicFiles), file => !/\.[cm]?js$/u.test(file)),
554
+ ...Array.map(
555
+ Array.filter(Object.keys(meta.outputs), file => !/\.[cm]?js$/u.test(file)),
556
+ file => file.replace(/^\.\//u, ""),
557
+ ),
558
+ ]),
559
+ publicJavaScript: Array.map(
560
+ Array.filter(MutableList.toArray(publicFiles), file => /\.[cm]?js$/u.test(file)),
561
+ source => ({ source, output: emitted[path.normalize(source)] ?? source }),
562
+ ),
563
+ packages: Array.sort(
564
+ Array.fromIterable(MutableHashMap.values(packages)),
565
+ Order.mapInput(
566
+ Order.String,
567
+ (pkg: { name: string; version: string }) => `${pkg.name}@${pkg.version}`,
568
+ ),
569
+ ),
570
+ };
571
+ });
572
+ });