Skip to content

Commit

Permalink
feat: add gitignore service
Browse files Browse the repository at this point in the history
  • Loading branch information
mahyarmirrashed committed Jul 2, 2023
1 parent 07dda62 commit d512a3d
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/services/gitignoreService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { GitService } from "@/services/gitService";
import { promises as fs } from "fs";
import { resolve } from "path";

const GITIGNORE_FILE_NAME = ".gitignore";
const OBSIDIAN_FOLDER_NAME = ".obsidian/";

const GITIGNORE_LINE = "\n# ignore obsidian vault state\n.obsidian/\n";

export class GitignoreService {
private gitignorePath: string;

constructor(private basePath: string, private gitService: GitService) {
this.gitignorePath = resolve(basePath, GITIGNORE_FILE_NAME);
}

private async ensureGitignoreExists() {
try {
await fs.access(this.gitignorePath);
} catch {
await fs.writeFile(this.gitignorePath, "");
}
}

async ensureObsidianIgnored(): Promise<void> {
const obsidianFolderPath = resolve(this.basePath, OBSIDIAN_FOLDER_NAME);

if (await this.gitService.isPathTracked(obsidianFolderPath)) {
await this.gitService.removePathFromHistory(`${OBSIDIAN_FOLDER_NAME}*`);

await this.ensureGitignoreExists();
await fs.appendFile(this.gitignorePath, GITIGNORE_LINE);
}
}
}

0 comments on commit d512a3d

Please sign in to comment.