@ohos-ports/assistant-ui 0.0.0-stage → 0.0.136-beta.1
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 +148 -2
- package/bin/assistant-ui.js +2 -0
- package/dist/codemods/utils/createTransformer.d.ts +15 -0
- package/dist/codemods/utils/createTransformer.d.ts.map +1 -0
- package/dist/codemods/utils/createTransformer.js +19 -0
- package/dist/codemods/utils/createTransformer.js.map +1 -0
- package/dist/codemods/utils/resolveBinding.d.ts +3 -0
- package/dist/codemods/utils/resolveBinding.d.ts.map +1 -0
- package/dist/codemods/utils/resolveBinding.js +77 -0
- package/dist/codemods/utils/resolveBinding.js.map +1 -0
- package/dist/codemods/v0-11/content-part-to-message-part.d.ts +3 -0
- package/dist/codemods/v0-11/content-part-to-message-part.d.ts.map +1 -0
- package/dist/codemods/v0-11/content-part-to-message-part.js +189 -0
- package/dist/codemods/v0-11/content-part-to-message-part.js.map +1 -0
- package/dist/codemods/v0-12/assistant-api-to-aui.d.ts +3 -0
- package/dist/codemods/v0-12/assistant-api-to-aui.d.ts.map +1 -0
- package/dist/codemods/v0-12/assistant-api-to-aui.js +117 -0
- package/dist/codemods/v0-12/assistant-api-to-aui.js.map +1 -0
- package/dist/codemods/v0-12/event-names-to-camelcase.d.ts +3 -0
- package/dist/codemods/v0-12/event-names-to-camelcase.d.ts.map +1 -0
- package/dist/codemods/v0-12/event-names-to-camelcase.js +38 -0
- package/dist/codemods/v0-12/event-names-to-camelcase.js.map +1 -0
- package/dist/codemods/v0-12/primitive-if-to-aui-if.d.ts +3 -0
- package/dist/codemods/v0-12/primitive-if-to-aui-if.d.ts.map +1 -0
- package/dist/codemods/v0-12/primitive-if-to-aui-if.js +226 -0
- package/dist/codemods/v0-12/primitive-if-to-aui-if.js.map +1 -0
- package/dist/codemods/v0-15/aui-accessor-calls-to-properties.d.ts +3 -0
- package/dist/codemods/v0-15/aui-accessor-calls-to-properties.d.ts.map +1 -0
- package/dist/codemods/v0-15/aui-accessor-calls-to-properties.js +85 -0
- package/dist/codemods/v0-15/aui-accessor-calls-to-properties.js.map +1 -0
- package/dist/codemods/v0-8/ui-package-split.d.ts +3 -0
- package/dist/codemods/v0-8/ui-package-split.d.ts.map +1 -0
- package/dist/codemods/v0-8/ui-package-split.js +102 -0
- package/dist/codemods/v0-8/ui-package-split.js.map +1 -0
- package/dist/codemods/v0-9/edge-package-split.d.ts +3 -0
- package/dist/codemods/v0-9/edge-package-split.d.ts.map +1 -0
- package/dist/codemods/v0-9/edge-package-split.js +47 -0
- package/dist/codemods/v0-9/edge-package-split.js.map +1 -0
- package/dist/commands/add.d.ts +17 -0
- package/dist/commands/add.d.ts.map +1 -0
- package/dist/commands/add.js +63 -0
- package/dist/commands/add.js.map +1 -0
- package/dist/commands/agent.d.ts +3 -0
- package/dist/commands/agent.d.ts.map +1 -0
- package/dist/commands/agent.js +27 -0
- package/dist/commands/agent.js.map +1 -0
- package/dist/commands/cloud.d.ts +25 -0
- package/dist/commands/cloud.d.ts.map +1 -0
- package/dist/commands/cloud.js +239 -0
- package/dist/commands/cloud.js.map +1 -0
- package/dist/commands/create.d.ts +58 -0
- package/dist/commands/create.d.ts.map +1 -0
- package/dist/commands/create.js +626 -0
- package/dist/commands/create.js.map +1 -0
- package/dist/commands/doctor.d.ts +22 -0
- package/dist/commands/doctor.d.ts.map +1 -0
- package/dist/commands/doctor.js +255 -0
- package/dist/commands/doctor.js.map +1 -0
- package/dist/commands/info.d.ts +6 -0
- package/dist/commands/info.d.ts.map +1 -0
- package/dist/commands/info.js +288 -0
- package/dist/commands/info.js.map +1 -0
- package/dist/commands/init.d.ts +13 -0
- package/dist/commands/init.d.ts.map +1 -0
- package/dist/commands/init.js +100 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/mcp.d.ts +3 -0
- package/dist/commands/mcp.d.ts.map +1 -0
- package/dist/commands/mcp.js +279 -0
- package/dist/commands/mcp.js.map +1 -0
- package/dist/commands/setup.d.ts +24 -0
- package/dist/commands/setup.d.ts.map +1 -0
- package/dist/commands/setup.js +136 -0
- package/dist/commands/setup.js.map +1 -0
- package/dist/commands/update.d.ts +3 -0
- package/dist/commands/update.d.ts.map +1 -0
- package/dist/commands/update.js +58 -0
- package/dist/commands/update.js.map +1 -0
- package/dist/commands/upgrade.d.ts +10 -0
- package/dist/commands/upgrade.d.ts.map +1 -0
- package/dist/commands/upgrade.js +38 -0
- package/dist/commands/upgrade.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/agent-gate.d.ts +66 -0
- package/dist/lib/agent-gate.d.ts.map +1 -0
- package/dist/lib/agent-gate.js +180 -0
- package/dist/lib/agent-gate.js.map +1 -0
- package/dist/lib/agent-setup-hint.d.ts +20 -0
- package/dist/lib/agent-setup-hint.d.ts.map +1 -0
- package/dist/lib/agent-setup-hint.js +33 -0
- package/dist/lib/agent-setup-hint.js.map +1 -0
- package/dist/lib/agent-skill.d.ts +14 -0
- package/dist/lib/agent-skill.d.ts.map +1 -0
- package/dist/lib/agent-skill.js +83 -0
- package/dist/lib/agent-skill.js.map +1 -0
- package/dist/lib/cloud-abort.d.ts +2 -0
- package/dist/lib/cloud-abort.d.ts.map +1 -0
- package/dist/lib/cloud-abort.js +12 -0
- package/dist/lib/cloud-abort.js.map +1 -0
- package/dist/lib/cloud-auth.d.ts +30 -0
- package/dist/lib/cloud-auth.d.ts.map +1 -0
- package/dist/lib/cloud-auth.js +179 -0
- package/dist/lib/cloud-auth.js.map +1 -0
- package/dist/lib/cloud-config.d.ts +31 -0
- package/dist/lib/cloud-config.d.ts.map +1 -0
- package/dist/lib/cloud-config.js +128 -0
- package/dist/lib/cloud-config.js.map +1 -0
- package/dist/lib/cloud-organizations.d.ts +17 -0
- package/dist/lib/cloud-organizations.d.ts.map +1 -0
- package/dist/lib/cloud-organizations.js +36 -0
- package/dist/lib/cloud-organizations.js.map +1 -0
- package/dist/lib/cloud-provision.d.ts +20 -0
- package/dist/lib/cloud-provision.d.ts.map +1 -0
- package/dist/lib/cloud-provision.js +59 -0
- package/dist/lib/cloud-provision.js.map +1 -0
- package/dist/lib/cloud-remote.d.ts +37 -0
- package/dist/lib/cloud-remote.d.ts.map +1 -0
- package/dist/lib/cloud-remote.js +118 -0
- package/dist/lib/cloud-remote.js.map +1 -0
- package/dist/lib/cloud-setup-login.d.ts +9 -0
- package/dist/lib/cloud-setup-login.d.ts.map +1 -0
- package/dist/lib/cloud-setup-login.js +136 -0
- package/dist/lib/cloud-setup-login.js.map +1 -0
- package/dist/lib/cloud-url.d.ts +3 -0
- package/dist/lib/cloud-url.d.ts.map +1 -0
- package/dist/lib/cloud-url.js +10 -0
- package/dist/lib/cloud-url.js.map +1 -0
- package/dist/lib/create-project.d.ts +51 -0
- package/dist/lib/create-project.d.ts.map +1 -0
- package/dist/lib/create-project.js +458 -0
- package/dist/lib/create-project.js.map +1 -0
- package/dist/lib/handle-cli-error.d.ts +2 -0
- package/dist/lib/handle-cli-error.d.ts.map +1 -0
- package/dist/lib/handle-cli-error.js +16 -0
- package/dist/lib/handle-cli-error.js.map +1 -0
- package/dist/lib/init-finish.d.ts +46 -0
- package/dist/lib/init-finish.d.ts.map +1 -0
- package/dist/lib/init-finish.js +187 -0
- package/dist/lib/init-finish.js.map +1 -0
- package/dist/lib/install-ai-sdk-lib.d.ts +2 -0
- package/dist/lib/install-ai-sdk-lib.d.ts.map +1 -0
- package/dist/lib/install-ai-sdk-lib.js +21 -0
- package/dist/lib/install-ai-sdk-lib.js.map +1 -0
- package/dist/lib/install-edge-lib.d.ts +2 -0
- package/dist/lib/install-edge-lib.d.ts.map +1 -0
- package/dist/lib/install-edge-lib.js +14 -0
- package/dist/lib/install-edge-lib.js.map +1 -0
- package/dist/lib/run-spawn.d.ts +21 -0
- package/dist/lib/run-spawn.d.ts.map +1 -0
- package/dist/lib/run-spawn.js +118 -0
- package/dist/lib/run-spawn.js.map +1 -0
- package/dist/lib/safe-project-file.d.ts +2 -0
- package/dist/lib/safe-project-file.d.ts.map +1 -0
- package/dist/lib/safe-project-file.js +25 -0
- package/dist/lib/safe-project-file.js.map +1 -0
- package/dist/lib/setup-session.d.ts +44 -0
- package/dist/lib/setup-session.d.ts.map +1 -0
- package/dist/lib/setup-session.js +122 -0
- package/dist/lib/setup-session.js.map +1 -0
- package/dist/lib/transform-options.d.ts +7 -0
- package/dist/lib/transform-options.d.ts.map +1 -0
- package/dist/lib/transform-options.js +0 -0
- package/dist/lib/transform.d.ts +17 -0
- package/dist/lib/transform.d.ts.map +1 -0
- package/dist/lib/transform.js +75 -0
- package/dist/lib/transform.js.map +1 -0
- package/dist/lib/upgrade.d.ts +9 -0
- package/dist/lib/upgrade.d.ts.map +1 -0
- package/dist/lib/upgrade.js +68 -0
- package/dist/lib/upgrade.js.map +1 -0
- package/dist/lib/utils/config.d.ts +20 -0
- package/dist/lib/utils/config.d.ts.map +1 -0
- package/dist/lib/utils/config.js +28 -0
- package/dist/lib/utils/config.js.map +1 -0
- package/dist/lib/utils/download.d.ts +5 -0
- package/dist/lib/utils/download.d.ts.map +1 -0
- package/dist/lib/utils/download.js +40 -0
- package/dist/lib/utils/download.js.map +1 -0
- package/dist/lib/utils/file-scanner.d.ts +15 -0
- package/dist/lib/utils/file-scanner.d.ts.map +1 -0
- package/dist/lib/utils/file-scanner.js +37 -0
- package/dist/lib/utils/file-scanner.js.map +1 -0
- package/dist/lib/utils/logger.d.ts +9 -0
- package/dist/lib/utils/logger.d.ts.map +1 -0
- package/dist/lib/utils/logger.js +25 -0
- package/dist/lib/utils/logger.js.map +1 -0
- package/dist/lib/utils/package-installer.d.ts +9 -0
- package/dist/lib/utils/package-installer.d.ts.map +1 -0
- package/dist/lib/utils/package-installer.js +21 -0
- package/dist/lib/utils/package-installer.js.map +1 -0
- package/dist/lib/utils/package-json.d.ts +2 -0
- package/dist/lib/utils/package-json.d.ts.map +1 -0
- package/dist/lib/utils/package-json.js +10 -0
- package/dist/lib/utils/package-json.js.map +1 -0
- package/dist/lib/utils/package-manager.d.ts +24 -0
- package/dist/lib/utils/package-manager.d.ts.map +1 -0
- package/dist/lib/utils/package-manager.js +117 -0
- package/dist/lib/utils/package-manager.js.map +1 -0
- package/dist/lib/utils/registry.d.ts +6 -0
- package/dist/lib/utils/registry.d.ts.map +1 -0
- package/dist/lib/utils/registry.js +35 -0
- package/dist/lib/utils/registry.js.map +1 -0
- package/dist/lib/utils/workspace.d.ts +3 -0
- package/dist/lib/utils/workspace.d.ts.map +1 -0
- package/dist/lib/utils/workspace.js +27 -0
- package/dist/lib/utils/workspace.js.map +1 -0
- package/dist/program.d.ts +3 -0
- package/dist/program.d.ts.map +1 -0
- package/dist/program.js +22 -0
- package/dist/program.js.map +1 -0
- package/dist/run.d.ts +2 -0
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +8 -0
- package/dist/run.js.map +1 -0
- package/package.json +76 -4
- package/src/codemods/utils/createTransformer.ts +41 -0
- package/src/codemods/utils/resolveBinding.test.ts +107 -0
- package/src/codemods/utils/resolveBinding.ts +130 -0
- package/src/codemods/v0-11/README.md +246 -0
- package/src/codemods/v0-11/__tests__/content-part-to-message-part.test.ts +496 -0
- package/src/codemods/v0-11/content-part-to-message-part.ts +302 -0
- package/src/codemods/v0-12/__tests__/assistant-api-to-aui.test.ts +1220 -0
- package/src/codemods/v0-12/__tests__/event-names-to-camelcase.test.ts +150 -0
- package/src/codemods/v0-12/__tests__/primitive-if-to-aui-if.test.ts +1166 -0
- package/src/codemods/v0-12/assistant-api-to-aui.ts +270 -0
- package/src/codemods/v0-12/event-names-to-camelcase.ts +47 -0
- package/src/codemods/v0-12/primitive-if-to-aui-if.ts +372 -0
- package/src/codemods/v0-15/__tests__/aui-accessor-calls-to-properties.test.ts +210 -0
- package/src/codemods/v0-15/aui-accessor-calls-to-properties.ts +138 -0
- package/src/codemods/v0-8/__tests__/ui-package-split.test.ts +83 -0
- package/src/codemods/v0-8/ui-package-split.ts +159 -0
- package/src/codemods/v0-9/edge-package-split.test.ts +100 -0
- package/src/codemods/v0-9/edge-package-split.ts +70 -0
- package/src/commands/add.ts +111 -0
- package/src/commands/agent.ts +31 -0
- package/src/commands/cloud.test.ts +477 -0
- package/src/commands/cloud.ts +523 -0
- package/src/commands/create.ts +914 -0
- package/src/commands/doctor.ts +425 -0
- package/src/commands/info.ts +481 -0
- package/src/commands/init.ts +156 -0
- package/src/commands/mcp.test.ts +123 -0
- package/src/commands/mcp.ts +475 -0
- package/src/commands/setup.test.ts +285 -0
- package/src/commands/setup.ts +240 -0
- package/src/commands/update.ts +86 -0
- package/src/commands/upgrade.ts +66 -0
- package/src/index.ts +6 -0
- package/src/lib/agent-gate.test.ts +435 -0
- package/src/lib/agent-gate.ts +271 -0
- package/src/lib/agent-setup-hint.test.ts +134 -0
- package/src/lib/agent-setup-hint.ts +56 -0
- package/src/lib/agent-skill.ts +107 -0
- package/src/lib/cloud-abort.ts +12 -0
- package/src/lib/cloud-auth.test.ts +536 -0
- package/src/lib/cloud-auth.ts +299 -0
- package/src/lib/cloud-config.test.ts +220 -0
- package/src/lib/cloud-config.ts +217 -0
- package/src/lib/cloud-organizations.test.ts +76 -0
- package/src/lib/cloud-organizations.ts +55 -0
- package/src/lib/cloud-provision.test.ts +157 -0
- package/src/lib/cloud-provision.ts +96 -0
- package/src/lib/cloud-remote.test.ts +252 -0
- package/src/lib/cloud-remote.ts +236 -0
- package/src/lib/cloud-setup-login.test.ts +361 -0
- package/src/lib/cloud-setup-login.ts +240 -0
- package/src/lib/cloud-url.ts +21 -0
- package/src/lib/create-project-format.test.ts +122 -0
- package/src/lib/create-project.test.ts +158 -0
- package/src/lib/create-project.ts +717 -0
- package/src/lib/handle-cli-error.test.ts +67 -0
- package/src/lib/handle-cli-error.ts +17 -0
- package/src/lib/init-finish.ts +293 -0
- package/src/lib/install-ai-sdk-lib.ts +25 -0
- package/src/lib/install-edge-lib.ts +16 -0
- package/src/lib/run-spawn.test.ts +194 -0
- package/src/lib/run-spawn.ts +154 -0
- package/src/lib/safe-project-file.ts +31 -0
- package/src/lib/setup-session.test.ts +184 -0
- package/src/lib/setup-session.ts +179 -0
- package/src/lib/transform-files.test.ts +69 -0
- package/src/lib/transform-options.ts +6 -0
- package/src/lib/transform.test.ts +155 -0
- package/src/lib/transform.ts +131 -0
- package/src/lib/upgrade.ts +89 -0
- package/src/lib/utils/config.ts +60 -0
- package/src/lib/utils/download.ts +57 -0
- package/src/lib/utils/file-scanner.ts +51 -0
- package/src/lib/utils/logger.ts +22 -0
- package/src/lib/utils/package-installer.test.ts +39 -0
- package/src/lib/utils/package-installer.ts +40 -0
- package/src/lib/utils/package-json.ts +12 -0
- package/src/lib/utils/package-manager.ts +160 -0
- package/src/lib/utils/registry.test.ts +164 -0
- package/src/lib/utils/registry.ts +78 -0
- package/src/lib/utils/workspace.ts +34 -0
- package/src/program.ts +37 -0
- package/src/run.test.ts +23 -0
- package/src/run.ts +5 -0
- package/templates/cloud-harness/.assistant-ui/template.json +3 -0
- package/templates/cloud-harness/.env.example +1 -0
- package/templates/cloud-harness/.oxfmtrc.json +32 -0
- package/templates/cloud-harness/README.md +28 -0
- package/templates/cloud-harness/app/api/chat/route.ts +22 -0
- package/templates/cloud-harness/app/api/credential/route.ts +14 -0
- package/templates/cloud-harness/app/api/request-context.ts +16 -0
- package/templates/cloud-harness/app/assistant.tsx +83 -0
- package/templates/cloud-harness/app/favicon.ico +0 -0
- package/templates/cloud-harness/app/globals.css +139 -0
- package/templates/cloud-harness/app/layout.tsx +37 -0
- package/templates/cloud-harness/app/page.tsx +5 -0
- package/templates/cloud-harness/components/assistant-ui/elements/attachment-state.ts +59 -0
- package/templates/cloud-harness/components/assistant-ui/elements/attachment.aui.tsx +246 -0
- package/templates/cloud-harness/components/assistant-ui/elements/collapsible-root.tsx +19 -0
- package/templates/cloud-harness/components/assistant-ui/elements/markdown-text.tsx +238 -0
- package/templates/cloud-harness/components/assistant-ui/elements/thread.aui.tsx +521 -0
- package/templates/cloud-harness/components/assistant-ui/elements/tool-fallback.aui.tsx +1336 -0
- package/templates/cloud-harness/components/assistant-ui/elements/tooltip-icon-button.tsx +49 -0
- package/templates/cloud-harness/components/ui/avatar.tsx +109 -0
- package/templates/cloud-harness/components/ui/button.tsx +58 -0
- package/templates/cloud-harness/components/ui/collapsible.tsx +21 -0
- package/templates/cloud-harness/components/ui/dialog.tsx +159 -0
- package/templates/cloud-harness/components/ui/textarea.tsx +18 -0
- package/templates/cloud-harness/components/ui/tooltip.tsx +69 -0
- package/templates/cloud-harness/components.json +24 -0
- package/templates/cloud-harness/hooks/use-attachment-src.ts +46 -0
- package/templates/cloud-harness/hooks/use-copy-to-clipboard.ts +54 -0
- package/templates/cloud-harness/lib/utils.ts +1 -0
- package/templates/cloud-harness/next.config.ts +6 -0
- package/templates/cloud-harness/package.json +51 -0
- package/templates/cloud-harness/postcss.config.mjs +5 -0
- package/templates/cloud-harness/tsconfig.json +33 -0
- package/templates/cloud-harness/vendor/LICENSE +21 -0
- package/templates/cloud-harness/vendor/README.md +13 -0
- package/templates/cloud-harness/vendor/assistant-ui-react-harness-sdk-0.0.2.tgz +0 -0
- package/templates/cloud-harness/vendor/harness-sdk-0.3.1.tgz +0 -0
- package/templates/cloud-harness/vendor/provenance.json +38 -0
- package/templates/cloud-harness/vendor/statewire-0.19.3.tgz +0 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { handleCliError } from "./handle-cli-error";
|
|
4
|
+
import { SpawnExitError, SpawnSignalError } from "./run-spawn";
|
|
5
|
+
|
|
6
|
+
describe("handleCliError", () => {
|
|
7
|
+
afterEach(() => {
|
|
8
|
+
vi.restoreAllMocks();
|
|
9
|
+
process.exitCode = undefined;
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("exits by the forwarded signal's status and re-raises it", () => {
|
|
13
|
+
const kill = vi.spyOn(process, "kill").mockReturnValue(true);
|
|
14
|
+
|
|
15
|
+
handleCliError(new SpawnSignalError("SIGINT", true));
|
|
16
|
+
|
|
17
|
+
expect(process.exitCode).toBe(130);
|
|
18
|
+
expect(kill).toHaveBeenCalledWith(process.pid, "SIGINT");
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("maps SIGTERM to 143", () => {
|
|
22
|
+
const kill = vi.spyOn(process, "kill").mockReturnValue(true);
|
|
23
|
+
|
|
24
|
+
handleCliError(new SpawnSignalError("SIGTERM", true));
|
|
25
|
+
|
|
26
|
+
expect(process.exitCode).toBe(143);
|
|
27
|
+
expect(kill).toHaveBeenCalledWith(process.pid, "SIGTERM");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("reports a signal the child raised on itself without re-raising it", () => {
|
|
31
|
+
const consoleError = vi
|
|
32
|
+
.spyOn(console, "error")
|
|
33
|
+
.mockImplementation(() => {});
|
|
34
|
+
const kill = vi.spyOn(process, "kill").mockReturnValue(true);
|
|
35
|
+
|
|
36
|
+
handleCliError(new SpawnSignalError("SIGKILL", false));
|
|
37
|
+
|
|
38
|
+
expect(process.exitCode).toBe(137);
|
|
39
|
+
expect(kill).not.toHaveBeenCalled();
|
|
40
|
+
expect(consoleError).toHaveBeenCalledWith("Process terminated by SIGKILL");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("stays silent on a signal the user sent", () => {
|
|
44
|
+
const consoleError = vi
|
|
45
|
+
.spyOn(console, "error")
|
|
46
|
+
.mockImplementation(() => {});
|
|
47
|
+
vi.spyOn(process, "kill").mockReturnValue(true);
|
|
48
|
+
|
|
49
|
+
handleCliError(new SpawnSignalError("SIGINT", true));
|
|
50
|
+
|
|
51
|
+
expect(consoleError).not.toHaveBeenCalled();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("reports any other failure as exit 1", () => {
|
|
55
|
+
const consoleError = vi
|
|
56
|
+
.spyOn(console, "error")
|
|
57
|
+
.mockImplementation(() => {});
|
|
58
|
+
const kill = vi.spyOn(process, "kill").mockReturnValue(true);
|
|
59
|
+
const error = new SpawnExitError(7);
|
|
60
|
+
|
|
61
|
+
handleCliError(error);
|
|
62
|
+
|
|
63
|
+
expect(process.exitCode).toBe(1);
|
|
64
|
+
expect(kill).not.toHaveBeenCalled();
|
|
65
|
+
expect(consoleError).toHaveBeenCalledWith(error);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { constants } from "node:os";
|
|
2
|
+
import { SpawnSignalError } from "./run-spawn";
|
|
3
|
+
|
|
4
|
+
export function handleCliError(error: unknown): void {
|
|
5
|
+
if (error instanceof SpawnSignalError) {
|
|
6
|
+
process.exitCode = 128 + (constants.signals[error.signal] ?? 0);
|
|
7
|
+
if (error.forwarded) {
|
|
8
|
+
process.kill(process.pid, error.signal);
|
|
9
|
+
} else {
|
|
10
|
+
console.error(error.message);
|
|
11
|
+
}
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
console.error(error);
|
|
16
|
+
process.exitCode = 1;
|
|
17
|
+
}
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { assertSafeProjectFile } from "./safe-project-file";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Follow-up fixes for `assistant-ui init` in an existing project, applied
|
|
7
|
+
* after `shadcn init` and `shadcn add` have run. Each step only touches a
|
|
8
|
+
* file when it can tell the file is still the generated default.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
const FONT_KEYS = ["--font-sans", "--font-serif", "--font-mono"] as const;
|
|
12
|
+
type FontKey = (typeof FONT_KEYS)[number];
|
|
13
|
+
export type FontSnapshot = Partial<Record<FontKey, string>>;
|
|
14
|
+
|
|
15
|
+
const CSS_CANDIDATES = [
|
|
16
|
+
"app/globals.css",
|
|
17
|
+
"src/app/globals.css",
|
|
18
|
+
"styles/globals.css",
|
|
19
|
+
"src/styles/globals.css",
|
|
20
|
+
];
|
|
21
|
+
|
|
22
|
+
const escapeRegExp = (value: string) =>
|
|
23
|
+
value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
24
|
+
|
|
25
|
+
const declarationPattern = (key: string) =>
|
|
26
|
+
new RegExp(`^([ \\t]*)${escapeRegExp(key)}:[ \\t]*([^;\\n]+);`, "m");
|
|
27
|
+
|
|
28
|
+
const isSelfReference = (key: string, value: string) =>
|
|
29
|
+
value.replace(/\s+/g, "") === `var(${key})`;
|
|
30
|
+
|
|
31
|
+
/** Reads the `--font-*` theme values a stylesheet sets before shadcn rewrites it. */
|
|
32
|
+
function readFontSnapshot(css: string): FontSnapshot {
|
|
33
|
+
const snapshot: FontSnapshot = {};
|
|
34
|
+
for (const key of FONT_KEYS) {
|
|
35
|
+
const match = css.match(declarationPattern(key));
|
|
36
|
+
const value = match?.[2]?.trim();
|
|
37
|
+
if (value && !isSelfReference(key, value)) snapshot[key] = value;
|
|
38
|
+
}
|
|
39
|
+
return snapshot;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function snapshotProjectFonts(
|
|
43
|
+
projectRoot: string,
|
|
44
|
+
): Record<string, FontSnapshot> {
|
|
45
|
+
const snapshots: Record<string, FontSnapshot> = {};
|
|
46
|
+
for (const candidate of CSS_CANDIDATES) {
|
|
47
|
+
try {
|
|
48
|
+
const css = fs.readFileSync(path.join(projectRoot, candidate), "utf8");
|
|
49
|
+
snapshots[candidate] = readFontSnapshot(css);
|
|
50
|
+
} catch {
|
|
51
|
+
// not present
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return snapshots;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** CSS variables the root layout's next/font calls declare, e.g. `--font-geist-sans`. */
|
|
58
|
+
function readLayoutFontVariables(layoutSource: string): string[] {
|
|
59
|
+
return [...layoutSource.matchAll(/variable:\s*["'`](--[\w-]+)["'`]/g)].map(
|
|
60
|
+
(match) => match[1]!,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const pickLayoutVariable = (key: FontKey, variables: string[]) => {
|
|
65
|
+
const kind = key.slice("--font-".length);
|
|
66
|
+
if (kind === "sans") {
|
|
67
|
+
return variables.find((v) => !/mono|serif/.test(v));
|
|
68
|
+
}
|
|
69
|
+
return variables.find((v) => v.includes(kind));
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export interface FontRepair {
|
|
73
|
+
key: string;
|
|
74
|
+
value: string | null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* shadcn writes `--font-sans: var(--font-sans)` into `@theme inline`, which
|
|
79
|
+
* only works when the layout loads a font into `--font-sans`. Anywhere else it
|
|
80
|
+
* is a cycle that drops the project's font. Point it back at what the stylesheet
|
|
81
|
+
* used before, or at the layout's next/font variable, or drop it.
|
|
82
|
+
*/
|
|
83
|
+
export function repairSelfReferencingFonts(
|
|
84
|
+
css: string,
|
|
85
|
+
snapshot: FontSnapshot,
|
|
86
|
+
layoutVariables: string[],
|
|
87
|
+
): { css: string; repairs: FontRepair[] } {
|
|
88
|
+
const repairs: FontRepair[] = [];
|
|
89
|
+
let next = css;
|
|
90
|
+
for (const key of FONT_KEYS) {
|
|
91
|
+
const match = next.match(declarationPattern(key));
|
|
92
|
+
if (!match || !isSelfReference(key, match[2]!)) continue;
|
|
93
|
+
if (layoutVariables.includes(key)) continue;
|
|
94
|
+
|
|
95
|
+
const layoutVariable = pickLayoutVariable(key, layoutVariables);
|
|
96
|
+
const value =
|
|
97
|
+
snapshot[key] ?? (layoutVariable ? `var(${layoutVariable})` : null);
|
|
98
|
+
next =
|
|
99
|
+
value === null
|
|
100
|
+
? next.replace(
|
|
101
|
+
new RegExp(`${declarationPattern(key).source}[ \\t]*\\n?`, "m"),
|
|
102
|
+
"",
|
|
103
|
+
)
|
|
104
|
+
: next.replace(declarationPattern(key), `${match[1]}${key}: ${value};`);
|
|
105
|
+
repairs.push({ key, value });
|
|
106
|
+
}
|
|
107
|
+
return { css: next, repairs };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const STARTER_MARKERS = [/utm_source=create-next-app/, /\/next\.svg/];
|
|
111
|
+
|
|
112
|
+
/** True for the page.tsx create-next-app writes, before anyone edits it. */
|
|
113
|
+
function isCreateNextAppStarterPage(source: string): boolean {
|
|
114
|
+
return STARTER_MARKERS.every((marker) => marker.test(source));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export const ASSISTANT_PAGE_SOURCE = `import { Assistant } from "./assistant";
|
|
118
|
+
|
|
119
|
+
export default function Home() {
|
|
120
|
+
return <Assistant />;
|
|
121
|
+
}
|
|
122
|
+
`;
|
|
123
|
+
|
|
124
|
+
const CN_ONE_LINER = /^export\s*\{\s*cn\s*\}\s*from\s*["']cn["'];?\s*$/;
|
|
125
|
+
|
|
126
|
+
export const CN_UTILS_SOURCE = `// \`cn\` is shadcn's class name helper: clsx + tailwind-merge in one package
|
|
127
|
+
// (https://github.com/shadcn-ui/cn). shadcn/ui components import it directly,
|
|
128
|
+
// so keep the \`cn\` dependency installed.
|
|
129
|
+
export { cn } from "cn"
|
|
130
|
+
`;
|
|
131
|
+
|
|
132
|
+
export interface InitFinishResult {
|
|
133
|
+
fontRepairs: FontRepair[];
|
|
134
|
+
cssPath?: string;
|
|
135
|
+
page:
|
|
136
|
+
| { status: "replaced"; pagePath: string }
|
|
137
|
+
| { status: "kept"; pagePath: string; assistantPath: string }
|
|
138
|
+
| { status: "no-assistant" };
|
|
139
|
+
cnExplained: boolean;
|
|
140
|
+
usesCnPackage: boolean;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const readJson = (file: string): Record<string, unknown> | undefined => {
|
|
144
|
+
try {
|
|
145
|
+
return JSON.parse(fs.readFileSync(file, "utf8")) as Record<string, unknown>;
|
|
146
|
+
} catch {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
const readText = (file: string) => {
|
|
152
|
+
try {
|
|
153
|
+
return fs.readFileSync(file, "utf8");
|
|
154
|
+
} catch {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const writeProjectFile = (projectRoot: string, rel: string, data: string) => {
|
|
160
|
+
const file = path.join(projectRoot, rel);
|
|
161
|
+
assertSafeProjectFile(file, projectRoot);
|
|
162
|
+
fs.writeFileSync(file, data);
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const findFirst = (projectRoot: string, candidates: string[]) =>
|
|
166
|
+
candidates.find((rel) => fs.existsSync(path.join(projectRoot, rel)));
|
|
167
|
+
|
|
168
|
+
export function finishExistingProjectInit(
|
|
169
|
+
projectRoot: string,
|
|
170
|
+
fontSnapshots: Record<string, FontSnapshot>,
|
|
171
|
+
): InitFinishResult {
|
|
172
|
+
const config = readJson(path.join(projectRoot, "components.json"));
|
|
173
|
+
const tailwind = config?.tailwind as { css?: unknown } | undefined;
|
|
174
|
+
const cssPath =
|
|
175
|
+
typeof tailwind?.css === "string" && tailwind.css
|
|
176
|
+
? path.normalize(tailwind.css).split(path.sep).join("/")
|
|
177
|
+
: undefined;
|
|
178
|
+
|
|
179
|
+
const appDir = findFirst(projectRoot, ["app", "src/app"]);
|
|
180
|
+
|
|
181
|
+
// 1. fonts
|
|
182
|
+
let fontRepairs: FontRepair[] = [];
|
|
183
|
+
const css = cssPath ? readText(path.join(projectRoot, cssPath)) : undefined;
|
|
184
|
+
if (cssPath && css !== undefined) {
|
|
185
|
+
const layoutRel = appDir
|
|
186
|
+
? findFirst(projectRoot, [
|
|
187
|
+
`${appDir}/layout.tsx`,
|
|
188
|
+
`${appDir}/layout.jsx`,
|
|
189
|
+
`${appDir}/layout.js`,
|
|
190
|
+
])
|
|
191
|
+
: undefined;
|
|
192
|
+
const layout = layoutRel
|
|
193
|
+
? (readText(path.join(projectRoot, layoutRel)) ?? "")
|
|
194
|
+
: "";
|
|
195
|
+
const result = repairSelfReferencingFonts(
|
|
196
|
+
css,
|
|
197
|
+
fontSnapshots[cssPath] ?? {},
|
|
198
|
+
readLayoutFontVariables(layout),
|
|
199
|
+
);
|
|
200
|
+
if (result.repairs.length > 0) {
|
|
201
|
+
writeProjectFile(projectRoot, cssPath, result.css);
|
|
202
|
+
fontRepairs = result.repairs;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// 2. render <Assistant /> on the home page
|
|
207
|
+
let page: InitFinishResult["page"] = { status: "no-assistant" };
|
|
208
|
+
const assistantRel = appDir
|
|
209
|
+
? findFirst(projectRoot, [
|
|
210
|
+
`${appDir}/assistant.tsx`,
|
|
211
|
+
`${appDir}/assistant.jsx`,
|
|
212
|
+
])
|
|
213
|
+
: undefined;
|
|
214
|
+
if (appDir && assistantRel) {
|
|
215
|
+
const pageRel =
|
|
216
|
+
findFirst(projectRoot, [
|
|
217
|
+
`${appDir}/page.tsx`,
|
|
218
|
+
`${appDir}/page.jsx`,
|
|
219
|
+
`${appDir}/page.js`,
|
|
220
|
+
]) ?? `${appDir}/page.tsx`;
|
|
221
|
+
const pageSource = readText(path.join(projectRoot, pageRel));
|
|
222
|
+
if (
|
|
223
|
+
pageSource !== undefined &&
|
|
224
|
+
isCreateNextAppStarterPage(pageSource) &&
|
|
225
|
+
path.extname(pageRel) === path.extname(assistantRel)
|
|
226
|
+
) {
|
|
227
|
+
writeProjectFile(projectRoot, pageRel, ASSISTANT_PAGE_SOURCE);
|
|
228
|
+
page = { status: "replaced", pagePath: pageRel };
|
|
229
|
+
} else {
|
|
230
|
+
page = { status: "kept", pagePath: pageRel, assistantPath: assistantRel };
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// 3. explain the `cn` dependency where people look for it
|
|
235
|
+
let cnExplained = false;
|
|
236
|
+
const utilsRel = findFirst(projectRoot, ["lib/utils.ts", "src/lib/utils.ts"]);
|
|
237
|
+
if (utilsRel) {
|
|
238
|
+
const utils = readText(path.join(projectRoot, utilsRel)) ?? "";
|
|
239
|
+
if (CN_ONE_LINER.test(utils)) {
|
|
240
|
+
writeProjectFile(projectRoot, utilsRel, CN_UTILS_SOURCE);
|
|
241
|
+
cnExplained = true;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const pkg = readJson(path.join(projectRoot, "package.json"));
|
|
246
|
+
const usesCnPackage = [pkg?.dependencies, pkg?.devDependencies].some(
|
|
247
|
+
(deps) =>
|
|
248
|
+
typeof deps === "object" && deps !== null && Object.hasOwn(deps, "cn"),
|
|
249
|
+
);
|
|
250
|
+
|
|
251
|
+
return {
|
|
252
|
+
fontRepairs,
|
|
253
|
+
...(cssPath ? { cssPath } : {}),
|
|
254
|
+
page,
|
|
255
|
+
cnExplained,
|
|
256
|
+
usesCnPackage,
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** The lines `assistant-ui init` prints once everything is installed. */
|
|
261
|
+
export function describeInitFinish(result: InitFinishResult): string[] {
|
|
262
|
+
const lines: string[] = [];
|
|
263
|
+
if (result.page.status === "replaced") {
|
|
264
|
+
lines.push(
|
|
265
|
+
`${result.page.pagePath} now renders <Assistant />. Run the dev server and open /.`,
|
|
266
|
+
);
|
|
267
|
+
} else if (result.page.status === "kept") {
|
|
268
|
+
const from = path.posix.relative(
|
|
269
|
+
path.posix.dirname(result.page.pagePath),
|
|
270
|
+
result.page.assistantPath.replace(/\.[jt]sx$/, ""),
|
|
271
|
+
);
|
|
272
|
+
const specifier = from.startsWith(".") ? from : `./${from}`;
|
|
273
|
+
lines.push(
|
|
274
|
+
`Next step: render <Assistant /> (import { Assistant } from "${specifier}") in ${result.page.pagePath} or any page.`,
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
lines.push(
|
|
278
|
+
"TooltipProvider: not needed. assistant-ui components include their own, so skip the layout change shadcn suggests above.",
|
|
279
|
+
);
|
|
280
|
+
for (const repair of result.fontRepairs) {
|
|
281
|
+
lines.push(
|
|
282
|
+
repair.value
|
|
283
|
+
? `Kept your font: ${repair.key} now points at ${repair.value} in ${result.cssPath}.`
|
|
284
|
+
: `Removed a self-referencing ${repair.key} from ${result.cssPath}.`,
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
if (result.usesCnPackage) {
|
|
288
|
+
lines.push(
|
|
289
|
+
"The `cn` dependency is shadcn's class name helper (clsx + tailwind-merge). shadcn components import it, so keep it.",
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
return lines;
|
|
293
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { installPackageIfNeeded } from "./utils/package-installer";
|
|
2
|
+
|
|
3
|
+
export default async function installAiSdkLib(): Promise<void> {
|
|
4
|
+
await installPackageIfNeeded({
|
|
5
|
+
packageName: "@assistant-ui/ai-sdk",
|
|
6
|
+
importPatterns: ["@assistant-ui/ai-sdk"],
|
|
7
|
+
promptMessage:
|
|
8
|
+
"AI SDK imports were added but @assistant-ui/ai-sdk is not installed. Do you want to install it? (Y/n) ",
|
|
9
|
+
skipMessage:
|
|
10
|
+
"@assistant-ui/ai-sdk is already installed. Skipping installation.",
|
|
11
|
+
notFoundMessage: "No AI SDK imports found; skipping installation.",
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
// The previous name is a separate package, so an import of it needs that
|
|
15
|
+
// package installed; the neutral one would not make it resolvable.
|
|
16
|
+
await installPackageIfNeeded({
|
|
17
|
+
packageName: "@assistant-ui/react-ai-sdk",
|
|
18
|
+
importPatterns: ["@assistant-ui/react-ai-sdk"],
|
|
19
|
+
promptMessage:
|
|
20
|
+
"AI SDK imports were added but @assistant-ui/react-ai-sdk is not installed. Do you want to install it? (Y/n) ",
|
|
21
|
+
skipMessage:
|
|
22
|
+
"@assistant-ui/react-ai-sdk is already installed. Skipping installation.",
|
|
23
|
+
notFoundMessage: "No legacy AI SDK imports found; skipping installation.",
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { installPackageIfNeeded } from "./utils/package-installer";
|
|
2
|
+
|
|
3
|
+
export default async function installEdgeLib(): Promise<void> {
|
|
4
|
+
// Only the retired edge specifier is unambiguous here. `useChatRuntime` and
|
|
5
|
+
// the AI SDK package specifiers are shared by both AI SDK packages, so
|
|
6
|
+
// installAiSdkLib resolves those by the specifier the file actually imports.
|
|
7
|
+
await installPackageIfNeeded({
|
|
8
|
+
packageName: "@assistant-ui/ai-sdk",
|
|
9
|
+
importPatterns: ["@assistant-ui/react-edge"],
|
|
10
|
+
promptMessage:
|
|
11
|
+
"Edge Runtime imports were detected but @assistant-ui/ai-sdk is not installed. Do you want to install it? (Y/n) ",
|
|
12
|
+
skipMessage:
|
|
13
|
+
"@assistant-ui/ai-sdk is already installed. Skipping installation.",
|
|
14
|
+
notFoundMessage: "No Edge Runtime imports found; skipping installation.",
|
|
15
|
+
});
|
|
16
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
|
+
import { PassThrough } from "node:stream";
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
|
|
5
|
+
const mocks = vi.hoisted(() => ({
|
|
6
|
+
spawn: vi.fn(),
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
vi.mock("cross-spawn", async (importOriginal) => ({
|
|
10
|
+
...(await importOriginal()),
|
|
11
|
+
spawn: mocks.spawn,
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
hasActiveSpawn,
|
|
16
|
+
runSpawn,
|
|
17
|
+
runSpawnCapture,
|
|
18
|
+
SpawnExitError,
|
|
19
|
+
SpawnSignalError,
|
|
20
|
+
} from "./run-spawn";
|
|
21
|
+
|
|
22
|
+
const createChild = () =>
|
|
23
|
+
Object.assign(new EventEmitter(), {
|
|
24
|
+
kill: vi.fn(() => true),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const trackSignal = (signal: NodeJS.Signals) => {
|
|
28
|
+
const before = new Set(process.listeners(signal));
|
|
29
|
+
return {
|
|
30
|
+
count: before.size,
|
|
31
|
+
added: () =>
|
|
32
|
+
process.listeners(signal).find((listener) => !before.has(listener)),
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
describe("runSpawn", () => {
|
|
37
|
+
afterEach(() => {
|
|
38
|
+
vi.restoreAllMocks();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("forwards the first termination signal and cleans up listeners", async () => {
|
|
42
|
+
const child = createChild();
|
|
43
|
+
mocks.spawn.mockReturnValue(child);
|
|
44
|
+
const sigterm = trackSignal("SIGTERM");
|
|
45
|
+
const result = runSpawn("assistant-ui", ["create"]);
|
|
46
|
+
const signalHandler = sigterm.added();
|
|
47
|
+
|
|
48
|
+
expect(signalHandler).toBeDefined();
|
|
49
|
+
signalHandler?.("SIGTERM");
|
|
50
|
+
expect(child.kill).toHaveBeenCalledWith("SIGTERM");
|
|
51
|
+
|
|
52
|
+
child.emit("close", null, "SIGTERM");
|
|
53
|
+
await expect(result).rejects.toMatchObject({
|
|
54
|
+
signal: "SIGTERM",
|
|
55
|
+
forwarded: true,
|
|
56
|
+
});
|
|
57
|
+
expect(process.listenerCount("SIGTERM")).toBe(sigterm.count);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("escalates a repeated termination signal", async () => {
|
|
61
|
+
const child = createChild();
|
|
62
|
+
mocks.spawn.mockReturnValue(child);
|
|
63
|
+
const sigterm = trackSignal("SIGTERM");
|
|
64
|
+
const result = runSpawn("assistant-ui", ["create"]);
|
|
65
|
+
const signalHandler = sigterm.added();
|
|
66
|
+
|
|
67
|
+
expect(signalHandler).toBeDefined();
|
|
68
|
+
signalHandler?.("SIGTERM");
|
|
69
|
+
signalHandler?.("SIGTERM");
|
|
70
|
+
|
|
71
|
+
expect(child.kill).toHaveBeenNthCalledWith(1, "SIGTERM");
|
|
72
|
+
expect(child.kill).toHaveBeenNthCalledWith(2, "SIGKILL");
|
|
73
|
+
expect(hasActiveSpawn()).toBe(true);
|
|
74
|
+
|
|
75
|
+
child.emit("close", null, "SIGKILL");
|
|
76
|
+
await expect(result).rejects.toBeInstanceOf(SpawnSignalError);
|
|
77
|
+
expect(hasActiveSpawn()).toBe(false);
|
|
78
|
+
expect(process.listenerCount("SIGTERM")).toBe(sigterm.count);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("settles after a signaled child exits while captured pipes stay open", async () => {
|
|
82
|
+
const child = Object.assign(createChild(), {
|
|
83
|
+
stdout: new PassThrough(),
|
|
84
|
+
stderr: new PassThrough(),
|
|
85
|
+
});
|
|
86
|
+
mocks.spawn.mockReturnValue(child);
|
|
87
|
+
const sigterm = trackSignal("SIGTERM");
|
|
88
|
+
const result = runSpawnCapture("jscodeshift", ["--version"]);
|
|
89
|
+
const signalHandler = sigterm.added();
|
|
90
|
+
|
|
91
|
+
signalHandler?.("SIGTERM");
|
|
92
|
+
signalHandler?.("SIGTERM");
|
|
93
|
+
child.emit("exit", null, "SIGKILL");
|
|
94
|
+
|
|
95
|
+
await expect(result).rejects.toBeInstanceOf(SpawnSignalError);
|
|
96
|
+
expect(child.stdout.destroyed).toBe(true);
|
|
97
|
+
expect(child.stderr.destroyed).toBe(true);
|
|
98
|
+
expect(hasActiveSpawn()).toBe(false);
|
|
99
|
+
expect(process.listenerCount("SIGTERM")).toBe(sigterm.count);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("settles on a third signal when the child does not exit", async () => {
|
|
103
|
+
const child = createChild();
|
|
104
|
+
mocks.spawn.mockReturnValue(child);
|
|
105
|
+
const sigterm = trackSignal("SIGTERM");
|
|
106
|
+
const result = runSpawn("assistant-ui", ["create"]);
|
|
107
|
+
const signalHandler = sigterm.added();
|
|
108
|
+
|
|
109
|
+
signalHandler?.("SIGTERM");
|
|
110
|
+
signalHandler?.("SIGTERM");
|
|
111
|
+
signalHandler?.("SIGTERM");
|
|
112
|
+
|
|
113
|
+
await expect(result).rejects.toMatchObject({
|
|
114
|
+
signal: "SIGTERM",
|
|
115
|
+
forwarded: true,
|
|
116
|
+
});
|
|
117
|
+
expect(child.kill).toHaveBeenNthCalledWith(1, "SIGTERM");
|
|
118
|
+
expect(child.kill).toHaveBeenNthCalledWith(2, "SIGKILL");
|
|
119
|
+
expect(child.kill).toHaveBeenCalledTimes(2);
|
|
120
|
+
expect(hasActiveSpawn()).toBe(false);
|
|
121
|
+
expect(process.listenerCount("SIGTERM")).toBe(sigterm.count);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("reports an active spawn only while the child is running", async () => {
|
|
125
|
+
const child = createChild();
|
|
126
|
+
mocks.spawn.mockReturnValue(child);
|
|
127
|
+
|
|
128
|
+
expect(hasActiveSpawn()).toBe(false);
|
|
129
|
+
const result = runSpawn("assistant-ui", ["create"]);
|
|
130
|
+
expect(hasActiveSpawn()).toBe(true);
|
|
131
|
+
|
|
132
|
+
child.emit("close", 0, null);
|
|
133
|
+
await result;
|
|
134
|
+
|
|
135
|
+
expect(hasActiveSpawn()).toBe(false);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it("preserves normal child exit handling", async () => {
|
|
139
|
+
const child = createChild();
|
|
140
|
+
mocks.spawn.mockReturnValue(child);
|
|
141
|
+
const result = runSpawn("assistant-ui", ["create"]);
|
|
142
|
+
|
|
143
|
+
child.emit("close", 0, null);
|
|
144
|
+
|
|
145
|
+
await expect(result).resolves.toBeUndefined();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("reports nonzero child exits", async () => {
|
|
149
|
+
const child = createChild();
|
|
150
|
+
mocks.spawn.mockReturnValue(child);
|
|
151
|
+
const result = runSpawn("assistant-ui", ["create"]);
|
|
152
|
+
|
|
153
|
+
child.emit("close", 7, null);
|
|
154
|
+
|
|
155
|
+
await expect(result).rejects.toEqual(expect.any(SpawnExitError));
|
|
156
|
+
await expect(result).rejects.toMatchObject({ code: 7 });
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("distinguishes child signals from forwarded signals", async () => {
|
|
160
|
+
const child = createChild();
|
|
161
|
+
mocks.spawn.mockReturnValue(child);
|
|
162
|
+
const result = runSpawn("assistant-ui", ["create"]);
|
|
163
|
+
|
|
164
|
+
child.emit("close", null, "SIGSEGV");
|
|
165
|
+
|
|
166
|
+
await expect(result).rejects.toMatchObject({
|
|
167
|
+
signal: "SIGSEGV",
|
|
168
|
+
forwarded: false,
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("captures output without blocking the event loop", async () => {
|
|
173
|
+
const child = Object.assign(new EventEmitter(), {
|
|
174
|
+
kill: vi.fn(() => true),
|
|
175
|
+
stdout: new PassThrough(),
|
|
176
|
+
stderr: new PassThrough(),
|
|
177
|
+
});
|
|
178
|
+
mocks.spawn.mockReturnValue(child);
|
|
179
|
+
const result = runSpawnCapture("jscodeshift", ["--version"]);
|
|
180
|
+
|
|
181
|
+
child.stdout.write("Processing file one\n");
|
|
182
|
+
child.stderr.write("warning\n");
|
|
183
|
+
child.stdout.end();
|
|
184
|
+
child.stderr.end();
|
|
185
|
+
child.emit("close", 0, null);
|
|
186
|
+
|
|
187
|
+
await expect(result).resolves.toMatchObject({
|
|
188
|
+
code: 0,
|
|
189
|
+
signal: null,
|
|
190
|
+
stdout: "Processing file one\n",
|
|
191
|
+
stderr: "warning\n",
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
});
|