Skip to content
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/thirty-birds-cheer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"lingo.dev": patch
---

save yaml with keys and values in quotes
23 changes: 23 additions & 0 deletions packages/cli/src/cli/loaders/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,29 @@ user.password=Contraseña

expect(fs.writeFile).toHaveBeenCalledWith("i18n/es.yaml", expectedOutput, { encoding: "utf-8", flag: "w" });
});

describe("yaml with quoted keys and values", async () => {
it.each([
["double quoted values", `greeting: "Hello!"`, `greeting: "¡Hola!"`],
["double quoted keys", `"greeting": Hello!`, `"greeting": ¡Hola!`],
["double quoted keys and values", `"greeting": "Hello!"`, `"greeting": "¡Hola!"`],
])("should return correct value for %s", async (_, input, expectedOutput) => {
const payload = { greeting: "¡Hola!" };

mockFileOperations(input);

const yamlLoader = createBucketLoader("yaml", "i18n/[locale].yaml", {
isCacheRestore: false,
defaultLocale: "en",
});
yamlLoader.setDefaultLocale("en");
await yamlLoader.pull("en");

await yamlLoader.push("es", payload);

expect(fs.writeFile).toHaveBeenCalledWith("i18n/es.yaml", expectedOutput, { encoding: "utf-8", flag: "w" });
});
});
});

describe("yaml-root-key bucket loader", () => {
Expand Down
39 changes: 37 additions & 2 deletions packages/cli/src/cli/loaders/yaml.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import YAML from "yaml";
import YAML, { ToStringOptions } from "yaml";
import { ILoader } from "./_types";
import { createLoader } from "./_utils";

Expand All @@ -7,10 +7,45 @@ export default function createYamlLoader(): ILoader<string, Record<string, any>>
async pull(locale, input) {
return YAML.parse(input) || {};
},
async push(locale, payload) {
async push(locale, payload, originalInput) {
return YAML.stringify(payload, {
lineWidth: -1,
defaultKeyType: getKeyType(originalInput),
defaultStringType: getStringType(originalInput),
});
},
});
}

// check if the yaml keys are using double quotes or single quotes
function getKeyType(yamlString: string | null): ToStringOptions["defaultKeyType"] {
if (yamlString) {
const lines = yamlString.split("\n");
const hasDoubleQuotes = lines.find((line) => {
return line.trim().startsWith('"') && line.trim().match('":');
});
if (hasDoubleQuotes) {
return "QUOTE_DOUBLE";
}
}
return "PLAIN";
}

// check if the yaml string values are using double quotes or single quotes
function getStringType(yamlString: string | null): ToStringOptions["defaultStringType"] {
if (yamlString) {
const lines = yamlString.split("\n");
const hasDoubleQuotes = lines.find((line) => {
const trimmedLine = line.trim();
return (
(trimmedLine.startsWith('"') || trimmedLine.match(/:\s*"/)) &&
(trimmedLine.endsWith('"') || trimmedLine.endsWith('",'))
);
});
console.log("hasDoubleQuotes", hasDoubleQuotes);
if (hasDoubleQuotes) {
return "QUOTE_DOUBLE";
}
}
return "PLAIN";
}