Skip to content

Commit

Permalink
Merge pull request #3390 from opral/sherl-159-adjust-saveprojecttodir…
Browse files Browse the repository at this point in the history
…ectory

fix `saveProjectToDirectory`
  • Loading branch information
felixhaeberle authored Feb 11, 2025
2 parents 5e0abf0 + adf7d6c commit e0cb1a5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-otters-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@inlang/sdk": patch
---

fix `saveProjectToDirectory` to have proper backwards compatibility and respect `pathPattern` file location`
41 changes: 41 additions & 0 deletions inlang/packages/sdk/src/project/saveProjectToDirectory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
30 changes: 16 additions & 14 deletions inlang/packages/sdk/src/project/saveProjectToDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 });
Expand All @@ -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,
});
}
}
}

0 comments on commit e0cb1a5

Please sign in to comment.