create-octane-xplat 0.18.0 → 0.20.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 (77) hide show
  1. package/README.md +2 -144
  2. package/compatibility.json +97 -19
  3. package/compatibility.ts +141 -72
  4. package/dist/compatibility.js +66 -26
  5. package/dist/index.js +127 -64
  6. package/dist/package-manager.js +62 -0
  7. package/dist/scaffold.js +377 -127
  8. package/index.ts +256 -154
  9. package/library-template/AGENTS.md +6 -4
  10. package/library-template/README.md +22 -18
  11. package/library-template/bunfig +4 -0
  12. package/library-template/package.json +115 -18
  13. package/library-template/patches/@nativescript-community__octane@0.2.5.patch +1018 -0
  14. package/library-template/patches/@nativescript-community__sqlite-bridge@4.1.1.patch +30 -0
  15. package/library-template/patches/@nativescript-community__vite-octane@0.2.1.patch +72 -0
  16. package/library-template/patches/@nativescript__core@9.1.3.patch +1280 -0
  17. package/library-template/patches/@nativescript__doctor@2.0.18.patch +13 -0
  18. package/library-template/patches/@nativescript__vite@8.0.20.patch +2019 -0
  19. package/library-template/patches/@octane-xplat__nativescript-mason-core@1.0.0-beta.7.patch +65 -0
  20. package/library-template/patches/@octanejs__tiptap@0.0.62.patch +12 -0
  21. package/library-template/patches/@sveltejs__acorn-typescript@1.0.13.patch +16 -0
  22. package/library-template/patches/css@3.0.0.patch +52 -0
  23. package/library-template/patches/octane@0.16.3.patch +1479 -0
  24. package/library-template/src/Badge.tsx +7 -6
  25. package/library-template/src/Badge.web.tsx +7 -6
  26. package/library-template/src/Welcome.stories.tsx +12 -0
  27. package/library-template/src/normalizeBadgeLabel.ts +1 -1
  28. package/library-template/src/props.ts +11 -11
  29. package/library-template/tests/normalizeBadgeLabel.test.ts +1 -1
  30. package/library-template/tests/packed-consumer.mjs +140 -123
  31. package/library-template/tsconfig.envs.json +1 -0
  32. package/library-template/tsconfig.json +1 -0
  33. package/library-template/tsconfig.types.json +15 -1
  34. package/library-template/tsrx-typegen.json +1 -1
  35. package/library-template/workshop.config.ts +3 -0
  36. package/package-manager.ts +93 -0
  37. package/package.json +70 -58
  38. package/scaffold.ts +1056 -526
  39. package/template/.agents/skills/xplat/SKILL.md +6 -6
  40. package/template/.agents/skills/xplat/references/components.md +1 -1
  41. package/template/.agents/skills/xplat/references/navigation.md +1 -1
  42. package/template/.agents/skills/xplat/references/styling.md +14 -14
  43. package/template/AGENTS.md +10 -10
  44. package/template/bunfig +4 -0
  45. package/template/gitignore +1 -0
  46. package/template/nativescript.config.ts +13 -16
  47. package/template/package.json +119 -10
  48. package/template/patches/@nativescript-community__octane@0.2.5.patch +1018 -0
  49. package/template/patches/@nativescript-community__sqlite-bridge@4.1.1.patch +30 -0
  50. package/template/patches/@nativescript-community__vite-octane@0.2.1.patch +72 -0
  51. package/template/patches/@nativescript__core@9.1.3.patch +1280 -0
  52. package/template/patches/@nativescript__doctor@2.0.18.patch +13 -0
  53. package/template/patches/@nativescript__vite@8.0.20.patch +2019 -0
  54. package/template/patches/@octane-xplat__nativescript-mason-core@1.0.0-beta.7.patch +65 -0
  55. package/template/patches/@octanejs__tiptap@0.0.62.patch +12 -0
  56. package/template/patches/@sveltejs__acorn-typescript@1.0.13.patch +16 -0
  57. package/template/patches/css@3.0.0.patch +52 -0
  58. package/template/patches/octane@0.16.3.patch +1479 -0
  59. package/template/src/App.tsrx +77 -42
  60. package/template/src/Welcome.stories.tsx +22 -0
  61. package/template/src/app.spec.tsrx +169 -163
  62. package/template/src/app.web.test.tsx +18 -13
  63. package/template/src/failure-reporting.spec.ts +3 -3
  64. package/template/src/main.ts +14 -13
  65. package/template/src/main.tsrx +2 -0
  66. package/template/src/main.web.tsrx +5 -5
  67. package/template/src/native-runtime.spec.ts +13 -11
  68. package/template/src/test-setup.web.ts +9 -8
  69. package/template/src/test.ts +6 -6
  70. package/template/tsconfig.json +17 -1
  71. package/template/vite.config.native.mts +1 -2
  72. package/template/vite.config.ts +29 -23
  73. package/template/vitest.config.mts +18 -18
  74. package/template/vitest.web.config.mts +7 -7
  75. package/template/workshop.config.ts +3 -0
  76. package/library-template/pnpm-workspace.yaml +0 -171
  77. package/template/pnpm-workspace.yaml +0 -192
