diff --git a/helper.ts b/helper.ts index ef0c742..dad4ba0 100644 --- a/helper.ts +++ b/helper.ts @@ -3,6 +3,7 @@ import { join } from 'node:path' import { it } from 'avait' import Bun from 'bun' import { create } from 'logua' +import { parse } from 'parse-gitignore' import { z } from 'zod' import { configurations, ignore } from './configuration' import { state } from './state' @@ -42,6 +43,23 @@ export async function findConfiguration() { state.options = userConfiguration } +async function addAdditionalGitignoreEntries(file: { name: string; contents: string }) { + const addedIgnores: string[] = [] + const existingFileContents = await Bun.file(file.name).text() + const { patterns: existingIgnores } = parse(existingFileContents) + const { patterns: updatedIgnores } = parse(file.contents) + + for (const pattern of updatedIgnores) { + if (!(existingIgnores.includes(pattern) || existingIgnores.includes(`!${pattern}`))) { + addedIgnores.push(pattern) + } + } + + if (addedIgnores.length) { + await Bun.write(file.name, `${existingFileContents}${existingFileContents.endsWith('\n') ? '' : '\n'}${addedIgnores.join('\n')}\n`) + } +} + export async function writeGitIgnore(ignores: string[]) { let userIgnores = state.options.ignore ?? state.options.gitignore ?? [] @@ -53,8 +71,11 @@ export async function writeGitIgnore(ignores: string[]) { userIgnores.push(...ignores) } - if (!existsSync('./.gitignore')) { - const file = ignore.createFile(userIgnores as string[]) + const file = ignore.createFile(userIgnores as string[]) + + if (existsSync(file.name)) { + await addAdditionalGitignoreEntries(file) + } else { await Bun.write(file.name, file.contents) } } diff --git a/index.ts b/index.ts index 9d7eb27..0b3fdf8 100644 --- a/index.ts +++ b/index.ts @@ -7,7 +7,7 @@ import { state } from './state' const ignores: string[] = [] -findConfiguration() +await findConfiguration() for (const { name, alias, configuration } of configurations) { const value = state.options[name] ?? (alias && state.options[alias]) diff --git a/package.json b/package.json index 66fd205..70fb8c7 100644 --- a/package.json +++ b/package.json @@ -14,11 +14,13 @@ "dependencies": { "avait": "^1.0.0", "logua": "^3.0.3", + "parse-gitignore": "^2.0.0", "zod": "^3.22.5" }, "devDependencies": { "@biomejs/biome": "^1.7.0", "@types/bun": "^1.1.0", + "@types/parse-gitignore": "^1.0.2", "eslint-config-airbnb": "^19.0.4", "typescript": "^5.4.5" }, @@ -29,7 +31,8 @@ "bin": "./index.ts", "files": [ "*.ts", - "configuration" + "configuration", + "*.md" ], "keywords": [ "configuration", @@ -48,6 +51,7 @@ "!test/fixture/*/package.json", "!test/fixture/*/configuration.ts" ], + "license": "MIT", "biome": "recommended", "vscode": "recommended", "typescript": {