@embeddable.com/sdk-core 3.8.0-next.0 → 3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,64 @@
1
+ import { select } from "@inquirer/prompts";
2
+ import axios from "axios";
3
+
4
+ export async function getWorkspaces(
5
+ ctx: any,
6
+ token: string,
7
+ workspaceSpinner: any,
8
+ ) {
9
+ try {
10
+ const response = await axios.get(`${ctx.pushBaseUrl}/workspace`, {
11
+ headers: {
12
+ Authorization: `Bearer ${token}`,
13
+ },
14
+ });
15
+
16
+ return response.data?.filter((w: any) => !w.devWorkspace);
17
+ } catch (e: any) {
18
+ if (e.response?.status === 401) {
19
+ workspaceSpinner.fail(
20
+ 'Unauthorized. Please login using "embeddable login" command.',
21
+ );
22
+ } else {
23
+ workspaceSpinner.fail("Failed to fetch workspaces");
24
+ }
25
+ process.exit(1);
26
+ }
27
+ }
28
+
29
+ export async function selectWorkspace(ora: any, ctx: any, token: string) {
30
+ const workspaceSpinner = ora({
31
+ text: `Fetching workspaces using ${ctx.pushBaseUrl}...`,
32
+ color: "green",
33
+ discardStdin: false,
34
+ }).start();
35
+
36
+ const availableWorkspaces = await getWorkspaces(ctx, token, workspaceSpinner);
37
+
38
+ let selectedWorkspace;
39
+
40
+ if (availableWorkspaces.length === 0) {
41
+ workspaceSpinner.fail("No workspaces found");
42
+ process.exit(1);
43
+ }
44
+
45
+ workspaceSpinner.info(`Found ${availableWorkspaces.length} workspace(s)`);
46
+
47
+ if (availableWorkspaces.length === 1) {
48
+ selectedWorkspace = availableWorkspaces[0];
49
+ } else {
50
+ selectedWorkspace = await select({
51
+ message: "Select workspace to push changes",
52
+ choices: availableWorkspaces.map((workspace: any) => ({
53
+ name: `${workspace.name} (${workspace.workspaceId})`,
54
+ value: workspace,
55
+ })),
56
+ });
57
+ }
58
+
59
+ workspaceSpinner.succeed(
60
+ `Workspace: ${selectedWorkspace.name} (${selectedWorkspace.workspaceId})`,
61
+ );
62
+
63
+ return selectedWorkspace;
64
+ }
package/src/dev.test.ts DELETED
@@ -1,102 +0,0 @@
1
- import { describe, it, expect, vi, beforeEach, afterEach, Mock } from "vitest";
2
- import * as http from "node:http";
3
- import axios from "axios";
4
- import provideConfig from "./provideConfig";
5
- import { getToken } from "./login";
6
- import * as chokidar from "chokidar";
7
- import dev from "./dev";
8
- import { checkNodeVersion } from "./utils";
9
- import { createManifest } from "./cleanup";
10
- import prepare from "./prepare";
11
- import { WebSocketServer } from "ws";
12
-
13
- // Mock dependencies
14
- vi.mock("./buildTypes", () => ({ default: vi.fn() }));
15
- vi.mock("./prepare", () => ({ default: vi.fn(), removeIfExists: vi.fn() }));
16
- vi.mock("./generate", () => ({ default: vi.fn() }));
17
- vi.mock("./provideConfig", () => ({ default: vi.fn() }));
18
- vi.mock("@stencil/core/sys/node", () => ({
19
- createNodeLogger: vi.fn(),
20
- createNodeSys: vi.fn(),
21
- }));
22
- vi.mock("ws", () => ({ WebSocketServer: vi.fn() }));
23
- vi.mock("chokidar", () => ({ watch: vi.fn() }));
24
- vi.mock("./login", () => ({ getToken: vi.fn(), default: vi.fn() }));
25
- vi.mock("axios", () => ({ default: { get: vi.fn() } }));
26
- vi.mock("@embeddable.com/sdk-utils", () => ({ findFiles: vi.fn() }));
27
- vi.mock("./push", () => ({ archive: vi.fn(), sendBuild: vi.fn() }));
28
- vi.mock("./validate", () => ({ default: vi.fn() }));
29
- vi.mock("./utils", () => ({ checkNodeVersion: vi.fn() }));
30
- vi.mock("./cleanup", () => ({ createManifest: vi.fn() }));
31
- vi.mock("node:http", () => ({
32
- createServer: vi.fn(() => ({ listen: vi.fn() })),
33
- }));
34
-
35
- const mockConfig = {
36
- client: {
37
- rootDir: "/mock/root",
38
- buildDir: "/mock/root/.embeddable-dev-build",
39
- componentDir: "/mock/root/.embeddable-dev-build/component",
40
- stencilBuild: "/mock/root/.embeddable-dev-build/dist/embeddable-wrapper",
41
- tmpDir: "/mock/root/.embeddable-dev-tmp",
42
- selfServeCustomizationDir: "/mock/root/self-serve",
43
- modelsSrc: "/mock/root/models",
44
- },
45
- plugins: [],
46
- previewBaseUrl: "http://preview.example.com",
47
- pushBaseUrl: "http://push.example.com",
48
- };
49
-
50
- describe("dev command", () => {
51
- let listenMock: Mock;
52
- beforeEach(() => {
53
- listenMock = vi.fn();
54
- vi.mocked(http.createServer).mockImplementation(
55
- () =>
56
- ({
57
- listen: listenMock,
58
- }) as any,
59
- );
60
- vi.mocked(WebSocketServer).mockImplementation(() => {
61
- return {
62
- clients: [],
63
- on: vi.fn(),
64
- } as any;
65
- });
66
- vi.mocked(chokidar.watch).mockReturnValue({
67
- on: vi.fn(),
68
- } as any);
69
- // Mock process.on to avoid actually setting up process listeners
70
- vi.spyOn(process, "on").mockImplementation(() => process);
71
-
72
- vi.mocked(provideConfig).mockResolvedValue(mockConfig);
73
- vi.mocked(getToken).mockResolvedValue("mock-token");
74
- vi.mocked(axios.get).mockResolvedValue({
75
- data: "mock-workspace",
76
- });
77
- });
78
-
79
- afterEach(() => {
80
- vi.restoreAllMocks();
81
- });
82
-
83
- it("should set up the development environment", async () => {
84
- // Run the dev command
85
- await dev();
86
-
87
- // Verify that the necessary functions were called
88
- expect(checkNodeVersion).toHaveBeenCalled();
89
- expect(prepare).toHaveBeenCalled();
90
- expect(http.createServer).toHaveBeenCalled();
91
- expect(WebSocketServer).toHaveBeenCalled();
92
-
93
- // Verify that the server was set up to listen on the correct port
94
- expect(listenMock).toHaveBeenCalledWith(8926, expect.any(Function));
95
-
96
- // Call the listen callback to simulate the server being set up
97
- listenMock.mock.calls[0][1]();
98
- expect(createManifest).toHaveBeenCalled();
99
-
100
- await expect.poll(() => chokidar.watch).toBeCalledTimes(2);
101
- });
102
- });