package/dist/scaffold.js CHANGED
@@ -1,9 +1,10 @@
1
- import { ensureCompatibilityCatalog } from "./compatibility.js";
2
- import { cpSync, existsSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
3
- import { basename, dirname, join, resolve } from "node:path";
1
+ import { compatibility, compatibilityRoot, mergeCompatibilityCatalog, requestedCatalogPins } from "./compatibility.js";
2
+ import { cpSync, existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
3
+ import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
4
4
  import { fileURLToPath } from "node:url";
5
- import { parseDocument } from "yaml";
6
- //#region scaffold.ts
5
+ import { createHash } from "node:crypto";
6
+ import { isMap, parseDocument, stringify } from "yaml";
7
+ // scaffold.ts
7
8
  var moduleDir = dirname(fileURLToPath(import.meta.url));
8
9
  var pkgRoot = basename(moduleDir) === "dist" ? join(moduleDir, "..") : moduleDir;
9
10
  var TEMPLATE_DIR = join(pkgRoot, "template");
@@ -27,9 +28,14 @@ var targets = {
27
28
  "dev",
28
29
  "build",
29
30
  "test",
30
- "test:web"
31
+ "test:web",
32
+ "workshop"
31
33
  ],
32
- devDeps: ["@octanejs/vite-plugin", "jsdom"]
34
+ devDeps: [
35
+ "@octanejs/vite-plugin",
36
+ "jsdom",
37
+ "@octane-xplat/workshop"
38
+ ]
33
39
  },
34
40
  ios: {
35
41
  label: "iOS (NativeScript)",
@@ -41,7 +47,7 @@ var targets = {
41
47
  "test:ios"
42
48
  ],
43
49
  devDeps: ["@nativescript/ios"],
44
- note: "needs macOS + Xcode — `pnpm xplat doctor` checks the toolchain"
50
+ note: "needs macOS + Xcode — `bun run xplat doctor` checks the toolchain"
45
51
  },
46
52
  android: {
47
53
  label: "Android (NativeScript)",
@@ -53,10 +59,11 @@ var targets = {
53
59
  "test:android"
54
60
  ],
55
61
  devDeps: ["@nativescript/android"],
56
- note: "needs Android SDK + a compatible JDK — `pnpm xplat doctor` checks the toolchain"
62
+ note: "needs Android SDK + a compatible JDK — `bun run xplat doctor` checks the toolchain"
57
63
  },
58
64
  "native-shared": {
59
65
  hidden: true,
66
+ builds: ["nativescript"],
60
67
  patches: [
61
68
  "@nativescript/core",
62
69
  "@nativescript/types",
@@ -82,17 +89,19 @@ var targets = {
82
89
  "@valor/nativescript-websockets"
83
90
  ],
84
91
  devDeps: [
92
+ "@octane-xplat/workshop",
85
93
  "@nativescript-community/vite-octane",
86
94
  "@nativescript/types",
87
95
  "@nativescript/unit-test-runner",
88
96
  "@nativescript/vite",
89
- "nativescript"
97
+ "nativescript",
98
+ "typescript-classic"
90
99
  ],
91
100
  typecheck: "xplat typecheck"
92
101
  }
93
102
  };
94
103
  var selectableTargets = Object.keys(targets).filter((id) => !targets[id].hidden);
95
- /** `pnpm create` without --targets and `xplat add`'s prompt default. */
104
+ /** `bun create` without --targets and `xplat add`'s prompt default. */
96
105
  var defaultTargets = [
97
106
  "web",
98
107
  "ios",
@@ -143,9 +152,10 @@ function pickKeys(source, names, label) {
143
152
  }
144
153
  /**
145
154
  * The package.json a given target set produces. With every target selected
146
- * this is identical to template/package.json.
155
+ * this is identical to template/package.json. For pnpm the Bun-config homes
156
+ * move to pnpm-workspace.yaml — composePnpmWorkspace emits the other half.
147
157
  */
148
- function composeManifest(ids) {
158
+ function composeManifest(ids, packageManager = "bun") {
149
159
  const resolved = resolveTargets(ids);
150
160
  const tpl = templateManifest();
151
161
  const manifest = { ...tpl };
@@ -166,93 +176,130 @@ function composeManifest(ids) {
166
176
  manifest.scripts = {};
167
177
  for (const name of Object.keys(tpl.scripts ?? {})) if (name === "typecheck" || wantedScripts.has(name)) manifest.scripts[name] = scripts[name];
168
178
  if (!resolved.includes("native-shared")) delete manifest.main;
169
- return manifest;
170
- }
171
- var PATCH_ENTRY = /^\s+"?(@?[\w./-]+@[\w.-]+)"?:\s+\S+\s*$/;
172
- var patchPkg = (key) => key.replace(/@[^@]+$/, "");
173
- /** The pnpm-workspace.yaml a given target set produces. */
174
- function composeWorkspaceYaml(ids) {
175
- const resolved = new Set(resolveTargets(ids));
176
- const drop = /* @__PURE__ */ new Set();
177
- for (const [id, t] of Object.entries(targets)) if (!resolved.has(id)) for (const name of t.patches ?? []) drop.add(name);
178
- const lines = readFileSync(join(TEMPLATE_DIR, "pnpm-workspace.yaml"), "utf8").split("\n");
179
- const out = [];
180
- let inPatches = false;
181
- let pending = [];
182
- for (const line of lines) {
183
- if (/^\S/.test(line)) {
184
- out.push(...pending, line);
185
- pending = [];
186
- inPatches = line.startsWith("patchedDependencies");
187
- continue;
188
- }
189
- if (!inPatches) {
190
- out.push(line);
191
- continue;
192
- }
193
- const m = line.match(PATCH_ENTRY);
194
- if (!m) {
195
- pending.push(line);
196
- continue;
197
- }
198
- if (drop.has(patchPkg(m[1]))) {
199
- pending = [];
200
- continue;
201
- }
202
- out.push(...pending, line);
203
- pending = [];
204
- }
205
- out.push(...pending);
206
- const doc = parseDocument(out.join("\n"));
207
- const manifest = composeManifest(ids);
179
+ const drop = new Set(Object.entries(targets).filter(([id]) => !resolved.includes(id)).flatMap(([, target]) => target.patches ?? []));
180
+ manifest.patchedDependencies = Object.fromEntries(Object.entries(tpl.patchedDependencies).filter(([key]) => !drop.has(key.slice(0, key.indexOf("@", 1)))));
208
181
  const used = /* @__PURE__ */ new Set([...Object.keys(manifest.dependencies), ...Object.keys(manifest.devDependencies)]);
209
- for (const name of Object.keys(doc.toJS().catalogs?.xplat ?? {})) if (!used.has(name)) doc.deleteIn([
210
- "catalogs",
211
- "xplat",
212
- name
213
- ]);
214
- return String(doc);
182
+ manifest.workspaces = {
183
+ ...tpl.workspaces,
184
+ catalogs: {
185
+ ...tpl.workspaces.catalogs,
186
+ xplat: Object.fromEntries(Object.entries(tpl.workspaces.catalogs.xplat).filter(([name]) => used.has(name)))
187
+ }
188
+ };
189
+ if (packageManager === "bun") return manifest;
190
+ return splitForPnpm(manifest, new Set(resolved)).manifest;
191
+ }
192
+ var deniedBuilds = [{ name: "@nativescript/core" }, { name: "protobufjs" }];
193
+ /**
194
+ * The allowBuilds map for a resolved target set: trusted dependencies (with
195
+ * target claims applied) approve, everything pnpm reported denies.
196
+ */
197
+ function allowBuildsFor(manifest, resolved) {
198
+ const wanted = /* @__PURE__ */ new Set();
199
+ for (const id of resolved) for (const name of targets[id].builds ?? []) wanted.add(name);
200
+ const builds = {};
201
+ for (const name of manifest.trustedDependencies ?? []) if (!Object.values(targets).some((target) => (target.builds ?? []).includes(name)) || wanted.has(name)) builds[name] = true;
202
+ for (const entry of deniedBuilds) if (!entry.target || resolved.has(entry.target)) builds[entry.name] = false;
203
+ return Object.fromEntries(Object.keys(builds).sort().map((name) => [name, builds[name]]));
215
204
  }
216
205
  /**
217
- * Add the patch entries that `enabledIds` owns to the owning workspace's
218
- * pnpm-workspace.yaml. Entries already present (by package name, any
219
- * version) are kept. Returns true if the file changed.
206
+ * Move Bun's config homes — catalogs, overrides, patches, trusted
207
+ * dependencies, the bun engine — out of the composed manifest into the
208
+ * pnpm-workspace.yaml object. Both manager variants come from the same
209
+ * composed manifest so pins can never disagree.
220
210
  */
221
- function ensurePatchEntries(appRoot, enabledIds) {
222
- const needed = /* @__PURE__ */ new Set();
223
- for (const id of enabledIds) for (const name of targets[id].patches ?? []) needed.add(name);
224
- if (!needed.size) return false;
225
- let workspace = resolve(appRoot);
226
- while (!existsSync(join(workspace, "pnpm-workspace.yaml")) && dirname(workspace) !== workspace) workspace = dirname(workspace);
227
- if (!existsSync(join(workspace, "pnpm-workspace.yaml"))) workspace = appRoot;
228
- const yamlPath = join(workspace, "pnpm-workspace.yaml");
229
- const appText = existsSync(yamlPath) ? readFileSync(yamlPath, "utf8") : "";
230
- if (!appText.trim()) {
231
- writeFileSync(yamlPath, composeWorkspaceYaml(enabledIds));
232
- return true;
211
+ function splitForPnpm(manifest, resolved) {
212
+ const next = { ...manifest };
213
+ const workspace = {};
214
+ workspace.packages = next.workspaces?.packages ?? [];
215
+ if (next.workspaces?.catalogs) workspace.catalogs = next.workspaces.catalogs;
216
+ delete next.workspaces;
217
+ if (next.overrides) {
218
+ workspace.overrides = next.overrides;
219
+ delete next.overrides;
233
220
  }
234
- const doc = parseDocument(appText);
235
- if (doc.errors.length) throw new Error(`${yamlPath}: ${doc.errors[0].message}`);
236
- const present = new Set(Object.keys(doc.toJS()?.patchedDependencies ?? {}).map(patchPkg));
237
- const template = parseDocument(readFileSync(join(TEMPLATE_DIR, "pnpm-workspace.yaml"), "utf8")).toJS();
238
- let changed = false;
239
- for (const [key, path] of Object.entries(template.patchedDependencies)) {
240
- const name = patchPkg(key);
241
- if (needed.has(name) && !present.has(name)) {
242
- doc.setIn(["patchedDependencies", key], path);
243
- changed = true;
244
- }
221
+ if (next.patchedDependencies) {
222
+ workspace.patchedDependencies = next.patchedDependencies;
223
+ delete next.patchedDependencies;
245
224
  }
246
- for (const [name, spec] of Object.entries(template.configDependencies)) {
247
- const prefix = `node_modules/.pnpm-config/${name}/`;
248
- if (Object.values(doc.toJS()?.patchedDependencies ?? {}).some((path) => typeof path === "string" && path.startsWith(prefix)) && !doc.hasIn(["configDependencies", name])) {
249
- doc.setIn(["configDependencies", name], spec);
250
- changed = true;
251
- }
225
+ workspace.allowUnusedPatches = true;
226
+ workspace.allowBuilds = allowBuildsFor(next, resolved);
227
+ delete next.trustedDependencies;
228
+ workspace.nodeLinker = "hoisted";
229
+ workspace.saveExact = true;
230
+ if (next.engines) {
231
+ delete next.engines.bun;
232
+ if (Object.keys(next.engines).length === 0) delete next.engines;
233
+ }
234
+ next.packageManager = `pnpm@${compatibility.tools.pnpm}`;
235
+ return {
236
+ manifest: next,
237
+ workspace
238
+ };
239
+ }
240
+ /** The pnpm-workspace.yaml a given target set produces. */
241
+ function composePnpmWorkspace(ids) {
242
+ const resolved = resolveTargets(ids);
243
+ const { workspace } = splitForPnpm(composeManifest(resolved), new Set(resolved));
244
+ return stringify(workspace);
245
+ }
246
+ /** Patch [specifier, path] entries the enabled targets need and `present` lacks. */
247
+ function neededPatchEntries(enabledIds, present) {
248
+ const needed = new Set(enabledIds.flatMap((id) => targets[id].patches ?? []));
249
+ const out = [];
250
+ for (const [specifier, path] of Object.entries(templateManifest().patchedDependencies)) {
251
+ const name = specifier.slice(0, specifier.indexOf("@", 1));
252
+ if (!needed.has(name) || present.has(name)) continue;
253
+ out.push([specifier, path]);
254
+ }
255
+ return out;
256
+ }
257
+ /** Copy a canonical patch into the root patches/ dir unless it already matches. */
258
+ function stagePatchFile(root, path, read, write) {
259
+ const source = join(TEMPLATE_DIR, path);
260
+ const destination = join(root, path);
261
+ const before = read(destination);
262
+ if (before !== null && !readFileSync(source).equals(before)) throw new Error(`Custom patch ${destination} differs from the framework patch; reconcile it first`);
263
+ write(destination, readFileSync(source), statSync(source).mode & 511);
264
+ }
265
+ var specifierName = (specifier) => specifier.slice(0, specifier.indexOf("@", 1));
266
+ /** Add target-owned local patches before installing newly enabled dependencies. */
267
+ function ensurePatchEntries(enabledIds, root, pkg, read, write) {
268
+ const present = new Set(Object.keys(pkg.patchedDependencies ?? {}).map(specifierName));
269
+ pkg.patchedDependencies ??= {};
270
+ let changed = false;
271
+ for (const [specifier, path] of neededPatchEntries(enabledIds, present)) {
272
+ stagePatchFile(root, path, read, write);
273
+ pkg.patchedDependencies[specifier] = path;
274
+ changed = true;
252
275
  }
253
- if (changed) writeFileSync(yamlPath, String(doc));
254
276
  return changed;
255
277
  }
278
+ /** The selected manager at a workspace root — mirrors the CLI's detection. */
279
+ function managerForRoot(root, pkg) {
280
+ const declared = pkg?.packageManager?.split("@")[0];
281
+ if (declared !== void 0) {
282
+ if (declared === "bun" || declared === "pnpm") return declared;
283
+ throw new Error(`Xplat supports Bun and pnpm projects; found packageManager ${declared}.`);
284
+ }
285
+ const bun = existsSync(join(root, "bun.lock")) || existsSync(join(root, "bun.lockb"));
286
+ const pnpm = existsSync(join(root, "pnpm-lock.yaml"));
287
+ if (bun && pnpm) throw new Error("Both Bun and pnpm lockfiles exist. Set packageManager in package.json or pass --package-manager.");
288
+ if (bun) return "bun";
289
+ if (pnpm) return "pnpm";
290
+ if (pkg?.patchedDependencies) return "bun";
291
+ return existsSync(join(root, "pnpm-workspace.yaml")) ? "pnpm" : "bun";
292
+ }
293
+ /**
294
+ * Native tsconfig `paths` pinning `@nativescript/core` to the app's own
295
+ * install. Without it, a `link:`/`file:`/`portal:` framework checkout's core
296
+ * imports resolve to that checkout's own copy and checkers see two distinct
297
+ * `View`/`Page`/`Color` type identities (private-member conflicts, issue #66).
298
+ */
299
+ var nativeCorePaths = {
300
+ "@nativescript/core": ["./node_modules/@nativescript/core"],
301
+ "@nativescript/core/*": ["./node_modules/@nativescript/core/*"]
302
+ };
256
303
  /**
257
304
  * The tsconfig.json a given target set produces. One program covers every
258
305
  * platform suffix (moduleSuffixes is the fixed precedence order); a web-only
@@ -263,44 +310,84 @@ function composeTsconfig(ids) {
263
310
  const config = JSON.parse(readFileSync(join(TEMPLATE_DIR, "tsconfig.json"), "utf8"));
264
311
  if (!resolved.has("native-shared")) {
265
312
  delete config.files;
313
+ delete config.compilerOptions.paths;
266
314
  config.compilerOptions.types = (config.compilerOptions.types ?? []).filter((t) => t !== "@nativescript/types");
267
315
  config.exclude = [.../* @__PURE__ */ new Set([...config.exclude ?? [], "src/main.ts"])];
268
316
  }
269
317
  return JSON.stringify(config, null, " ") + "\n";
270
318
  }
319
+ /**
320
+ * Starter text files whose command forms follow the chosen package manager.
321
+ * Nothing under patches/ is ever transformed — patch bytes must equal the
322
+ * canonical copies.
323
+ */
324
+ var managerTextFiles = [
325
+ "AGENTS.md",
326
+ "README.md",
327
+ "nativescript.config.ts",
328
+ "vitest.config.mts",
329
+ "src/app.spec.tsrx"
330
+ ];
331
+ var isManagerTextFile = (rel) => managerTextFiles.includes(rel) || rel.startsWith(".agents/skills/xplat/");
332
+ var renamedFiles = {
333
+ gitignore: ".gitignore",
334
+ bunfig: "bunfig.toml"
335
+ };
336
+ var destName = (rel) => renamedFiles[rel] ?? rel;
337
+ /**
338
+ * Rewrite the canonical Bun command forms in starter text for another
339
+ * manager. Applied only to managerTextFiles.
340
+ */
341
+ function localizeCommands(text, manager) {
342
+ if (manager === "bun") return text;
343
+ return text.replace(/\bbun run (\S+) -- /g, "pnpm run $1 ").replace(/\bbunx\b/g, "pnpm dlx").replace(/\bbun pm pack\b/g, "pnpm pack").replace(/\bbun run\b/g, "pnpm run").replace(/\bbun add\b/g, "pnpm add").replace(/\bbun install\b/g, "pnpm install").replace(/packageManager: 'bun'/g, "packageManager: 'pnpm'");
344
+ }
271
345
  /** Write a scaffold with tests for `ids` into `dir` (must not exist or be empty). */
272
- function composeTargets(ids, dir) {
346
+ function composeTargets(ids, dir, options) {
347
+ const packageManager = options?.packageManager ?? "bun";
273
348
  const resolved = new Set(resolveTargets(ids));
274
349
  mkdirSync(dir, { recursive: true });
275
350
  for (const rel of walk(TEMPLATE_DIR)) {
276
- if (rel === "package.json" || rel === "pnpm-workspace.yaml" || rel === "tsconfig.json") continue;
351
+ if (rel === "package.json" || rel === "tsconfig.json") continue;
352
+ if (packageManager === "pnpm" && rel === "bunfig") continue;
277
353
  const owner = ownerOf(rel);
278
354
  if (owner && !resolved.has(owner)) continue;
279
- const dest = join(dir, rel === "gitignore" ? ".gitignore" : rel);
355
+ const source = join(TEMPLATE_DIR, rel);
356
+ const dest = join(dir, destName(rel));
280
357
  mkdirSync(dirname(dest), { recursive: true });
281
- cpSync(join(TEMPLATE_DIR, rel), dest, { recursive: true });
358
+ if (packageManager === "pnpm" && isManagerTextFile(rel)) writeFileSync(dest, localizeCommands(readFileSync(source, "utf8"), packageManager));
359
+ else cpSync(source, dest, { recursive: true });
282
360
  }
283
- writeFileSync(join(dir, "package.json"), JSON.stringify(composeManifest(ids), null, " ") + "\n");
284
- writeFileSync(join(dir, "pnpm-workspace.yaml"), composeWorkspaceYaml(ids));
361
+ writeFileSync(join(dir, "package.json"), JSON.stringify(composeManifest(ids, packageManager), null, " ") + "\n");
285
362
  writeFileSync(join(dir, "tsconfig.json"), composeTsconfig(ids));
363
+ if (packageManager === "pnpm") writeFileSync(join(dir, "pnpm-workspace.yaml"), composePnpmWorkspace(ids));
286
364
  return [...resolved];
287
365
  }
288
366
  /** Write the cross-platform library starter into `dir`. */
289
- function composeLibrary(dir, packageName = basename(resolve(dir))) {
367
+ function composeLibrary(dir, packageName = basename(resolve(dir)), options) {
290
368
  if (packageName.length > 214 || !/^(?:@[a-z0-9][a-z0-9._-]*\/)?[a-z0-9][a-z0-9._-]*$/.test(packageName)) throw new Error(`invalid npm package name "${packageName}" — use lowercase letters, numbers, dots, underscores, or hyphens`);
369
+ const packageManager = options?.packageManager ?? "bun";
291
370
  mkdirSync(dir, { recursive: true });
292
371
  for (const rel of walk(LIBRARY_TEMPLATE_DIR)) {
293
- if (rel === "package.json") continue;
294
- const dest = join(dir, rel === "gitignore" ? ".gitignore" : rel);
372
+ if (rel === "package.json" || packageManager === "pnpm" && rel === "bunfig") continue;
373
+ const dest = join(dir, destName(rel));
295
374
  mkdirSync(dirname(dest), { recursive: true });
296
375
  const source = join(LIBRARY_TEMPLATE_DIR, rel);
297
376
  if (/\.(?:md|ts|tsx|mjs|json)$/.test(rel)) {
298
- const content = readFileSync(source, "utf8").replaceAll("octane-xplat-library", packageName);
377
+ let content = readFileSync(source, "utf8").replaceAll("octane-xplat-library", packageName);
378
+ if (packageManager === "pnpm" && isManagerTextFile(rel)) content = localizeCommands(content, packageManager);
299
379
  writeFileSync(dest, content);
300
380
  } else cpSync(source, dest, { recursive: true });
301
381
  }
302
382
  const manifest = JSON.parse(readFileSync(join(LIBRARY_TEMPLATE_DIR, "package.json"), "utf8"));
303
383
  manifest.name = packageName;
384
+ if (packageManager === "pnpm") {
385
+ for (const [name, script] of Object.entries(manifest.scripts ?? {})) manifest.scripts[name] = localizeCommands(String(script), packageManager);
386
+ const { manifest: split, workspace } = splitForPnpm(manifest, /* @__PURE__ */ new Set(["native-shared"]));
387
+ writeFileSync(join(dir, "package.json"), JSON.stringify(split, null, " ") + "\n");
388
+ writeFileSync(join(dir, "pnpm-workspace.yaml"), stringify(workspace));
389
+ return;
390
+ }
304
391
  writeFileSync(join(dir, "package.json"), JSON.stringify(manifest, null, " ") + "\n");
305
392
  }
306
393
  var readJson = (file) => {
@@ -326,15 +413,14 @@ function targetEnabled(appRoot, id) {
326
413
  return runtime ? typeof declared[runtime] === "string" : false;
327
414
  }
328
415
  /** Copy a template file/dir into the app unless the destination exists. */
329
- function copyMissing(rel, appRoot, report) {
416
+ function copyMissing(rel, appRoot, report, present, copy) {
330
417
  const src = join(TEMPLATE_DIR, rel);
331
- const dest = join(appRoot, rel === "gitignore" ? ".gitignore" : rel);
332
- if (existsSync(dest)) {
418
+ const dest = join(appRoot, destName(rel));
419
+ if (present(dest)) {
333
420
  report.skipped.push(rel);
334
421
  return;
335
422
  }
336
- mkdirSync(dirname(dest), { recursive: true });
337
- cpSync(src, dest, { recursive: true });
423
+ copy(src, dest);
338
424
  report.copied.push(rel);
339
425
  }
340
426
  /**
@@ -343,27 +429,112 @@ function copyMissing(rel, appRoot, report) {
343
429
  * suffix — so enabling native means wiring the ambient declaration file and
344
430
  * the entrypoint into the app's own config.
345
431
  */
346
- function enableNativeInTsconfig(appRoot) {
432
+ function enableNativeInTsconfig(appRoot, read, write) {
347
433
  const path = join(appRoot, "tsconfig.json");
348
- const text = existsSync(path) ? readFileSync(path, "utf8") : "";
349
- const config = readJson(path);
434
+ const text = read(path)?.toString("utf8") ?? "";
435
+ let config;
436
+ try {
437
+ config = JSON.parse(text);
438
+ } catch {
439
+ return false;
440
+ }
350
441
  if (!config || typeof config !== "object") return false;
351
442
  const indent = text.match(/\n(\s+)"/)?.[1] ?? " ";
352
443
  config.files = [.../* @__PURE__ */ new Set(["references.d.ts", ...config.files ?? []])];
353
444
  config.exclude = (config.exclude ?? []).filter((e) => e !== "src/main.ts");
354
445
  const types = config.compilerOptions?.types;
355
446
  if (config.compilerOptions && Array.isArray(types) && !types.includes("@nativescript/types")) config.compilerOptions.types = [...types, "@nativescript/types"];
356
- writeFileSync(path, JSON.stringify(config, null, indent) + "\n");
447
+ if (config.compilerOptions && typeof config.compilerOptions === "object") {
448
+ const paths = config.compilerOptions.paths ??= {};
449
+ for (const [key, value] of Object.entries(nativeCorePaths)) if (!(key in paths)) paths[key] = value;
450
+ }
451
+ write(path, Buffer.from(JSON.stringify(config, null, indent) + "\n"), 420);
357
452
  return true;
358
453
  }
359
454
  /**
360
- * Enable `id` (plus unmet `requires`) inside an existing app at `appRoot`.
361
- * Additive only: existing files, scripts, and dep entries are skipped, never
362
- * overwritten. Returns a report for the caller to render.
455
+ * Plan enabling `id` and unmet requirements without writing. Existing target
456
+ * files, scripts and dependency choices stay intact; managed config and catalog
457
+ * changes carry raw-byte input hashes for the publisher.
363
458
  */
364
- function applyTarget(appRoot, id) {
459
+ function planTarget(appRoot, id) {
460
+ appRoot = resolve(appRoot);
461
+ const root = compatibilityRoot(appRoot);
462
+ const inputs = /* @__PURE__ */ new Map();
463
+ const contents = /* @__PURE__ */ new Map();
464
+ const files = /* @__PURE__ */ new Map();
465
+ const directories = /* @__PURE__ */ new Map();
466
+ const hash = (bytes) => createHash("sha256").update(bytes).digest("hex");
467
+ const within = (path) => {
468
+ const rel = relative(root, path);
469
+ return rel === "" || rel !== ".." && !rel.startsWith(".." + sep) && !isAbsolute(rel);
470
+ };
471
+ const observe = (path) => {
472
+ path = resolve(path);
473
+ if (!within(path)) throw new Error(`Scaffold path leaves its workspace: ${path}`);
474
+ if (inputs.has(path)) return inputs.get(path);
475
+ let info;
476
+ try {
477
+ info = lstatSync(path);
478
+ } catch (error) {
479
+ if (error.code !== "ENOENT") throw error;
480
+ const input = {
481
+ path,
482
+ kind: "missing"
483
+ };
484
+ inputs.set(path, input);
485
+ return input;
486
+ }
487
+ if (info.isSymbolicLink() || !info.isDirectory() && !info.isFile()) throw new Error(`Scaffold path must be an ordinary file or directory: ${path}`);
488
+ const input = {
489
+ path,
490
+ kind: info.isDirectory() ? "directory" : "file",
491
+ mode: info.mode & 511
492
+ };
493
+ if (input.kind === "file") {
494
+ if (info.size > 16777216) throw new Error(`Scaffold input exceeds 16 MiB: ${path}`);
495
+ const bytes = readFileSync(path);
496
+ input.sha256 = hash(bytes);
497
+ contents.set(path, bytes);
498
+ }
499
+ inputs.set(path, input);
500
+ return input;
501
+ };
502
+ const read = (path) => {
503
+ const input = observe(path);
504
+ if (input.kind === "missing") return null;
505
+ if (input.kind !== "file") throw new Error(`Expected scaffold file: ${path}`);
506
+ return contents.get(resolve(path));
507
+ };
508
+ const directory = (path, mode = 493) => {
509
+ if (path === root || path === appRoot) return;
510
+ const input = observe(path);
511
+ if (input.kind === "directory") return;
512
+ if (input.kind !== "missing") throw new Error(`Expected scaffold directory: ${path}`);
513
+ directory(dirname(path));
514
+ directories.set(path, mode);
515
+ };
516
+ const write = (path, bytes, mode) => {
517
+ const before = read(path);
518
+ if (before !== null && before.equals(bytes)) return;
519
+ directory(dirname(path));
520
+ files.set(path, {
521
+ path,
522
+ before: before === null ? null : hash(before),
523
+ content: bytes.toString("base64"),
524
+ mode: before === null ? mode : observe(path).mode
525
+ });
526
+ };
527
+ const copy = (source, dest) => {
528
+ const info = lstatSync(source);
529
+ if (info.isDirectory()) {
530
+ directory(dest, info.mode & 511);
531
+ for (const name of readdirSync(source).sort()) copy(join(source, name), join(dest, name));
532
+ } else if (info.isFile()) write(dest, readFileSync(source), info.mode & 511);
533
+ else throw new Error(`Scaffold template must not contain aliases: ${source}`);
534
+ };
365
535
  const manifestPath = join(appRoot, "package.json");
366
- const raw = readFileSync(manifestPath, "utf8");
536
+ const raw = read(manifestPath)?.toString("utf8");
537
+ if (raw === void 0) throw new Error(`No package.json at ${appRoot}`);
367
538
  const manifest = JSON.parse(raw);
368
539
  const tpl = templateManifest();
369
540
  const report = {
@@ -375,15 +546,22 @@ function applyTarget(appRoot, id) {
375
546
  scriptsSkipped: [],
376
547
  depsAdded: []
377
548
  };
378
- let touched = false;
379
549
  for (const tid of resolveTargets([id])) {
380
- if (targetEnabled(appRoot, tid)) {
550
+ const declared = {
551
+ ...manifest.dependencies,
552
+ ...manifest.devDependencies
553
+ };
554
+ const runtime = {
555
+ ios: "@nativescript/ios",
556
+ android: "@nativescript/android"
557
+ }[tid];
558
+ if (tid === "web" ? observe(join(appRoot, "vite.config.ts")).kind !== "missing" || observe(join(appRoot, "vite.config.mts")).kind !== "missing" : tid === "native-shared" ? observe(join(appRoot, "nativescript.config.ts")).kind !== "missing" : runtime !== void 0 && typeof declared[runtime] === "string") {
381
559
  report.already.push(tid);
382
560
  continue;
383
561
  }
384
562
  report.enabled.push(tid);
385
563
  const t = targets[tid];
386
- for (const rel of t.files) copyMissing(rel, appRoot, report);
564
+ for (const rel of t.files) copyMissing(rel, appRoot, report, (path) => observe(path).kind !== "missing", copy);
387
565
  for (const [section, key] of [["dependencies", "deps"], ["devDependencies", "devDeps"]]) for (const name of t[key] ?? []) {
388
566
  manifest[section] ??= {};
389
567
  if (manifest[section][name] === void 0) {
@@ -405,17 +583,89 @@ function applyTarget(appRoot, id) {
405
583
  }
406
584
  if (tid === "native-shared") {
407
585
  if (manifest.main === void 0) manifest.main = tpl.main;
408
- report.tsconfigUpdated = enableNativeInTsconfig(appRoot);
586
+ report.tsconfigUpdated = enableNativeInTsconfig(appRoot, read, write);
409
587
  }
410
- touched = true;
411
588
  }
412
- if (ensurePatchEntries(appRoot, [...report.enabled, ...report.already])) report.patchesAdded = true;
413
- ensureCompatibilityCatalog(appRoot, manifest);
414
- if (touched) {
589
+ const enabledTargets = [...report.enabled, ...report.already];
590
+ if (managerForRoot(root, root === appRoot ? manifest : JSON.parse(read(join(root, "package.json"))?.toString("utf8") ?? "null")) === "pnpm") {
591
+ const yamlPath = join(root, "pnpm-workspace.yaml");
592
+ const doc = parseDocument(read(yamlPath)?.toString("utf8") ?? "packages: []\n");
593
+ const { catalog, pins } = requestedCatalogPins(manifest);
594
+ for (const [name, spec] of Object.entries(pins)) if (doc.getIn([
595
+ "catalogs",
596
+ catalog,
597
+ name
598
+ ]) === void 0) doc.setIn([
599
+ "catalogs",
600
+ catalog,
601
+ name
602
+ ], spec);
603
+ const existing = doc.get("patchedDependencies");
604
+ const patches = neededPatchEntries(enabledTargets, new Set((isMap(existing) ? existing.items : []).map((pair) => specifierName(String(pair.key)))));
605
+ for (const [specifier, path] of patches) {
606
+ stagePatchFile(root, path, read, write);
607
+ doc.setIn(["patchedDependencies", specifier], path);
608
+ }
609
+ if (patches.length) report.patchesAdded = true;
610
+ const resolved = new Set(resolveTargets(enabledTargets));
611
+ for (const id of resolved) for (const name of targets[id].builds ?? []) if (doc.getIn(["allowBuilds", name]) === void 0) {
612
+ doc.setIn(["allowBuilds", name], true);
613
+ report.allowBuildsAdded = true;
614
+ }
615
+ for (const entry of deniedBuilds) if ((!entry.target || resolved.has(entry.target)) && doc.getIn(["allowBuilds", entry.name]) === void 0) {
616
+ doc.setIn(["allowBuilds", entry.name], false);
617
+ report.allowBuildsAdded = true;
618
+ }
619
+ write(yamlPath, Buffer.from(String(doc)), 420);
620
+ } else {
621
+ const owner = root === appRoot ? manifest : JSON.parse(read(join(root, "package.json")).toString("utf8"));
622
+ if (ensurePatchEntries(enabledTargets, root, owner, read, write)) report.patchesAdded = true;
623
+ mergeCompatibilityCatalog(manifest, owner);
624
+ if (root !== appRoot) write(join(root, "package.json"), Buffer.from(JSON.stringify(owner, null, " ") + "\n"), 420);
625
+ }
626
+ if (JSON.stringify(manifest) !== JSON.stringify(JSON.parse(raw))) {
415
627
  const indent = raw.match(/\n(\s+)"/)?.[1] ?? " ";
416
- writeFileSync(manifestPath, JSON.stringify(manifest, null, indent) + "\n");
628
+ write(manifestPath, Buffer.from(JSON.stringify(manifest, null, indent) + "\n"), 420);
629
+ }
630
+ if (files.size > 8192 || inputs.size > 2e4 || directories.size > 8192) throw new Error("Scaffold plan exceeds inventory limits");
631
+ let total = 0;
632
+ for (const file of files.values()) {
633
+ const bytes = Buffer.from(file.content, "base64").length;
634
+ total += bytes;
635
+ if (bytes > 16777216 || total > 33554432) throw new Error("Scaffold plan exceeds byte limits");
636
+ }
637
+ return {
638
+ version: 1,
639
+ root,
640
+ appRoot,
641
+ id,
642
+ files: [...files.values()],
643
+ directories: [...directories].map(([path, mode]) => ({
644
+ path,
645
+ mode
646
+ })),
647
+ inputs: [...inputs.values()],
648
+ report
649
+ };
650
+ }
651
+ /** Apply the same read-only plan used by native CLI orchestration. */
652
+ function applyTarget(appRoot, id) {
653
+ const plan = planTarget(appRoot, id);
654
+ for (const input of plan.inputs) {
655
+ let info;
656
+ try {
657
+ info = lstatSync(input.path);
658
+ } catch (error) {
659
+ if (error.code !== "ENOENT") throw error;
660
+ }
661
+ const kind = !info ? "missing" : info.isFile() ? "file" : info.isDirectory() ? "directory" : "alias";
662
+ if (kind !== input.kind || kind === "file" && createHash("sha256").update(readFileSync(input.path)).digest("hex") !== input.sha256) throw new Error(`Scaffold input changed; retry: ${input.path}`);
417
663
  }
418
- return report;
664
+ for (const entry of plan.directories) mkdirSync(entry.path, {
665
+ recursive: true,
666
+ mode: entry.mode
667
+ });
668
+ for (const entry of plan.files) writeFileSync(entry.path, Buffer.from(entry.content, "base64"), { mode: entry.mode });
669
+ return plan.report;
419
670
  }
420
- //#endregion
421
- export { LIBRARY_TEMPLATE_DIR, TEMPLATE_DIR, applyTarget, composeLibrary, composeManifest, composeTargets, composeTsconfig, composeWorkspaceYaml, defaultTargets, resolveTargets, selectableTargets, targetEnabled, targets };
671
+ export { LIBRARY_TEMPLATE_DIR, TEMPLATE_DIR, applyTarget, composeLibrary, composeManifest, composePnpmWorkspace, composeTargets, composeTsconfig, defaultTargets, localizeCommands, nativeCorePaths, planTarget, resolveTargets, selectableTargets, targetEnabled, targets };