diff --git a/src/components/shared/ComponentEditor/utils/preserveComponentName.ts b/src/components/shared/ComponentEditor/utils/preserveComponentName.ts index a7d40278d..334b545a0 100644 --- a/src/components/shared/ComponentEditor/utils/preserveComponentName.ts +++ b/src/components/shared/ComponentEditor/utils/preserveComponentName.ts @@ -2,7 +2,7 @@ import yaml from "js-yaml"; import type { ComponentSpec } from "@/utils/componentSpec"; import { isValidComponentSpec } from "@/utils/componentSpec"; -import { componentSpecToYaml } from "@/utils/componentStore"; +import { componentSpecToYaml } from "@/utils/yaml"; export const preserveComponentName = ( yamlText: string, diff --git a/src/components/shared/ReactFlow/FlowSidebar/sections/FileActions.tsx b/src/components/shared/ReactFlow/FlowSidebar/sections/FileActions.tsx index 7a5a8e640..5e7b0203d 100644 --- a/src/components/shared/ReactFlow/FlowSidebar/sections/FileActions.tsx +++ b/src/components/shared/ReactFlow/FlowSidebar/sections/FileActions.tsx @@ -21,8 +21,8 @@ import { useAutoSaveStatus } from "@/providers/AutoSaveProvider"; import { useComponentSpec } from "@/providers/ComponentSpecProvider"; import { EDITOR_PATH } from "@/routes/router"; import { getPipelineFile, useSavePipeline } from "@/services/pipelineService"; -import { componentSpecToYaml } from "@/utils/componentStore"; import { formatRelativeTime } from "@/utils/date"; +import { componentSpecToYaml } from "@/utils/yaml"; const FileActions = ({ isOpen }: { isOpen: boolean }) => { const { componentSpec } = useComponentSpec(); diff --git a/src/components/shared/TaskDetails/Implementation.tsx b/src/components/shared/TaskDetails/Implementation.tsx index 9cd83d32f..5272a0a65 100644 --- a/src/components/shared/TaskDetails/Implementation.tsx +++ b/src/components/shared/TaskDetails/Implementation.tsx @@ -2,7 +2,7 @@ import { useMemo } from "react"; import { CodeViewer } from "@/components/shared/CodeViewer"; import type { ComponentSpec } from "@/utils/componentSpec"; -import { componentSpecToText } from "@/utils/componentStore"; +import { componentSpecToText } from "@/utils/yaml"; interface TaskImplementationProps { displayName: string; diff --git a/src/providers/ComponentLibraryProvider/componentLibrary.test.ts b/src/providers/ComponentLibraryProvider/componentLibrary.test.ts index bfd84ce2d..51a8ec05d 100644 --- a/src/providers/ComponentLibraryProvider/componentLibrary.test.ts +++ b/src/providers/ComponentLibraryProvider/componentLibrary.test.ts @@ -13,6 +13,7 @@ import type { } from "@/utils/componentSpec"; import * as componentStore from "@/utils/componentStore"; import * as localforage from "@/utils/localforage"; +import * as yamlUtils from "@/utils/yaml"; import { fetchFavoriteComponents, @@ -35,6 +36,7 @@ const mockLocalforage = vi.mocked(localforage); describe("componentLibrary", () => { beforeEach(() => { vi.clearAllMocks(); + vi.spyOn(yamlUtils, "componentSpecToYaml").mockReturnValue("spec-as-yaml"); }); afterEach(() => { @@ -373,7 +375,9 @@ describe("componentLibrary", () => { }; // Mock componentSpecToYaml to return predictable text - mockComponentStore.componentSpecToYaml.mockReturnValue("spec-as-yaml"); + vi.spyOn(yamlUtils, "componentSpecToYaml").mockReturnValue( + "spec-as-yaml", + ); // Act const result = await populateComponentRefs(folder); @@ -388,7 +392,7 @@ describe("componentLibrary", () => { }, text: "spec-as-yaml", }); - expect(mockComponentStore.componentSpecToYaml).toHaveBeenCalledWith( + expect(yamlUtils.componentSpecToYaml).toHaveBeenCalledWith( componentRef.spec, ); expect(mockComponentStore.generateDigest).toHaveBeenCalledWith( diff --git a/src/providers/ComponentLibraryProvider/componentLibrary.ts b/src/providers/ComponentLibraryProvider/componentLibrary.ts index 0653e0b06..7c3fe2359 100644 --- a/src/providers/ComponentLibraryProvider/componentLibrary.ts +++ b/src/providers/ComponentLibraryProvider/componentLibrary.ts @@ -9,12 +9,12 @@ import type { TaskSpec, } from "@/utils/componentSpec"; import { - componentSpecToYaml, generateDigest, getAllComponentFilesFromList, } from "@/utils/componentStore"; import { USER_COMPONENTS_LIST_NAME } from "@/utils/constants"; import { getComponentByUrl } from "@/utils/localforage"; +import { componentSpecToYaml } from "@/utils/yaml"; export const fetchUserComponents = async (): Promise => { try { diff --git a/src/providers/ComponentSpecProvider.tsx b/src/providers/ComponentSpecProvider.tsx index cb1644080..4146a1fb7 100644 --- a/src/providers/ComponentSpecProvider.tsx +++ b/src/providers/ComponentSpecProvider.tsx @@ -22,6 +22,7 @@ import { type ComponentValidationIssue, type ValidationError, } from "@/utils/validations"; +import { componentSpecToYaml } from "@/utils/yaml"; import { createRequiredContext, @@ -34,7 +35,6 @@ import { } from "../utils/componentSpec"; import { type ComponentReferenceWithSpec, - componentSpecToYaml, generateDigest, writeComponentToFileListFromText, } from "../utils/componentStore"; diff --git a/src/services/componentService.ts b/src/services/componentService.ts index 9e493fd2c..cb6fc84f6 100644 --- a/src/services/componentService.ts +++ b/src/services/componentService.ts @@ -25,7 +25,7 @@ import { type TaskSpec, type UnknownComponentReference, } from "@/utils/componentSpec"; -import { componentSpecToYaml, generateDigest } from "@/utils/componentStore"; +import { generateDigest } from "@/utils/componentStore"; import { componentExistsByUrl, getAllUserComponents, @@ -34,6 +34,7 @@ import { saveComponent, type UserComponent, } from "@/utils/localforage"; +import { componentSpecToYaml } from "@/utils/yaml"; import { componentSpecFromYaml } from "@/utils/yaml"; export interface ExistingAndNewComponent { diff --git a/src/services/pipelineRunService.ts b/src/services/pipelineRunService.ts index 0db9cd82b..f11310f34 100644 --- a/src/services/pipelineRunService.ts +++ b/src/services/pipelineRunService.ts @@ -9,7 +9,6 @@ import { isGraphImplementation, } from "@/utils/componentSpec"; import { - componentSpecToYaml, getComponentFileFromList, writeComponentToFileListFromText, } from "@/utils/componentStore"; @@ -19,6 +18,7 @@ import { USER_PIPELINES_LIST_NAME, } from "@/utils/constants"; import { fetchWithErrorHandling } from "@/utils/fetchWithErrorHandling"; +import { componentSpecToYaml } from "@/utils/yaml"; export const createPipelineRun = async ( payload: BodyCreateApiPipelineRunsPost, diff --git a/src/services/pipelineService.ts b/src/services/pipelineService.ts index 26627f608..948b283aa 100644 --- a/src/services/pipelineService.ts +++ b/src/services/pipelineService.ts @@ -9,7 +9,6 @@ import { } from "@/utils/componentSpec"; import { type ComponentFileEntry, - componentSpecToYaml, deleteComponentFileFromList, fullyLoadComponentRefFromUrl, getAllComponentFilesFromList, @@ -17,6 +16,7 @@ import { writeComponentToFileListFromText, } from "@/utils/componentStore"; import { USER_PIPELINES_LIST_NAME } from "@/utils/constants"; +import { componentSpecToYaml } from "@/utils/yaml"; import { componentSpecFromYaml } from "@/utils/yaml"; export const deletePipeline = async (name: string, onDelete?: () => void) => { diff --git a/src/utils/URL.ts b/src/utils/URL.ts index 6857899ff..8dc0e4e03 100644 --- a/src/utils/URL.ts +++ b/src/utils/URL.ts @@ -1,7 +1,7 @@ import { RUNS_BASE_PATH } from "@/routes/router"; import type { ComponentSpec } from "./componentSpec"; -import { componentSpecToText } from "./componentStore"; +import { componentSpecToText } from "./yaml"; const convertGcsUrlToBrowserUrl = ( url: string, diff --git a/src/utils/componentStore.ts b/src/utils/componentStore.ts index 56909e0d0..dbc1f0a08 100644 --- a/src/utils/componentStore.ts +++ b/src/utils/componentStore.ts @@ -1,4 +1,3 @@ -import yaml from "js-yaml"; import localForage from "localforage"; import { fetchComponentTextFromUrl } from "@/services/componentService"; @@ -8,7 +7,7 @@ import { downloadDataWithCache } from "./cache"; import type { ComponentReference, ComponentSpec } from "./componentSpec"; import { USER_COMPONENTS_LIST_NAME } from "./constants"; import { getIdOrTitleFromPath } from "./URL"; -import { componentSpecFromYaml } from "./yaml"; +import { componentSpecFromYaml, componentSpecToYaml } from "./yaml"; // IndexedDB: DB and table names const DB_NAME = "components"; @@ -606,18 +605,6 @@ export const deleteComponentFileFromList = async ( return componentListDb.removeItem(fileName); }; -export const componentSpecToYaml = (componentSpec: ComponentSpec) => { - return yaml.dump(componentSpec, { lineWidth: 10000 }); -}; - -export const componentSpecToText = (componentSpec: ComponentSpec) => { - return yaml.dump(componentSpec, { - lineWidth: 80, - noRefs: true, - indent: 2, - }); -}; - // TODO: Remove the upgrade code in several weeks. const upgradeSingleComponentListDb = async (listName: string) => { const componentListVersionKey = "component_list_format_version_" + listName; diff --git a/src/utils/yaml.ts b/src/utils/yaml.ts index 029d809f6..f6adf362b 100644 --- a/src/utils/yaml.ts +++ b/src/utils/yaml.ts @@ -1,7 +1,6 @@ import yaml from "js-yaml"; import { type ComponentSpec, isValidComponentSpec } from "./componentSpec"; -import { componentSpecToText } from "./componentStore"; const copyToYaml = ( spec: ComponentSpec, @@ -50,4 +49,16 @@ export function componentSpecFromYaml(yamlText: string): ComponentSpec { return loadedSpec; } +export const componentSpecToYaml = (componentSpec: ComponentSpec) => { + return yaml.dump(componentSpec, { lineWidth: 10000 }); +}; + +export const componentSpecToText = (componentSpec: ComponentSpec) => { + return yaml.dump(componentSpec, { + lineWidth: 80, + noRefs: true, + indent: 2, + }); +}; + export default copyToYaml;