From 8d36e2aa272d140efb368fd7fdc3464f2ac724d3 Mon Sep 17 00:00:00 2001 From: Felix Haeberle Date: Mon, 10 Feb 2025 11:47:54 +0100 Subject: [PATCH 1/2] adjust `saveProjectToDirectory` --- .../project/saveProjectToDirectory.test.ts | 41 +++++++++++++++++++ .../sdk/src/project/saveProjectToDirectory.ts | 30 +++++++------- 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/inlang/packages/sdk/src/project/saveProjectToDirectory.test.ts b/inlang/packages/sdk/src/project/saveProjectToDirectory.test.ts index 2001c75b09..1c6f7360fe 100644 --- a/inlang/packages/sdk/src/project/saveProjectToDirectory.test.ts +++ b/inlang/packages/sdk/src/project/saveProjectToDirectory.test.ts @@ -328,3 +328,44 @@ test("adds a gitignore file if it doesn't exist", async () => { ); expect(gitignore).toBe("cache"); }); + + test("uses exportFiles when both exportFiles and saveMessages are defined", async () => { + const exportFilesSpy = vi.fn().mockResolvedValue([]); + const saveMessagesSpy = vi.fn(); + const mockPlugin: InlangPlugin = { + key: "mock", + exportFiles: exportFilesSpy, + saveMessages: saveMessagesSpy, + }; + const volume = Volume.fromJSON({}); + const project = await loadProjectInMemory({ + blob: await newProject(), + providePlugins: [mockPlugin], + }); + await saveProjectToDirectory({ + path: "/foo/project.inlang", + fs: volume.promises as any, + project, + }); + expect(exportFilesSpy).toHaveBeenCalled(); + expect(saveMessagesSpy).not.toHaveBeenCalled(); + }); + + test("uses saveMessages when exportFiles is not defined", async () => { + const saveMessagesSpy = vi.fn().mockResolvedValue([]); + const mockPlugin: InlangPlugin = { + key: "mock", + saveMessages: saveMessagesSpy, + }; + const volume = Volume.fromJSON({}); + const project = await loadProjectInMemory({ + blob: await newProject(), + providePlugins: [mockPlugin], + }); + await saveProjectToDirectory({ + path: "/foo/project.inlang", + fs: volume.promises as any, + project, + }); + expect(saveMessagesSpy).toHaveBeenCalled(); + }); diff --git a/inlang/packages/sdk/src/project/saveProjectToDirectory.ts b/inlang/packages/sdk/src/project/saveProjectToDirectory.ts index f53588c9ea..0f3a26e508 100644 --- a/inlang/packages/sdk/src/project/saveProjectToDirectory.ts +++ b/inlang/packages/sdk/src/project/saveProjectToDirectory.ts @@ -48,19 +48,6 @@ export async function saveProjectToDirectory(args: { const settings = await args.project.settings.get(); for (const plugin of plugins) { - // old legacy remove with v3 - if (plugin.saveMessages) { - // in-efficient re-qeuery but it's a legacy function that will be removed. - // the effort of adjusting the code to not re-query is not worth it. - const bundlesNested = await selectBundleNested(args.project.db).execute(); - await plugin.saveMessages({ - messages: bundlesNested.map((b) => toMessageV1(b)), - // @ts-expect-error - legacy - nodeishFs: withAbsolutePaths(args.fs, args.path), - settings, - }); - } - if (plugin.exportFiles) { const bundles = await args.project.db .selectFrom("bundle") @@ -81,7 +68,10 @@ export async function saveProjectToDirectory(args: { settings, }); for (const file of files) { - const p = absolutePathFromProject(args.path, file.name); + const pathPattern = settings[plugin.key]?.pathPattern; + const p = pathPattern + ? pathPattern.replace(/\{(languageTag|locale)\}/g, file.locale) + : absolutePathFromProject(args.path, file.name); const dirname = path.dirname(p); if ((await args.fs.stat(dirname)).isDirectory() === false) { await args.fs.mkdir(dirname, { recursive: true }); @@ -106,5 +96,17 @@ export async function saveProjectToDirectory(args: { } } } + // old legacy remove with v3 + else if (plugin.saveMessages) { + // in-efficient re-qeuery but it's a legacy function that will be removed. + // the effort of adjusting the code to not re-query is not worth it. + const bundlesNested = await selectBundleNested(args.project.db).execute(); + await plugin.saveMessages({ + messages: bundlesNested.map((b) => toMessageV1(b)), + // @ts-expect-error - legacy + nodeishFs: withAbsolutePaths(args.fs, args.path), + settings, + }); + } } } From adf7d6c132bd8bb4bfe83cb2255976bcabe5ac37 Mon Sep 17 00:00:00 2001 From: Felix Haeberle Date: Tue, 11 Feb 2025 15:21:56 +0100 Subject: [PATCH 2/2] add changesets --- .changeset/swift-otters-prove.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/swift-otters-prove.md diff --git a/.changeset/swift-otters-prove.md b/.changeset/swift-otters-prove.md new file mode 100644 index 0000000000..b44d386179 --- /dev/null +++ b/.changeset/swift-otters-prove.md @@ -0,0 +1,5 @@ +--- +"@inlang/sdk": patch +--- + +fix `saveProjectToDirectory` to have proper backwards compatibility and respect `pathPattern` file location`