@legendapp/spark 0.0.1-next.4 → 0.0.1-next.6
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.
- package/README.md +1 -1
- package/customize.ts +2 -0
- package/html.ts +1 -0
- package/markdown.ts +1 -0
- package/package.json +22 -13
- package/patches/dependencies.json +10 -4
- package/patches/react-native-enriched-html@1.1.1.patch +854 -0
- package/patches/{react-native-enriched-markdown@1.0.2.patch → react-native-enriched-markdown@1.1.1.patch} +2560 -1983
- package/schema.json +104 -0
- package/skills/spark/SKILL.md +56 -0
- package/skills/spark/UPGRADING.md +254 -0
- package/vendor/node_modules/@legendapp/spark-ai/package.json +4 -4
- package/vendor/node_modules/@legendapp/spark-ai/src/invocation-command.cjs +11 -0
- package/vendor/node_modules/@legendapp/spark-ai/src/invocation-command.d.cts +2 -0
- package/vendor/node_modules/@legendapp/spark-ai/src/invocations.ts +2 -19
- package/vendor/node_modules/@legendapp/spark-audio/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-auth-session/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-cli/dist/add-desktop.js +4 -4
- package/vendor/node_modules/@legendapp/spark-cli/dist/build.js +3 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/create.js +31 -11
- package/vendor/node_modules/@legendapp/spark-cli/dist/customize-ai.cjs +108 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/customize-artifacts.js +315 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/customize-cli.cjs +75 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/customize-github.cjs +133 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/customize-policy.cjs +102 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/customize-provider.cjs +2 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/customize-worker.cjs +273 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/dependency-pins.js +54 -29
- package/vendor/node_modules/@legendapp/spark-cli/dist/dev-command.js +2 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/enriched-html-pins.js +128 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/expo-dev-patch.cjs +30 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/index.js +16 -7
- package/vendor/node_modules/@legendapp/spark-cli/dist/init-template.d.cts +1 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/js-runtime.cjs +96 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/migration-helpers.cjs +152 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/migration-helpers.d.cts +15 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/migrations.cjs +184 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/migrations.d.cts +16 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/native-patch-preflight.js +2 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/package-manager-config.js +41 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/package-manager.js +13 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/project-version.js +12 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/project.js +40 -9
- package/vendor/node_modules/@legendapp/spark-cli/dist/release.json +19 -19
- package/vendor/node_modules/@legendapp/spark-cli/dist/runtime-bootstrap.ps1 +87 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/runtime-bootstrap.sh +107 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/runtime-downloads.tsv +13 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/sdk-install.js +8 -3
- package/vendor/node_modules/@legendapp/spark-cli/dist/upgrade.js +102 -0
- package/vendor/node_modules/@legendapp/spark-cli/package.json +6 -4
- package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/.claude/skills/spark/SKILL.md +9 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/AGENTS.md +4 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/CLAUDE.md +4 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/README.md +15 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/blank-typescript/package.json +46 -2
- package/vendor/node_modules/@legendapp/spark-cli/templates/universal/.claude/skills/spark/SKILL.md +9 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/universal/AGENTS.md +4 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/universal/CLAUDE.md +4 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/universal/README.md +15 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/universal/package.json +45 -2
- package/vendor/node_modules/@legendapp/spark-cli/templates/windows/.claude/skills/spark/SKILL.md +9 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/windows/AGENTS.md +4 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/windows/CLAUDE.md +4 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/windows/README.md +15 -0
- package/vendor/node_modules/@legendapp/spark-cli/templates/windows/package.json +46 -2
- package/vendor/node_modules/@legendapp/spark-clipboard/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-codex/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-commands/package.json +6 -6
- package/vendor/node_modules/@legendapp/spark-context-menu/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-desktop-app/macos/RNDesktopApp.mm +14 -1
- package/vendor/node_modules/@legendapp/spark-desktop-app/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-desktop-app/src/customize-hooks.ts +17 -0
- package/vendor/node_modules/@legendapp/spark-desktop-app/src/customize.ts +117 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/app.plugin.cjs +20 -1
- package/vendor/node_modules/@legendapp/spark-desktop-config/config.cjs +3 -2
- package/vendor/node_modules/@legendapp/spark-desktop-config/config.d.cts +14 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/customize-provider.cjs +16 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/customize.cjs +16 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/package.json +4 -4
- package/vendor/node_modules/@legendapp/spark-desktop-config/schema.json +104 -0
- package/vendor/node_modules/@legendapp/spark-desktop-host/SparkCustomize.mm +517 -0
- package/vendor/node_modules/@legendapp/spark-desktop-host/package.json +4 -3
- package/vendor/node_modules/@legendapp/spark-desktop-links/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-desktop-shortcuts/macos/RNDesktopShortcuts.mm +3 -1
- package/vendor/node_modules/@legendapp/spark-desktop-shortcuts/macos/keyboard-manager/RNKeyboardManager.h +3 -0
- package/vendor/node_modules/@legendapp/spark-desktop-shortcuts/macos/keyboard-manager/RNKeyboardManager.mm +1 -1
- package/vendor/node_modules/@legendapp/spark-desktop-shortcuts/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-desktop-windows/macos/window-manager/RNWindowManager.mm +5 -4
- package/vendor/node_modules/@legendapp/spark-desktop-windows/package.json +4 -4
- package/vendor/node_modules/@legendapp/spark-desktop-windows/src/api.ts +62 -25
- package/vendor/node_modules/@legendapp/spark-desktop-windows/src/macos-adapter.ts +7 -5
- package/vendor/node_modules/@legendapp/spark-documents/package.json +8 -8
- package/vendor/node_modules/@legendapp/spark-drag-drop/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-file-dialog/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-file-system/macos/SparkFileMutations.h +1 -0
- package/vendor/node_modules/@legendapp/spark-file-system/macos/SparkFileMutations.mm +11 -10
- package/vendor/node_modules/@legendapp/spark-file-system/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-global-shortcuts/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-html/package.json +24 -0
- package/vendor/node_modules/@legendapp/spark-html/src/Html.tsx +22 -0
- package/vendor/node_modules/@legendapp/spark-html/src/HtmlInput.tsx +102 -0
- package/vendor/node_modules/@legendapp/spark-html/src/index.ts +4 -0
- package/vendor/node_modules/@legendapp/spark-html/src/types.ts +4 -0
- package/vendor/node_modules/@legendapp/spark-markdown/package.json +24 -0
- package/vendor/node_modules/@legendapp/spark-markdown/src/Markdown.tsx +24 -0
- package/vendor/node_modules/@legendapp/spark-markdown/src/MarkdownInput.tsx +111 -0
- package/vendor/node_modules/@legendapp/spark-markdown/src/index.ts +4 -0
- package/vendor/node_modules/@legendapp/spark-message-dialog/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-native-menu/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-notifications/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-processes/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-rich-text/package.json +18 -0
- package/vendor/node_modules/@legendapp/spark-rich-text/src/components.tsx +52 -0
- package/vendor/node_modules/@legendapp/spark-rich-text/src/index.ts +4 -0
- package/vendor/node_modules/@legendapp/spark-rich-text/src/types.ts +24 -0
- package/vendor/node_modules/@legendapp/spark-rich-text/src/useInputShortcuts.macos.ts +55 -0
- package/vendor/node_modules/@legendapp/spark-rich-text/src/useInputShortcuts.ts +13 -0
- package/vendor/node_modules/@legendapp/spark-rich-text/src/useInputValue.ts +14 -0
- package/vendor/node_modules/@legendapp/spark-rich-text/src/value.ts +16 -0
- package/vendor/node_modules/@legendapp/spark-secure-storage/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-settings/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-settings-ui/package.json +5 -5
- package/vendor/node_modules/@legendapp/spark-sqlite/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-system/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-tray/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-ui/macos/appkit-split-view/RNSidebarSplitViewComponent.mm +102 -14
- package/vendor/node_modules/@legendapp/spark-ui/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-ui/src/appkit-split-view/SidebarSplitViewNativeComponent.ts +5 -0
- package/vendor/node_modules/@legendapp/spark-ui/src/appkit-split-view/index.tsx +12 -4
- package/vendor/node_modules/@legendapp/spark-updates/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-webview/package.json +2 -2
- package/vendor/node_modules/@legendapp/spark-window-options/package.json +1 -1
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { overridePackageName } from "./package-manager.js";
|
|
1
2
|
import { readFileSync, writeFileSync } from "node:fs";
|
|
2
3
|
import path from "node:path";
|
|
3
4
|
import ts from "typescript";
|
|
@@ -6,8 +7,9 @@ const packages = ["react", "react-native", "react-native-macos", "expo"];
|
|
|
6
7
|
// create/refreshLocalPackages use this starter's tested compatibility matrix too.
|
|
7
8
|
const defaults = JSON.parse(readFileSync(new URL("../templates/blank-typescript/package.json", import.meta.url), "utf8"));
|
|
8
9
|
export const expectedDependencyPins = Object.fromEntries(packages.map(name => [name, defaults.dependencies[name]]));
|
|
9
|
-
export function dependencyPinIssues(pkg, platforms) {
|
|
10
|
-
const required = packages.filter(name => name !== "react-native-macos" || platforms.includes("macos"));
|
|
10
|
+
export function dependencyPinIssues(pkg, platforms, expected = expectedDependencyPins) {
|
|
11
|
+
const required = [...packages, ...(expected["@legendapp/spark"] ? ["@legendapp/spark"] : [])].filter(name => name !== "react-native-macos" || platforms.includes("macos"));
|
|
12
|
+
const checked = Object.keys(expected).filter(name => name !== "react-native-macos" || platforms.includes("macos"));
|
|
11
13
|
const issues = [];
|
|
12
14
|
function check(name, value, where) {
|
|
13
15
|
// npm and pnpm permit an override to reference a direct dependency's spec.
|
|
@@ -17,10 +19,10 @@ export function dependencyPinIssues(pkg, platforms) {
|
|
|
17
19
|
console.log(`Skipping dependency pin check for ${where.join(" → ")}: ${resolved} is managed by the catalog or workspace.`);
|
|
18
20
|
return;
|
|
19
21
|
}
|
|
20
|
-
if (resolved !==
|
|
21
|
-
issues.push({ package: name, found: typeof value === "string" ? value : JSON.stringify(value), expected:
|
|
22
|
+
if (resolved !== expected[name])
|
|
23
|
+
issues.push({ package: name, found: typeof value === "string" ? value : JSON.stringify(value), expected: expected[name], where });
|
|
22
24
|
}
|
|
23
|
-
for (const name of
|
|
25
|
+
for (const name of checked) {
|
|
24
26
|
let present = false;
|
|
25
27
|
for (const section of ["dependencies", "devDependencies"]) {
|
|
26
28
|
if (Object.hasOwn(pkg[section] ?? {}, name)) {
|
|
@@ -28,16 +30,12 @@ export function dependencyPinIssues(pkg, platforms) {
|
|
|
28
30
|
check(name, pkg[section][name], [section, name]);
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
|
-
if (!present)
|
|
32
|
-
issues.push({ package: name, found: "(missing)", expected:
|
|
33
|
+
if (!present && required.includes(name))
|
|
34
|
+
issues.push({ package: name, found: "(missing)", expected: expected[name], where: ["dependencies", name], missing: true });
|
|
33
35
|
}
|
|
34
36
|
function overrides(entries, where, parent) {
|
|
35
37
|
for (const [selector, value] of Object.entries(entries ?? {})) {
|
|
36
|
-
|
|
37
|
-
// Comparators after @, whitespace or || belong to the version range.
|
|
38
|
-
const child = selector.split(/(?<![@\s|])>(?=[@a-zA-Z])/).at(-1);
|
|
39
|
-
const segments = child.split("/");
|
|
40
|
-
const name = selector === "." ? parent : segments.at(-2)?.startsWith("@") ? undefined : required.find(name => new RegExp(`^${name}(?:@[^/]*)?$`).test(segments.at(-1)));
|
|
38
|
+
const name = selector === "." ? parent : overridePackageName(selector, checked);
|
|
41
39
|
const location = [...where, selector];
|
|
42
40
|
if (value && typeof value === "object" && !Array.isArray(value))
|
|
43
41
|
overrides(value, location, name);
|
|
@@ -61,7 +59,7 @@ export function printDependencyPins(issues) {
|
|
|
61
59
|
console.log(row.map((value, column) => value.padEnd(widths[column])).join(" ").trimEnd());
|
|
62
60
|
}
|
|
63
61
|
/** Edit only mismatched values and insert missing pins, preserving existing formatting. */
|
|
64
|
-
export function upgradeDependencyPins(root, dryRun = false) {
|
|
62
|
+
export function upgradeDependencyPins(root, dryRun = false, expected = expectedDependencyPins, requiredOverrides) {
|
|
65
63
|
const file = path.join(root, "package.json");
|
|
66
64
|
let source = readFileSync(file, "utf8");
|
|
67
65
|
const pkg = JSON.parse(source);
|
|
@@ -92,27 +90,53 @@ export function upgradeDependencyPins(root, dryRun = false) {
|
|
|
92
90
|
rejectDuplicates(pnpm, ["pnpm"], false);
|
|
93
91
|
if (pnpm && ts.isObjectLiteralExpression(pnpm))
|
|
94
92
|
rejectDuplicates(property(pnpm, "overrides")?.initializer, ["pnpm", "overrides"]);
|
|
95
|
-
const issues = dependencyPinIssues(pkg, supportedPlatforms(root));
|
|
96
|
-
if (
|
|
93
|
+
const issues = dependencyPinIssues(pkg, supportedPlatforms(root), expected);
|
|
94
|
+
if (requiredOverrides) {
|
|
95
|
+
let entries = pkg;
|
|
96
|
+
for (const key of requiredOverrides.where)
|
|
97
|
+
entries = entries?.[key];
|
|
98
|
+
for (const [name, version] of Object.entries(requiredOverrides.pins)) {
|
|
99
|
+
if (!Object.hasOwn(entries ?? {}, name))
|
|
100
|
+
issues.push({ package: name, found: "(missing)", expected: version, where: [...requiredOverrides.where, name], missing: true });
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (!issues.length)
|
|
97
104
|
return issues;
|
|
98
105
|
const edits = [];
|
|
99
106
|
const missing = issues.filter(issue => issue.missing);
|
|
100
107
|
if (missing.length) {
|
|
101
|
-
if ("dependencies" in pkg && (!pkg.dependencies || typeof pkg.dependencies !== "object" || Array.isArray(pkg.dependencies)))
|
|
102
|
-
throw new Error("package.json dependencies must be an object. No files were changed.");
|
|
103
108
|
const indent = source.match(/^[\t ]+(?=")/m)?.[0] ?? " ";
|
|
104
109
|
const newline = source.includes("\r\n") ? "\r\n" : "\n";
|
|
105
|
-
const
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
110
|
+
const additions = {};
|
|
111
|
+
for (const issue of missing) {
|
|
112
|
+
let entries = additions;
|
|
113
|
+
for (const key of issue.where.slice(0, -1))
|
|
114
|
+
entries = entries[key] ??= {};
|
|
115
|
+
entries[issue.where.at(-1)] = issue.expected;
|
|
116
|
+
}
|
|
117
|
+
function insert(target, additions, where) {
|
|
118
|
+
const entries = [];
|
|
119
|
+
for (const [key, value] of Object.entries(additions)) {
|
|
120
|
+
const existing = property(target, key)?.initializer;
|
|
121
|
+
if (existing) {
|
|
122
|
+
if (!ts.isObjectLiteralExpression(existing))
|
|
123
|
+
throw new Error(`package.json ${[...where, key].join(" → ")} must be an object. No files were changed.`);
|
|
124
|
+
insert(existing, value, [...where, key]);
|
|
125
|
+
}
|
|
126
|
+
else
|
|
127
|
+
entries.push(`${JSON.stringify(key)}: ${JSON.stringify(value, null, indent)}`);
|
|
128
|
+
}
|
|
129
|
+
if (!entries.length)
|
|
130
|
+
return;
|
|
131
|
+
const last = target.properties.at(-1);
|
|
132
|
+
const baseIndent = source.slice(0, target.getStart(parsed)).match(/(?:^|\n)([\t ]*)[^\n]*$/)[1];
|
|
133
|
+
const multiline = !last || source.slice(target.getStart(parsed), target.end).includes("\n");
|
|
134
|
+
const separator = multiline ? newline + baseIndent + indent : " ";
|
|
135
|
+
const text = entries.map(entry => entry.replaceAll("\n", newline + baseIndent + indent)).join("," + separator);
|
|
136
|
+
edits.push(last ? { start: last.end, end: last.end, text: "," + separator + text }
|
|
137
|
+
: { start: target.getStart(parsed) + 1, end: target.end - 1, text: separator + text + newline + baseIndent });
|
|
138
|
+
}
|
|
139
|
+
insert(object, additions, []);
|
|
116
140
|
}
|
|
117
141
|
for (const issue of issues) {
|
|
118
142
|
if (issue.missing)
|
|
@@ -127,6 +151,7 @@ export function upgradeDependencyPins(root, dryRun = false) {
|
|
|
127
151
|
}
|
|
128
152
|
for (const edit of edits.sort((a, b) => b.start - a.start))
|
|
129
153
|
source = source.slice(0, edit.start) + edit.text + source.slice(edit.end);
|
|
130
|
-
|
|
154
|
+
if (!dryRun)
|
|
155
|
+
writeFileSync(file, source);
|
|
131
156
|
return issues;
|
|
132
157
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { spawnProcess } from "./process.js";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { prepareConfig, selectTarget, supportedPlatforms } from "@legendapp/spark-desktop-config/config.cjs";
|
|
4
|
+
import { ensureProjectVersion } from "./project-version.js";
|
|
4
5
|
import { dev } from "./dev.js";
|
|
5
6
|
import { findProject } from "./local.js";
|
|
6
7
|
import { hostPlatform } from "./platform.js";
|
|
@@ -59,6 +60,7 @@ export async function devCommand(args) {
|
|
|
59
60
|
return;
|
|
60
61
|
}
|
|
61
62
|
const root = findProject(start);
|
|
63
|
+
ensureProjectVersion(root);
|
|
62
64
|
const { desktop, initial } = devTargets(supportedPlatforms(root), options.platform ?? process.env.SPARK_PLATFORM);
|
|
63
65
|
// This selects only desktop build/signature state in the supervisor. Expo's
|
|
64
66
|
// child uses shared development config and chooses each JS graph per request.
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { existsSync, readFileSync, realpathSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { readJson, writeJson } from "./json.js";
|
|
5
|
+
import { readPnpmConfig } from "./package-manager-config.js";
|
|
6
|
+
import { packageManager, managerCommand } from "./package-manager.js";
|
|
7
|
+
import { run } from "./commands.js";
|
|
8
|
+
const defaults = JSON.parse(readFileSync(new URL("../templates/blank-typescript/package.json", import.meta.url), "utf8"));
|
|
9
|
+
export function enrichedHtmlPinPolicy(overrides) {
|
|
10
|
+
return Object.fromEntries(Object.entries(overrides).filter(([name]) => name === "dompurify" || name.startsWith("@tiptap/")));
|
|
11
|
+
}
|
|
12
|
+
export const enrichedHtmlPins = enrichedHtmlPinPolicy(defaults.overrides);
|
|
13
|
+
function semver() {
|
|
14
|
+
return createRequire(createRequire(import.meta.url).resolve("npm/package.json"))("semver");
|
|
15
|
+
}
|
|
16
|
+
// Read installed manifests even when exports hides package.json. Real paths retain
|
|
17
|
+
// the importer's dependency context in pnpm's isolated layout.
|
|
18
|
+
function manifestFrom(root, name) {
|
|
19
|
+
const req = createRequire(path.join(root, "package.json"));
|
|
20
|
+
const file = req.resolve.paths(name)?.map(base => path.join(base, name, "package.json")).find(existsSync);
|
|
21
|
+
return file ? realpathSync(file) : undefined;
|
|
22
|
+
}
|
|
23
|
+
export function enrichedHtmlPinIssues(root, policy = enrichedHtmlPins) {
|
|
24
|
+
const sdk = manifestFrom(root, "@legendapp/spark");
|
|
25
|
+
const html = [manifestFrom(root, "react-native-enriched-html"), sdk && manifestFrom(path.resolve(path.dirname(sdk), readJson(sdk).spark?.bundledModuleRoot ?? "."), "react-native-enriched-html")].filter((file) => !!file);
|
|
26
|
+
if (!html.length)
|
|
27
|
+
return [];
|
|
28
|
+
const { valid, lt } = semver();
|
|
29
|
+
const react = manifestFrom(root, "react");
|
|
30
|
+
if (!react)
|
|
31
|
+
throw new Error("Install the app's React dependency before checking enriched-html web dependencies.");
|
|
32
|
+
const expected = { ...policy, "react-dom": readJson(react).version };
|
|
33
|
+
const issues = [];
|
|
34
|
+
const queue = [...html], visited = new Set();
|
|
35
|
+
while (queue.length) {
|
|
36
|
+
const file = queue.shift();
|
|
37
|
+
if (visited.has(file))
|
|
38
|
+
continue;
|
|
39
|
+
visited.add(file);
|
|
40
|
+
const pkg = readJson(file);
|
|
41
|
+
if (pkg.name in expected) {
|
|
42
|
+
const version = expected[pkg.name];
|
|
43
|
+
if (!valid(pkg.version) || (pkg.name === "react-dom" ? pkg.version !== version : lt(pkg.version, version)))
|
|
44
|
+
issues.push({ package: pkg.name, found: pkg.version, expected: version, where: [file] });
|
|
45
|
+
}
|
|
46
|
+
for (const name of Object.keys({ ...pkg.dependencies, ...pkg.peerDependencies, ...pkg.optionalDependencies, "react-dom": "*" })) {
|
|
47
|
+
if (!(name in expected))
|
|
48
|
+
continue;
|
|
49
|
+
const dependency = manifestFrom(path.dirname(file), name);
|
|
50
|
+
if (dependency)
|
|
51
|
+
queue.push(dependency);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return issues;
|
|
55
|
+
}
|
|
56
|
+
/** App-owned solver policy; never edits enriched-html or its installed metadata. */
|
|
57
|
+
export function repairEnrichedHtmlPins(root, manager, dryRun = false, policy = enrichedHtmlPins) {
|
|
58
|
+
const issues = enrichedHtmlPinIssues(root, policy);
|
|
59
|
+
if (!issues.length)
|
|
60
|
+
return issues;
|
|
61
|
+
const { validRange, lt, minVersion } = semver();
|
|
62
|
+
function preservePin(spec, version, name) {
|
|
63
|
+
if (typeof spec !== "string" || !validRange(spec))
|
|
64
|
+
return false;
|
|
65
|
+
const minimum = minVersion(spec);
|
|
66
|
+
return !!minimum && !lt(minimum, version) && (name !== "react-dom" || spec === version || lt(version, minimum));
|
|
67
|
+
}
|
|
68
|
+
const pkg = readJson(path.join(root, "package.json"));
|
|
69
|
+
const yaml = manager === "pnpm" ? readPnpmConfig(root) : undefined;
|
|
70
|
+
const field = manager === "yarn" ? "resolutions" : "overrides";
|
|
71
|
+
const owner = manager === "pnpm" ? (pkg.pnpm ??= {}) : pkg;
|
|
72
|
+
const pins = owner[field] ??= {};
|
|
73
|
+
const expected = { ...policy, "react-dom": readJson(manifestFrom(root, "react")).version };
|
|
74
|
+
let changed = false, yamlChanged = false;
|
|
75
|
+
for (const [name, version] of Object.entries(expected)) {
|
|
76
|
+
if (!version)
|
|
77
|
+
continue;
|
|
78
|
+
// pnpm 11's YAML wins; mirror it for pnpm 10 without downgrading either pin.
|
|
79
|
+
const yamlPin = yaml?.getIn(["overrides", name]);
|
|
80
|
+
const direct = pkg.dependencies?.[name] ?? pkg.devDependencies?.[name];
|
|
81
|
+
const entry = pins[name];
|
|
82
|
+
const value = entry && typeof entry === "object" ? entry["."] : entry;
|
|
83
|
+
const current = typeof value === "string" && value.startsWith("$") ? pkg.dependencies?.[value.slice(1)] ?? pkg.devDependencies?.[value.slice(1)] : value;
|
|
84
|
+
const candidates = [yamlPin, current, direct].filter(spec => preservePin(spec, version, name));
|
|
85
|
+
const target = candidates.reduce((best, spec) => lt(minVersion(best), minVersion(spec)) ? spec : best, candidates[0] ?? version);
|
|
86
|
+
if (!preservePin(current, version, name)) {
|
|
87
|
+
if (entry && typeof entry === "object")
|
|
88
|
+
entry["."] = target;
|
|
89
|
+
else
|
|
90
|
+
pins[name] = target;
|
|
91
|
+
changed = true;
|
|
92
|
+
}
|
|
93
|
+
if (yaml && !preservePin(yamlPin, version, name)) {
|
|
94
|
+
yaml.setIn(["overrides", name], target);
|
|
95
|
+
yamlChanged = true;
|
|
96
|
+
}
|
|
97
|
+
// npm rejects overrides that differ from a direct dependency's spec.
|
|
98
|
+
for (const section of ["dependencies", "devDependencies"]) {
|
|
99
|
+
if (pkg[section]?.[name] && !preservePin(pkg[section][name], version, name)) {
|
|
100
|
+
pkg[section][name] = target;
|
|
101
|
+
changed = true;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (!dryRun) {
|
|
106
|
+
if (changed)
|
|
107
|
+
writeJson(path.join(root, "package.json"), pkg);
|
|
108
|
+
if (yamlChanged)
|
|
109
|
+
writeFileSync(path.join(root, "pnpm-workspace.yaml"), yaml.toString());
|
|
110
|
+
}
|
|
111
|
+
return issues;
|
|
112
|
+
}
|
|
113
|
+
export function assertEnrichedHtmlPins(root, policy = enrichedHtmlPins) {
|
|
114
|
+
const issues = enrichedHtmlPinIssues(root, policy);
|
|
115
|
+
if (!issues.length)
|
|
116
|
+
return;
|
|
117
|
+
const manager = packageManager(root);
|
|
118
|
+
repairEnrichedHtmlPins(root, manager, false, policy);
|
|
119
|
+
throw new Error(`Enriched HTML web dependency pins need installation (${[...new Set(issues.map(issue => `${issue.package}@${issue.found} → ${issue.expected}`))].join(", ")}). App-level pins checked and repaired; newer explicit pins preserved. Run ${manager} install, then retry. If versions still differ, align your explicit pins with the app's React and Spark's web dependency policy.`);
|
|
120
|
+
}
|
|
121
|
+
/** Existing install paths can also repair a newly introduced enriched-html graph. */
|
|
122
|
+
export async function installWithEnrichedHtmlPins(root, manager, policy = enrichedHtmlPins) {
|
|
123
|
+
repairEnrichedHtmlPins(root, manager, false, policy);
|
|
124
|
+
await run(root, managerCommand(manager, ["install"]));
|
|
125
|
+
if (repairEnrichedHtmlPins(root, manager, false, policy).length)
|
|
126
|
+
await run(root, managerCommand(manager, ["install"]));
|
|
127
|
+
assertEnrichedHtmlPins(root, policy);
|
|
128
|
+
}
|
|
@@ -27,7 +27,7 @@ function preparePatch(root) {
|
|
|
27
27
|
const manifest = expoRequire.resolve('@expo/cli/package.json');
|
|
28
28
|
const version = JSON.parse(fs.readFileSync(manifest, 'utf8')).version;
|
|
29
29
|
if (version !== VERSION) throw new Error(`spark desktop keys require @expo/cli ${VERSION}; found ${version}. Restore the supported version or update the spark Expo patch.`);
|
|
30
|
-
|
|
30
|
+
const result = new Map(patches.map(([relative, hash, before, after]) => {
|
|
31
31
|
const file = fs.realpathSync(path.join(path.dirname(manifest), 'build/src/start', relative));
|
|
32
32
|
const source = fs.readFileSync(file, 'utf8');
|
|
33
33
|
if (createHash('sha256').update(source).digest('hex') !== hash || source.split(before).length !== 2) {
|
|
@@ -35,6 +35,35 @@ function preparePatch(root) {
|
|
|
35
35
|
}
|
|
36
36
|
return [file, source.replace(before, after)];
|
|
37
37
|
}));
|
|
38
|
+
const graph = fs.realpathSync(createRequire(manifest).resolve('metro/private/DeltaBundler/Graph'));
|
|
39
|
+
const source = fs.readFileSync(graph, 'utf8');
|
|
40
|
+
const before = ' _freeModule(module, delta) {\n if (delta.added.has(module.path)) {';
|
|
41
|
+
const methods = ['traverseDependencies', 'initialTraverseDependencies'];
|
|
42
|
+
if (source.split(before).length !== 2 || methods.some(name => !source.includes(` async ${name}(`))) throw new Error('Unsupported Metro graph source. Update the Spark Metro patch before starting desktop development.');
|
|
43
|
+
// Metro snapshots released modules, but its cycle collector also frees modules
|
|
44
|
+
// that the same update may reimport. Retain their data before collecting them.
|
|
45
|
+
result.set(graph, source.replace(before, ` _freeModule(module, delta) {
|
|
46
|
+
if (!delta.updatedModuleData.has(module.path) && !delta.baseModuleData.has(module.path)) {
|
|
47
|
+
delta.baseModuleData.set(module.path, this._moduleSnapshot(module));
|
|
48
|
+
}
|
|
49
|
+
if (delta.added.has(module.path)) {`) + `
|
|
50
|
+
// The HMR client keeps removed modules. Re-adding a collected native spec would
|
|
51
|
+
// hot-evaluate its view registration again. Promote specs only after the entire
|
|
52
|
+
// graph transaction succeeds, so failed updates can roll back their additions.
|
|
53
|
+
for (const name of ${JSON.stringify(methods)}) {
|
|
54
|
+
const traverse = exports.Graph.prototype[name];
|
|
55
|
+
exports.Graph.prototype[name] = async function(...args) {
|
|
56
|
+
const delta = await traverse.apply(this, args);
|
|
57
|
+
if (this.transformOptions.dev) {
|
|
58
|
+
for (const path of delta.added.keys()) {
|
|
59
|
+
if (/NativeComponent\\.[jt]sx?$/.test(path)) this.entryPoints.add(path);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return delta;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
`);
|
|
66
|
+
return result;
|
|
38
67
|
}
|
|
39
68
|
|
|
40
69
|
module.exports = { VERSION, preparePatch };
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { enrichedHtmlPinIssues } from "./enriched-html-pins.js";
|
|
2
|
+
import { publishCustomize } from "./customize-artifacts.js";
|
|
1
3
|
import { nodeCommand, spawnProcess } from "./process.js";
|
|
2
4
|
import { examples } from "./examples.js";
|
|
3
5
|
import { prepareGoProfile } from "./go-profile.js";
|
|
@@ -21,6 +23,8 @@ import { findFramework, findProject, sparkHome, creationManifest, packageManifes
|
|
|
21
23
|
import { readJson, stateFile, VERSION, writeJson } from "./project.js";
|
|
22
24
|
import { devCommand } from "./dev-command.js";
|
|
23
25
|
import { printDependencyPins, upgradeDependencyPins } from "./dependency-pins.js";
|
|
26
|
+
import { upgrade } from "./upgrade.js";
|
|
27
|
+
import { ensureProjectVersion } from "./project-version.js";
|
|
24
28
|
import { reportError } from "./errors.js";
|
|
25
29
|
async function main() {
|
|
26
30
|
const argv = process.argv.slice(2);
|
|
@@ -42,6 +46,7 @@ async function main() {
|
|
|
42
46
|
throw new Error(`Use create --example ${examples.join(" | ")}`);
|
|
43
47
|
if (!values.help && ["build", "prebuild"].includes(command ?? "")) {
|
|
44
48
|
const root = project();
|
|
49
|
+
ensureProjectVersion(root);
|
|
45
50
|
const selected = readConfig(root).expo.platforms[0];
|
|
46
51
|
if (!["ios", "android", "web"].includes(selected) && (values.device || values.port))
|
|
47
52
|
throw new Error("--device and --port are mobile build options; use spark dev for desktop launch options.");
|
|
@@ -81,7 +86,8 @@ async function main() {
|
|
|
81
86
|
spark prebuild Generate a Windows/mobile native project
|
|
82
87
|
spark package Sign and notarize a distribution archive
|
|
83
88
|
spark doctor Check app dependency pins and native tooling (exits 1 on issues)
|
|
84
|
-
spark upgrade
|
|
89
|
+
spark upgrade Update Spark on its current release channel (--dry-run previews changes)
|
|
90
|
+
spark customize publish Upload prepared customization artifacts to existing GitHub releases
|
|
85
91
|
|
|
86
92
|
Inside an app: npm run dev, npm run build, npm run package (or pnpm, yarn, bun)
|
|
87
93
|
|
|
@@ -185,8 +191,9 @@ Overrides: --project <directory>, --port <number>, dev --runner-binary <runtime
|
|
|
185
191
|
console.log("No Spark app found; skipping dependency pin check.");
|
|
186
192
|
}
|
|
187
193
|
if (root) {
|
|
194
|
+
ensureProjectVersion(root);
|
|
188
195
|
try {
|
|
189
|
-
const issues = upgradeDependencyPins(root, true);
|
|
196
|
+
const issues = [...upgradeDependencyPins(root, true), ...enrichedHtmlPinIssues(root)];
|
|
190
197
|
printDependencyPins(issues);
|
|
191
198
|
if (issues.length) {
|
|
192
199
|
process.exitCode = 1;
|
|
@@ -206,11 +213,13 @@ Overrides: --project <directory>, --port <number>, dev --runner-binary <runtime
|
|
|
206
213
|
break;
|
|
207
214
|
}
|
|
208
215
|
case "upgrade": {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
216
|
+
await upgrade(project(), !!values["dry-run"]);
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
case "customize": {
|
|
220
|
+
if (positionals[1] !== "publish")
|
|
221
|
+
throw new Error("Usage: spark customize publish (after a customize release build)");
|
|
222
|
+
await publishCustomize(project());
|
|
214
223
|
break;
|
|
215
224
|
}
|
|
216
225
|
case "updates": {
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function initializeTemplate(root: string): void;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
const { spawn, spawnSync } = require('node:child_process');
|
|
2
|
+
const { createHash } = require('node:crypto');
|
|
3
|
+
const { existsSync, mkdirSync, symlinkSync, linkSync, copyFileSync, realpathSync } = require('node:fs');
|
|
4
|
+
const os = require('node:os');
|
|
5
|
+
const path = require('node:path');
|
|
6
|
+
|
|
7
|
+
const minimum = { node: [24, 19, 0], bun: [1, 3, 14] };
|
|
8
|
+
function supported(versions) {
|
|
9
|
+
const name = versions.bun ? 'bun' : 'node';
|
|
10
|
+
const version = versions[name];
|
|
11
|
+
const parts = /^([0-9]+)\.([0-9]+)\.([0-9]+)$/.exec(version ?? '')?.slice(1).map(Number);
|
|
12
|
+
if (!parts) return false;
|
|
13
|
+
for (let i = 0; i < 3; i++) {
|
|
14
|
+
if (parts[i] !== minimum[name][i]) return parts[i] > minimum[name][i];
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function candidates(command, env) {
|
|
20
|
+
if (path.isAbsolute(command) || command.includes('/') || command.includes('\\')) return [path.resolve(command)];
|
|
21
|
+
return (env.PATH ?? '').split(path.delimiter).filter(Boolean).map(dir => path.join(dir, command + (process.platform === 'win32' ? '.exe' : '')));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function selectRuntime(env = process.env, current = process.execPath) {
|
|
25
|
+
const requested = env.SPARK_JS_RUNTIME;
|
|
26
|
+
const choices = requested ? candidates(requested, env) : [...candidates('bun', env), current, ...candidates('node', env)];
|
|
27
|
+
for (const executable of [...new Set(choices)]) {
|
|
28
|
+
if (!existsSync(executable)) continue;
|
|
29
|
+
try {
|
|
30
|
+
const resolved = realpathSync(executable);
|
|
31
|
+
const result = resolved === realpathSync(process.execPath) ? undefined : spawnSync(executable, ['-p', 'JSON.stringify({node:process.versions.node,bun:process.versions.bun})'], { encoding: 'utf8', timeout: 5000, env });
|
|
32
|
+
if (result && result.status !== 0) continue;
|
|
33
|
+
const versions = result ? JSON.parse(result.stdout) : process.versions;
|
|
34
|
+
const name = versions.bun ? 'bun' : 'node';
|
|
35
|
+
if ((requested === 'node' || requested === 'bun') && requested !== name) continue;
|
|
36
|
+
if (supported(versions)) return { executable: resolved, name, version: versions.bun ?? versions.node };
|
|
37
|
+
} catch {}
|
|
38
|
+
}
|
|
39
|
+
throw new Error(`${requested ? `Requested JavaScript runtime ${requested} is unavailable or unsupported.` : 'No compatible JavaScript runtime found.'} Spark needs Node 24.19.0+ or Bun 1.3.14+. Use the SDK bootstrap to download one.`);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function runtimeEnvironment(runtime, env = process.env) {
|
|
43
|
+
const directories = [path.dirname(runtime.executable)];
|
|
44
|
+
if (runtime.name === 'bun') {
|
|
45
|
+
// Upstream scripts and native build phases still invoke "node". Scope its
|
|
46
|
+
// compatibility alias to Spark's children, without changing the user's PATH.
|
|
47
|
+
const key = createHash('sha256').update(runtime.executable + runtime.version).digest('hex').slice(0, 16);
|
|
48
|
+
const bin = path.join(env.SPARK_HOME ?? path.join(os.homedir(), '.spark'), 'tooling', 'shims', key);
|
|
49
|
+
const alias = path.join(bin, process.platform === 'win32' ? 'node.exe' : 'node');
|
|
50
|
+
mkdirSync(bin, { recursive: true });
|
|
51
|
+
if (!existsSync(alias)) {
|
|
52
|
+
try {
|
|
53
|
+
if (process.platform === 'win32') linkSync(runtime.executable, alias);
|
|
54
|
+
else symlinkSync(runtime.executable, alias);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
if (error.code === 'EXDEV' && process.platform === 'win32') copyFileSync(runtime.executable, alias);
|
|
57
|
+
else if (error.code !== 'EEXIST') throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
directories.unshift(bin);
|
|
61
|
+
}
|
|
62
|
+
const rest = (env.PATH ?? '').split(path.delimiter).filter(directory => !directories.includes(directory));
|
|
63
|
+
const inherited = { ...env };
|
|
64
|
+
if (process.platform === 'win32') for (const key of Object.keys(inherited)) {
|
|
65
|
+
if (['PATH', 'SPARK_JS_RUNTIME', 'NODE_BINARY'].includes(key.toUpperCase())) delete inherited[key];
|
|
66
|
+
}
|
|
67
|
+
return { ...inherited, SPARK_JS_RUNTIME: runtime.executable, NODE_BINARY: runtime.executable, PATH: [...directories, ...rest].join(path.delimiter) };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function initializeRuntime() {
|
|
71
|
+
const runtime = selectRuntime();
|
|
72
|
+
const env = runtimeEnvironment(runtime);
|
|
73
|
+
// Bun 1.3.14 resolves spawnSync commands without an explicit env using its
|
|
74
|
+
// startup PATH. Relaunch once so upstream scripts see the node alias too.
|
|
75
|
+
if (runtime.executable !== realpathSync(process.execPath) || (runtime.name === 'bun' && env.PATH !== process.env.PATH)) {
|
|
76
|
+
const child = spawn(runtime.executable, process.argv.slice(1), { env, stdio: 'inherit' });
|
|
77
|
+
process.on('SIGINT', () => child.kill('SIGINT'));
|
|
78
|
+
process.on('SIGTERM', () => child.kill('SIGTERM'));
|
|
79
|
+
const code = await new Promise((resolve, reject) => {
|
|
80
|
+
child.once('error', reject);
|
|
81
|
+
child.once('exit', (code, signal) => resolve(code ?? 128 + (os.constants.signals[signal] ?? 1)));
|
|
82
|
+
});
|
|
83
|
+
process.exit(code);
|
|
84
|
+
}
|
|
85
|
+
Object.assign(process.env, env);
|
|
86
|
+
return runtime;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
module.exports = { minimum, supported, selectRuntime, runtimeEnvironment, initializeRuntime };
|
|
90
|
+
|
|
91
|
+
// Shell/PowerShell can probe an engine without quoting inline JavaScript.
|
|
92
|
+
if (require.main === module) {
|
|
93
|
+
const requested = process.argv[2];
|
|
94
|
+
const name = process.versions.bun ? 'bun' : 'node';
|
|
95
|
+
process.exit(supported(process.versions) && (!requested || requested === name) ? 0 : 1);
|
|
96
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
const fs = require('node:fs');
|
|
3
|
+
const path = require('node:path');
|
|
4
|
+
const ts = require('typescript');
|
|
5
|
+
const upgradeNotesPath = 'node_modules/@legendapp/spark/skills/spark/UPGRADING.md';
|
|
6
|
+
|
|
7
|
+
/** @type {typeof import('./migration-helpers.cjs').createMigrationContext} */
|
|
8
|
+
function createMigrationContext(root, version, changes, manualSteps) {
|
|
9
|
+
root = fs.realpathSync(root);
|
|
10
|
+
/** @param {string} file */
|
|
11
|
+
function location(file) {
|
|
12
|
+
const absolute = path.resolve(root, file);
|
|
13
|
+
if (absolute === root || !absolute.startsWith(root + path.sep)) throw new Error(`Migration path must stay in the app: ${file}`);
|
|
14
|
+
// Never follow app symlinks into another workspace or installed dependency.
|
|
15
|
+
for (let current = absolute; current !== root; current = path.dirname(current)) {
|
|
16
|
+
if (fs.existsSync(current) && fs.lstatSync(current).isSymbolicLink()) throw new Error(`Migration path is a symlink: ${file}`);
|
|
17
|
+
}
|
|
18
|
+
return { absolute, relative: path.relative(root, absolute).split(path.sep).join('/') };
|
|
19
|
+
}
|
|
20
|
+
/** @type {import('./migration-helpers.cjs').MigrationContext} */
|
|
21
|
+
const context = {
|
|
22
|
+
readFile(file) {
|
|
23
|
+
const { absolute, relative } = location(file);
|
|
24
|
+
const change = changes.get(relative);
|
|
25
|
+
if (change) return change.after;
|
|
26
|
+
return fs.existsSync(absolute) ? fs.readFileSync(absolute, 'utf8') : null;
|
|
27
|
+
},
|
|
28
|
+
writeFile(file, after) {
|
|
29
|
+
const { relative } = location(file);
|
|
30
|
+
const change = changes.get(relative);
|
|
31
|
+
const before = change ? change.before : context.readFile(file);
|
|
32
|
+
changes.set(relative, { file: relative, before, after });
|
|
33
|
+
},
|
|
34
|
+
editJson(file, edit) {
|
|
35
|
+
const before = context.readFile(file);
|
|
36
|
+
if (before === null) return;
|
|
37
|
+
try {
|
|
38
|
+
const value = JSON.parse(before);
|
|
39
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
40
|
+
context.manualStep(`Edit ${file} manually; expected a JSON object.`);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const original = JSON.stringify(value);
|
|
44
|
+
edit(value);
|
|
45
|
+
if (JSON.stringify(value) !== original) {
|
|
46
|
+
const indent = before.match(/\n([\t ]+)\S/)?.[1] ?? ' ';
|
|
47
|
+
const newline = before.includes('\r\n') ? '\r\n' : '\n';
|
|
48
|
+
context.writeFile(file, JSON.stringify(value, null, indent).replaceAll('\n', newline) + (before.endsWith('\n') ? newline : ''));
|
|
49
|
+
}
|
|
50
|
+
} catch (error) {
|
|
51
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
52
|
+
context.manualStep(`Edit ${file} manually; it is not valid JSON: ${error.message}`);
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
renameFile(from, to) {
|
|
56
|
+
const content = context.readFile(from);
|
|
57
|
+
if (content === null) return;
|
|
58
|
+
if (context.readFile(to) !== null) {
|
|
59
|
+
context.manualStep(`Move ${from} to ${to}; the destination already exists. Preserve both files.`);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
context.writeFile(to, content);
|
|
63
|
+
const { relative } = location(from);
|
|
64
|
+
const change = changes.get(relative);
|
|
65
|
+
const before = change ? change.before : content;
|
|
66
|
+
changes.set(relative, { file: relative, before, after: null });
|
|
67
|
+
},
|
|
68
|
+
updateTemplate(file, previous, next) {
|
|
69
|
+
const content = context.readFile(file);
|
|
70
|
+
if (content === null || content === previous) context.writeFile(file, next);
|
|
71
|
+
else if (content !== next) context.manualStep(`Merge the new template for ${file}; the app has customized it.`);
|
|
72
|
+
},
|
|
73
|
+
manualStep(message) {
|
|
74
|
+
const notes = `${upgradeNotesPath}#${version.replaceAll('.', '')}`;
|
|
75
|
+
if (!manualSteps.some(step => step.version === version && step.message === message)) manualSteps.push({ version, message, notes });
|
|
76
|
+
},
|
|
77
|
+
replaceImport(from, to, names = {}) {
|
|
78
|
+
const files = new Set();
|
|
79
|
+
/** @param {string} directory */
|
|
80
|
+
function visit(directory) {
|
|
81
|
+
for (const entry of fs.readdirSync(path.join(root, directory), { withFileTypes: true })) {
|
|
82
|
+
const file = path.posix.join(directory, entry.name);
|
|
83
|
+
if (entry.isDirectory() && !['node_modules', '.git', '.spark', 'spark-packages', 'build', 'dist', 'Pods'].includes(entry.name)) visit(file);
|
|
84
|
+
else if (entry.isFile() && /\.[cm]?[jt]sx?$/.test(file)) files.add(file);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
visit('');
|
|
88
|
+
for (const file of changes.keys()) if (/\.[cm]?[jt]sx?$/.test(file)) files.add(file);
|
|
89
|
+
for (const file of files) {
|
|
90
|
+
const content = context.readFile(file);
|
|
91
|
+
if (content === null || !content.includes(from)) continue;
|
|
92
|
+
const source = ts.createSourceFile(file, content, ts.ScriptTarget.Latest, true);
|
|
93
|
+
if (/** @type {ts.SourceFile & { parseDiagnostics: readonly ts.Diagnostic[] }} */ (source).parseDiagnostics.length) {
|
|
94
|
+
context.manualStep(`Update imports from ${from} in ${file}; the source could not be parsed.`);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
/** @type {{ start: number; end: number; text: string }[]} */
|
|
98
|
+
const edits = [];
|
|
99
|
+
// A locally bound require is not necessarily the CommonJS loader.
|
|
100
|
+
let shadowedRequire = false;
|
|
101
|
+
/** @param {ts.Node} node */
|
|
102
|
+
function findRequireBinding(node) {
|
|
103
|
+
if ((ts.isVariableDeclaration(node) || ts.isParameter(node) || ts.isBindingElement(node) || ts.isFunctionDeclaration(node) || ts.isImportSpecifier(node)) && node.name && ts.isIdentifier(node.name) && node.name.text === 'require') shadowedRequire = true;
|
|
104
|
+
ts.forEachChild(node, findRequireBinding);
|
|
105
|
+
}
|
|
106
|
+
findRequireBinding(source);
|
|
107
|
+
/** @param {ts.Node} node */
|
|
108
|
+
function inspect(node) {
|
|
109
|
+
if (ts.isStringLiteralLike(node) && node.text === from) {
|
|
110
|
+
const parent = node.parent;
|
|
111
|
+
if ((ts.isImportDeclaration(parent) || ts.isExportDeclaration(parent)) && parent.moduleSpecifier === node) {
|
|
112
|
+
const bindings = ts.isImportDeclaration(parent) ? parent.importClause?.namedBindings : parent.exportClause;
|
|
113
|
+
if (Object.keys(names).length && (!bindings || !(ts.isNamedImports(bindings) || ts.isNamedExports(bindings)) || (ts.isImportDeclaration(parent) && parent.importClause?.name))) {
|
|
114
|
+
context.manualStep(`Update renamed APIs from ${from} in ${file}; namespace/default imports need review.`);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (bindings && (ts.isNamedImports(bindings) || ts.isNamedExports(bindings))) {
|
|
118
|
+
for (const specifier of bindings.elements) {
|
|
119
|
+
const imported = specifier.propertyName ?? specifier.name;
|
|
120
|
+
const replacement = names[imported.text];
|
|
121
|
+
if (replacement && replacement !== imported.text) edits.push({ start: imported.getStart(source), end: imported.end, text: specifier.propertyName ? replacement : `${replacement} as ${specifier.name.text}` });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
} else if (ts.isCallExpression(parent) && parent.arguments[0] === node && parent.arguments.length === 1 &&
|
|
125
|
+
(parent.expression.kind === ts.SyntaxKind.ImportKeyword || (ts.isIdentifier(parent.expression) && parent.expression.text === 'require'))) {
|
|
126
|
+
if (shadowedRequire && ts.isIdentifier(parent.expression)) {
|
|
127
|
+
context.manualStep(`Review ${from} in ${file}; require is locally bound.`);
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (Object.keys(names).length) {
|
|
131
|
+
context.manualStep(`Update renamed APIs from ${from} in ${file}; require/dynamic imports need review.`);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
context.manualStep(`Review the reference to ${from} in ${file}; it is not a supported import.`);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
edits.push({ start: node.getStart(source) + 1, end: node.end - 1, text: to });
|
|
139
|
+
}
|
|
140
|
+
ts.forEachChild(node, inspect);
|
|
141
|
+
}
|
|
142
|
+
inspect(source);
|
|
143
|
+
let after = content;
|
|
144
|
+
for (const edit of edits.sort((a, b) => b.start - a.start)) after = after.slice(0, edit.start) + edit.text + after.slice(edit.end);
|
|
145
|
+
if (after !== content) context.writeFile(file, after);
|
|
146
|
+
}
|
|
147
|
+
},
|
|
148
|
+
};
|
|
149
|
+
return context;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
module.exports = { createMigrationContext, upgradeNotesPath };
|