Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix saveProjectToDirectory #3390

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
});
}
}
}