Skip to content

Commit

Permalink
fix(parse): installation fix and additional entries for existing file
Browse files Browse the repository at this point in the history
  • Loading branch information
tobua committed Apr 21, 2024
1 parent dddde34 commit 76c2114
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
25 changes: 23 additions & 2 deletions helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 ?? []

Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -29,7 +31,8 @@
"bin": "./index.ts",
"files": [
"*.ts",
"configuration"
"configuration",
"*.md"
],
"keywords": [
"configuration",
Expand All @@ -48,6 +51,7 @@
"!test/fixture/*/package.json",
"!test/fixture/*/configuration.ts"
],
"license": "MIT",
"biome": "recommended",
"vscode": "recommended",
"typescript": {
Expand Down

0 comments on commit 76c2114

Please sign in to comment.