Skip to content

Commit 3f2b766

Browse files
committed
feat: add cache file to gitignore
1 parent 842b8f1 commit 3f2b766

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,4 @@ yarn-error.log*
3636
# Misc
3737
.DS_Store
3838
*.pem
39+
i18n.cache

packages/cli/src/cli/cmd/i18n.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { createTwoFilesPatch } from "diff";
1616
import inquirer from "inquirer";
1717
import externalEditor from "external-editor";
1818
import { cacheChunk, deleteCache, getNormalizedCache } from "../utils/cache";
19+
import updateGitignore from "../utils/update-gitignore";
1920

2021
export default new Command()
2122
.command("i18n")
@@ -32,6 +33,8 @@ export default new Command()
3233
.option("--debug", "Debug mode")
3334
.option("--strict", "Stop on first error")
3435
.action(async function (options) {
36+
updateGitignore();
37+
3538
const ora = Ora();
3639
const flags = parseFlags(options);
3740

packages/cli/src/cli/cmd/init.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { getSettings, saveSettings } from "../utils/settings";
1212
import { createAuthenticator } from "../utils/auth";
1313
import findLocaleFiles from "../utils/find-locale-paths";
1414
import { ensurePatterns } from "../utils/ensure-patterns";
15+
import updateGitignore from "../utils/update-gitignore";
1516

1617
const openUrl = (path: string) => {
1718
const settings = getSettings(undefined);
@@ -200,6 +201,8 @@ export default new InteractiveCommand()
200201
Ora().succeed(`Authenticated as ${auth.email}`);
201202
}
202203

204+
updateGitignore();
205+
203206
if (!isInteractive) {
204207
Ora().info("Please see https://docs.lingo.dev/");
205208
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import fs from "fs";
2+
import path from "path";
3+
4+
export default function updateGitignore() {
5+
const cacheFile = "i18n.cache";
6+
const projectRoot = findCurrentProjectRoot();
7+
if (!projectRoot) {
8+
return;
9+
}
10+
const gitignorePath = path.join(projectRoot, ".gitignore");
11+
const gitignore = fs.readFileSync(gitignorePath, "utf8").split("\n");
12+
const cacheIsIgnored = gitignore.includes(cacheFile);
13+
14+
if (!cacheIsIgnored) {
15+
let content = "";
16+
17+
// Ensure there's a trailing newline
18+
if (fs.existsSync(gitignorePath)) {
19+
content = fs.readFileSync(gitignorePath, "utf8");
20+
if (content !== "" && !content.endsWith("\n")) {
21+
content += "\n";
22+
}
23+
}
24+
25+
content += `${cacheFile}\n`;
26+
fs.writeFileSync(gitignorePath, content);
27+
}
28+
}
29+
30+
function findCurrentProjectRoot() {
31+
let currentDir = process.cwd();
32+
while (currentDir !== path.parse(currentDir).root) {
33+
const gitDirPath = path.join(currentDir, ".git");
34+
if (fs.existsSync(gitDirPath) && fs.lstatSync(gitDirPath).isDirectory()) {
35+
return currentDir;
36+
}
37+
currentDir = path.dirname(currentDir);
38+
}
39+
return null;
40+
}

0 commit comments

Comments
 (0)