@legendapp/spark 0.0.1-next.3 → 0.0.1-next.5
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/camera.ts +13 -0
- package/customize.ts +2 -0
- package/html.ts +1 -0
- package/markdown.ts +1 -0
- package/package.json +34 -15
- package/patches/camera/react-native-nitro-image.patch +320 -0
- package/patches/camera/react-native-nitro-modules.patch +50 -0
- package/patches/camera/react-native-vision-camera.patch +2662 -0
- package/patches/camera/upstream.json +22 -0
- package/patches/dependencies.json +14 -0
- package/patches/react-native-enriched-html@1.1.1.patch +854 -0
- package/patches/react-native-enriched-markdown@1.1.1.patch +9261 -0
- package/postinstall.cjs +1 -0
- package/schema.json +104 -0
- package/skills/spark/SKILL.md +56 -0
- package/skills/spark/UPGRADING.md +246 -0
- package/ui-swipe.ts +1 -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 +65 -11
- package/vendor/node_modules/@legendapp/spark-cli/dist/cli-arguments.js +2 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/commands.js +3 -2
- package/vendor/node_modules/@legendapp/spark-cli/dist/create.js +36 -13
- package/vendor/node_modules/@legendapp/spark-cli/dist/credentials.js +41 -26
- 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 +157 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/dev-command.js +3 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/dev.js +1 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/enriched-html-pins.js +128 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/errors.js +100 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/examples.js +1 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/expo-dev-patch.cjs +30 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/expo-native.cjs +5 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/helpers.js +1 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/index.js +52 -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/json.js +16 -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 +5 -1
- 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/process.js +7 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/project-version.js +12 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/project.js +66 -30
- package/vendor/node_modules/@legendapp/spark-cli/dist/release.js +2 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/release.json +20 -20
- 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/universal.cjs +2 -1
- package/vendor/node_modules/@legendapp/spark-cli/dist/upgrade.js +102 -0
- package/vendor/node_modules/@legendapp/spark-cli/dist/windows.js +1 -8
- 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 +47 -3
- 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 +46 -3
- 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 +47 -3
- package/vendor/node_modules/@legendapp/spark-clipboard/package.json +3 -3
- package/vendor/node_modules/@legendapp/spark-codex/package.json +4 -4
- 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/contracts/menu.ts +6 -4
- 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 +5 -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/expo.cjs +1 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/native-patches.cjs +106 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/native-patches.d.cts +1 -0
- package/vendor/node_modules/@legendapp/spark-desktop-config/package.json +5 -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 +44 -4
- package/vendor/node_modules/@legendapp/spark-desktop-windows/package.json +4 -4
- package/vendor/node_modules/@legendapp/spark-desktop-windows/src/api.ts +65 -26
- package/vendor/node_modules/@legendapp/spark-desktop-windows/src/macos-adapter.ts +8 -6
- 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-notifications/windows/SparkNotifications/SparkNotifications.h +16 -11
- 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 +360 -35
- package/vendor/node_modules/@legendapp/spark-ui/macos/swipe-actions/RNSwipeActions.h +10 -0
- package/vendor/node_modules/@legendapp/spark-ui/macos/swipe-actions/RNSwipeActions.mm +422 -0
- package/vendor/node_modules/@legendapp/spark-ui/package.json +4 -3
- package/vendor/node_modules/@legendapp/spark-ui/src/appkit-split-view/SidebarSplitViewNativeComponent.ts +11 -0
- package/vendor/node_modules/@legendapp/spark-ui/src/appkit-split-view/index.tsx +30 -13
- package/vendor/node_modules/@legendapp/spark-ui/src/swipe-actions/SwipeActionsNativeComponent.ts +11 -0
- package/vendor/node_modules/@legendapp/spark-ui/src/swipe-actions/index.tsx +41 -0
- 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
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
// Command failures retain diagnostics without making callers parse their message.
|
|
4
|
+
export class CommandError extends Error {
|
|
5
|
+
argv;
|
|
6
|
+
code;
|
|
7
|
+
output;
|
|
8
|
+
log;
|
|
9
|
+
constructor(argv, code, output, log) {
|
|
10
|
+
super(`${argv.join(" ")} exited ${code}${log ? `. See ${log}` : ""}${output ? `\n${output.slice(-1800)}` : ""}`);
|
|
11
|
+
this.argv = argv;
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.output = output;
|
|
14
|
+
this.log = log;
|
|
15
|
+
this.name = "CommandError";
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
export function isNetworkError(error) {
|
|
19
|
+
const message = error instanceof CommandError ? error.output : error instanceof Error ? error.message : String(error);
|
|
20
|
+
return /NSURLErrorDomain Code=-(?:1001|1005|1009)|internet connection appears to be offline|\b(?:ENOTFOUND|EAI_AGAIN|ETIMEDOUT|ECONNRESET|ENETUNREACH)\b|could not resolve host|failed to connect|network is unreachable/i.test(message);
|
|
21
|
+
}
|
|
22
|
+
export function errorPresentation(error) {
|
|
23
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
24
|
+
const command = error instanceof CommandError ? error.argv.join(" ") : "";
|
|
25
|
+
const details = error instanceof CommandError ? error.output : message;
|
|
26
|
+
let summary = message.split("\n")[0] || "An unexpected error occurred.";
|
|
27
|
+
let next;
|
|
28
|
+
if (isNetworkError(error)) {
|
|
29
|
+
summary = command.includes("notarytool") ? "Could not reach Apple’s notarization service." : "Could not reach the remote service.";
|
|
30
|
+
next = "Check your internet connection or VPN, then retry the same command.";
|
|
31
|
+
}
|
|
32
|
+
else if ((/\b(?:E401|ENEEDAUTH)\b|401 Unauthorized/i.test(details) && /npm/.test(command || message)) || command === "npm whoami") {
|
|
33
|
+
summary = "npm authentication failed.";
|
|
34
|
+
next = "Run npm login, then retry the same release command.";
|
|
35
|
+
}
|
|
36
|
+
else if (command === "gh auth status" || /gh auth login/.test(details)) {
|
|
37
|
+
summary = "GitHub authentication failed.";
|
|
38
|
+
next = "Run gh auth login, then retry the same command.";
|
|
39
|
+
}
|
|
40
|
+
else if (/No Keychain password item found for profile:/.test(details)) {
|
|
41
|
+
summary = "The selected notarization Keychain profile was not found.";
|
|
42
|
+
next = "Run spark credentials to select an existing profile or explicitly create one.";
|
|
43
|
+
}
|
|
44
|
+
else if (/notarytool/.test(command) && /\b401\b|invalid credentials|authentication/i.test(details)) {
|
|
45
|
+
summary = "Apple rejected the notarization credentials.";
|
|
46
|
+
next = "Run spark credentials to select or explicitly create a valid Keychain profile.";
|
|
47
|
+
}
|
|
48
|
+
else if (error instanceof CommandError) {
|
|
49
|
+
const checks = { "bun run test": "Tests failed.", "bun run typecheck": "Type checking failed.", "bun install --frozen-lockfile": "Dependency installation failed." };
|
|
50
|
+
summary = checks[command] ?? `${command} failed (exit ${error.code}).`;
|
|
51
|
+
next = "Review the diagnostic log or output above, fix the problem, then retry the same command.";
|
|
52
|
+
}
|
|
53
|
+
return { summary, next };
|
|
54
|
+
}
|
|
55
|
+
function diagnostics(error, seen = new Set()) {
|
|
56
|
+
let result = String(error);
|
|
57
|
+
if (error instanceof Error && !seen.has(error)) {
|
|
58
|
+
seen.add(error);
|
|
59
|
+
result = error.stack ?? error.message;
|
|
60
|
+
if (error instanceof CommandError && error.output)
|
|
61
|
+
result += `\n\nCommand output:\n${error.output}`;
|
|
62
|
+
if (error.cause)
|
|
63
|
+
result += `\n\nCaused by:\n${diagnostics(error.cause, seen)}`;
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
export function reportError(root, error, label = "Release") {
|
|
68
|
+
const { summary, next } = errorPresentation(error);
|
|
69
|
+
console.error(`\n${label} stopped: ${summary}`);
|
|
70
|
+
if (next)
|
|
71
|
+
console.error(`Next: ${next}`);
|
|
72
|
+
let log;
|
|
73
|
+
try {
|
|
74
|
+
const folder = path.join(root, ".spark/logs");
|
|
75
|
+
mkdirSync(folder, { recursive: true });
|
|
76
|
+
const destination = path.join(folder, `${Date.now()}-${crypto.randomUUID()}-error.log`);
|
|
77
|
+
writeFileSync(destination, diagnostics(error));
|
|
78
|
+
log = destination;
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// Reporting must still work when the original failure was a full disk.
|
|
82
|
+
if (error instanceof CommandError)
|
|
83
|
+
log = error.log;
|
|
84
|
+
}
|
|
85
|
+
if (log)
|
|
86
|
+
console.error(`Details: ${log}`);
|
|
87
|
+
else
|
|
88
|
+
console.error("Could not save diagnostics. Set SPARK_DEBUG=1 to print them.");
|
|
89
|
+
if (process.env.SPARK_DEBUG === "1")
|
|
90
|
+
console.error(diagnostics(error));
|
|
91
|
+
}
|
|
92
|
+
export async function releaseCommand(root, action) {
|
|
93
|
+
try {
|
|
94
|
+
await action();
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
reportError(root, error);
|
|
98
|
+
process.exitCode = 1;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { packageManager, managerCommand } from "./package-manager.js";
|
|
2
2
|
import { cpSync, writeFileSync } from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
|
-
import { readJson, writeJson } from "./
|
|
4
|
+
import { readJson, writeJson } from "./json.js";
|
|
5
5
|
import { run } from "./commands.js";
|
|
6
6
|
export const examples = ["document-editor", "notes-lite", "music-lite", "diff-lite"];
|
|
7
7
|
export async function configureExample(root, example) {
|
|
@@ -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,5 +1,9 @@
|
|
|
1
1
|
exports.withSparkNative = (original, root) => {
|
|
2
|
-
if (!['macos', 'windows'].includes(process.env.SPARK_PLATFORM))
|
|
2
|
+
if (!['macos', 'windows'].includes(process.env.SPARK_PLATFORM)) {
|
|
3
|
+
const patched = require('@legendapp/spark-desktop-config/native-patches.cjs').prepareNativePatches(root);
|
|
4
|
+
if (!Object.keys(patched).length) return original;
|
|
5
|
+
return { ...original, dependencies: { ...patched, ...original.dependencies } };
|
|
6
|
+
}
|
|
3
7
|
const generated = require('./universal.cjs').nativeConfig(root);
|
|
4
8
|
const dependencies = { ...original.dependencies };
|
|
5
9
|
for (const [name, config] of Object.entries(generated.dependencies ?? {})) {
|
|
@@ -2,8 +2,8 @@ import { copyFileSync, lstatSync, mkdirSync, realpathSync, chmodSync, readdirSyn
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { createRequire } from "node:module";
|
|
4
4
|
import { architecture } from "./platform.js";
|
|
5
|
-
const { validateHelpers } = createRequire(import.meta.url)("@legendapp/spark-desktop-config/helpers.cjs");
|
|
6
5
|
export function resolveHelpers(root, helpers = {}, platform = "macos", arch = architecture(platform)) {
|
|
6
|
+
const { validateHelpers } = createRequire(import.meta.url)("@legendapp/spark-desktop-config/helpers.cjs");
|
|
7
7
|
validateHelpers(helpers);
|
|
8
8
|
const base = realpathSync(root);
|
|
9
9
|
function inspect(file) {
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { enrichedHtmlPinIssues } from "./enriched-html-pins.js";
|
|
2
|
+
import { publishCustomize } from "./customize-artifacts.js";
|
|
3
|
+
import { nodeCommand, spawnProcess } from "./process.js";
|
|
2
4
|
import { examples } from "./examples.js";
|
|
3
5
|
import { prepareGoProfile } from "./go-profile.js";
|
|
4
6
|
import { exportSDK, importSDK } from "./sdk-transfer.js";
|
|
@@ -7,7 +9,7 @@ import path from "node:path";
|
|
|
7
9
|
import { cliArguments } from "./cli-arguments.js";
|
|
8
10
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
9
11
|
import { prepareConfig, readConfig, isExpoProject } from "@legendapp/spark-desktop-config/config.cjs";
|
|
10
|
-
import {
|
|
12
|
+
import { prepareWindows } from "./windows.js";
|
|
11
13
|
import { create, refreshLocalPackages } from "./create.js";
|
|
12
14
|
import { addDesktop } from "./add-desktop.js";
|
|
13
15
|
import { buildMode } from "./build-mode.js";
|
|
@@ -20,6 +22,10 @@ import { doctor, run } from "./commands.js";
|
|
|
20
22
|
import { findFramework, findProject, sparkHome, creationManifest, packageManifest, registerRuntime } from "./local.js";
|
|
21
23
|
import { readJson, stateFile, VERSION, writeJson } from "./project.js";
|
|
22
24
|
import { devCommand } from "./dev-command.js";
|
|
25
|
+
import { printDependencyPins, upgradeDependencyPins } from "./dependency-pins.js";
|
|
26
|
+
import { upgrade } from "./upgrade.js";
|
|
27
|
+
import { ensureProjectVersion } from "./project-version.js";
|
|
28
|
+
import { reportError } from "./errors.js";
|
|
23
29
|
async function main() {
|
|
24
30
|
const argv = process.argv.slice(2);
|
|
25
31
|
if (argv[0] === "dev")
|
|
@@ -40,6 +46,7 @@ async function main() {
|
|
|
40
46
|
throw new Error(`Use create --example ${examples.join(" | ")}`);
|
|
41
47
|
if (!values.help && ["build", "prebuild"].includes(command ?? "")) {
|
|
42
48
|
const root = project();
|
|
49
|
+
ensureProjectVersion(root);
|
|
43
50
|
const selected = readConfig(root).expo.platforms[0];
|
|
44
51
|
if (!["ios", "android", "web"].includes(selected) && (values.device || values.port))
|
|
45
52
|
throw new Error("--device and --port are mobile build options; use spark dev for desktop launch options.");
|
|
@@ -78,6 +85,9 @@ async function main() {
|
|
|
78
85
|
spark build Build a standalone desktop app
|
|
79
86
|
spark prebuild Generate a Windows/mobile native project
|
|
80
87
|
spark package Sign and notarize a distribution archive
|
|
88
|
+
spark doctor Check app dependency pins and native tooling (exits 1 on issues)
|
|
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
|
|
81
91
|
|
|
82
92
|
Inside an app: npm run dev, npm run build, npm run package (or pnpm, yarn, bun)
|
|
83
93
|
|
|
@@ -99,8 +109,6 @@ Overrides: --project <directory>, --port <number>, dev --runner-binary <runtime
|
|
|
99
109
|
case "create": {
|
|
100
110
|
if (!positionals[1])
|
|
101
111
|
throw new Error("Usage: spark create MyApp");
|
|
102
|
-
if (!(values.universal || values.example) && !["macos", "windows"].includes(platform))
|
|
103
|
-
throw new Error("Use create --universal for mobile/web targets");
|
|
104
112
|
await create(path.resolve(positionals[1]), creationManifest(values.packages), platform, !!values.universal || !!values.example, values.example, values["package-manager"]);
|
|
105
113
|
break;
|
|
106
114
|
}
|
|
@@ -173,10 +181,47 @@ Overrides: --project <directory>, --port <number>, dev --runner-binary <runtime
|
|
|
173
181
|
throw new Error("For macOS, spark build --dev generates and builds the native project.");
|
|
174
182
|
await prepareWindows(project(), "dev");
|
|
175
183
|
break;
|
|
176
|
-
case "doctor":
|
|
177
|
-
|
|
184
|
+
case "doctor": {
|
|
185
|
+
let root;
|
|
186
|
+
let pinIssues = false;
|
|
187
|
+
try {
|
|
188
|
+
root = project();
|
|
189
|
+
}
|
|
190
|
+
catch {
|
|
191
|
+
console.log("No Spark app found; skipping dependency pin check.");
|
|
192
|
+
}
|
|
193
|
+
if (root) {
|
|
194
|
+
ensureProjectVersion(root);
|
|
195
|
+
try {
|
|
196
|
+
const issues = [...upgradeDependencyPins(root, true), ...enrichedHtmlPinIssues(root)];
|
|
197
|
+
printDependencyPins(issues);
|
|
198
|
+
if (issues.length) {
|
|
199
|
+
process.exitCode = 1;
|
|
200
|
+
pinIssues = true;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
console.error(`Dependency pin check failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
205
|
+
process.exitCode = 1;
|
|
206
|
+
pinIssues = true;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
await doctor(root ?? start);
|
|
178
210
|
console.log("Native toolchain available.");
|
|
211
|
+
if (pinIssues)
|
|
212
|
+
console.log("Dependency pin issues found; run spark upgrade.");
|
|
179
213
|
break;
|
|
214
|
+
}
|
|
215
|
+
case "upgrade": {
|
|
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());
|
|
223
|
+
break;
|
|
224
|
+
}
|
|
180
225
|
case "updates": {
|
|
181
226
|
if (positionals[1] !== "init" || !positionals[2])
|
|
182
227
|
throw new Error("Usage: spark updates init https://example.com/updates/appcast.xml");
|
|
@@ -230,6 +275,6 @@ try {
|
|
|
230
275
|
await main();
|
|
231
276
|
}
|
|
232
277
|
catch (error) {
|
|
233
|
-
|
|
278
|
+
reportError(process.cwd(), error, "Spark");
|
|
234
279
|
process.exitCode = 1;
|
|
235
280
|
}
|
|
@@ -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,16 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export function readJson(file) {
|
|
4
|
+
return JSON.parse(readFileSync(file, "utf8"));
|
|
5
|
+
}
|
|
6
|
+
export function writeJson(file, value) {
|
|
7
|
+
const content = JSON.stringify(value, null, 2) + "\n";
|
|
8
|
+
// Metro watches JSON too: rewriting unchanged session state on every runtime
|
|
9
|
+
// check causes empty Fast Refresh updates even when app code has not changed.
|
|
10
|
+
if (existsSync(file) && readFileSync(file, "utf8") === content)
|
|
11
|
+
return;
|
|
12
|
+
mkdirSync(path.dirname(file), { recursive: true });
|
|
13
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
14
|
+
writeFileSync(temporary, content);
|
|
15
|
+
renameSync(temporary, file);
|
|
16
|
+
}
|
|
@@ -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 };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue };
|
|
2
|
+
export type JsonObject = { [key: string]: JsonValue };
|
|
3
|
+
export interface FileChange { file: string; before: string | null; after: string | null }
|
|
4
|
+
export interface ManualStep { version: string; message: string; notes: string }
|
|
5
|
+
export interface MigrationContext {
|
|
6
|
+
readFile(file: string): string | null;
|
|
7
|
+
writeFile(file: string, content: string): void;
|
|
8
|
+
editJson<T extends object = JsonObject>(file: string, edit: (value: T) => void): void;
|
|
9
|
+
renameFile(from: string, to: string): void;
|
|
10
|
+
updateTemplate(file: string, previous: string, next: string): void;
|
|
11
|
+
replaceImport(from: string, to: string, names?: Readonly<Record<string, string>>): void;
|
|
12
|
+
manualStep(message: string): void;
|
|
13
|
+
}
|
|
14
|
+
export const upgradeNotesPath: string;
|
|
15
|
+
export function createMigrationContext(root: string, version: string, changes: Map<string, FileChange>, manualSteps: ManualStep[]): MigrationContext;
|