Skip to content

Commit c3b6a04

Browse files
authored
fix(cli): save yaml with keys and values in quotes (#532)
1 parent b27abc5 commit c3b6a04

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

.changeset/thirty-birds-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"lingo.dev": patch
3+
---
4+
5+
save yaml with keys and values in quotes

packages/cli/src/cli/loaders/index.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,29 @@ user.password=Contraseña
12361236

12371237
expect(fs.writeFile).toHaveBeenCalledWith("i18n/es.yaml", expectedOutput, { encoding: "utf-8", flag: "w" });
12381238
});
1239+
1240+
describe("yaml with quoted keys and values", async () => {
1241+
it.each([
1242+
["double quoted values", `greeting: "Hello!"`, `greeting: "¡Hola!"`],
1243+
["double quoted keys", `"greeting": Hello!`, `"greeting": ¡Hola!`],
1244+
["double quoted keys and values", `"greeting": "Hello!"`, `"greeting": "¡Hola!"`],
1245+
])("should return correct value for %s", async (_, input, expectedOutput) => {
1246+
const payload = { greeting: "¡Hola!" };
1247+
1248+
mockFileOperations(input);
1249+
1250+
const yamlLoader = createBucketLoader("yaml", "i18n/[locale].yaml", {
1251+
isCacheRestore: false,
1252+
defaultLocale: "en",
1253+
});
1254+
yamlLoader.setDefaultLocale("en");
1255+
await yamlLoader.pull("en");
1256+
1257+
await yamlLoader.push("es", payload);
1258+
1259+
expect(fs.writeFile).toHaveBeenCalledWith("i18n/es.yaml", expectedOutput, { encoding: "utf-8", flag: "w" });
1260+
});
1261+
});
12391262
});
12401263

12411264
describe("yaml-root-key bucket loader", () => {
Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import YAML from "yaml";
1+
import YAML, { ToStringOptions } from "yaml";
22
import { ILoader } from "./_types";
33
import { createLoader } from "./_utils";
44

@@ -7,10 +7,45 @@ export default function createYamlLoader(): ILoader<string, Record<string, any>>
77
async pull(locale, input) {
88
return YAML.parse(input) || {};
99
},
10-
async push(locale, payload) {
10+
async push(locale, payload, originalInput) {
1111
return YAML.stringify(payload, {
1212
lineWidth: -1,
13+
defaultKeyType: getKeyType(originalInput),
14+
defaultStringType: getStringType(originalInput),
1315
});
1416
},
1517
});
1618
}
19+
20+
// check if the yaml keys are using double quotes or single quotes
21+
function getKeyType(yamlString: string | null): ToStringOptions["defaultKeyType"] {
22+
if (yamlString) {
23+
const lines = yamlString.split("\n");
24+
const hasDoubleQuotes = lines.find((line) => {
25+
return line.trim().startsWith('"') && line.trim().match('":');
26+
});
27+
if (hasDoubleQuotes) {
28+
return "QUOTE_DOUBLE";
29+
}
30+
}
31+
return "PLAIN";
32+
}
33+
34+
// check if the yaml string values are using double quotes or single quotes
35+
function getStringType(yamlString: string | null): ToStringOptions["defaultStringType"] {
36+
if (yamlString) {
37+
const lines = yamlString.split("\n");
38+
const hasDoubleQuotes = lines.find((line) => {
39+
const trimmedLine = line.trim();
40+
return (
41+
(trimmedLine.startsWith('"') || trimmedLine.match(/:\s*"/)) &&
42+
(trimmedLine.endsWith('"') || trimmedLine.endsWith('",'))
43+
);
44+
});
45+
console.log("hasDoubleQuotes", hasDoubleQuotes);
46+
if (hasDoubleQuotes) {
47+
return "QUOTE_DOUBLE";
48+
}
49+
}
50+
return "PLAIN";
51+
}

0 commit comments

Comments
 (0)