Edit global .gitignore file and configure git accordingly if it doesn't exist

Open edit-global-gitignore in Script Kit

It may make sense to unignore the db/_edit-global-gitignore.json database file if you want to share your global gitignore between computers (that share the same .kenv).

As a JetBrains user, this script comes in very handy when needing to ignore some of the Plugin config files that are automatically created in the .idea folder:

**/.idea/GitLink.xml
**/.idea/deploymentTargetDropDown.xml
**/.idea/gbrowser_project.xml
**/.idea/highlightedFiles.xml
**/.idea/discord.xml
**/.idea/developer-tools.xml
**/.idea/CustomInspectionsConfig.xml
// Name: Edit global .gitignore
// Description: Opens an editor with the global .gitignore file and sets it up if it doesn't exist
import "@johnlindquist/kit"
import { writeFile } from "node:fs/promises"
import { startSpinner } from "@josxa/kit-utils"
const cache = await db({ defaultEntries: [] as string[] })
const DESIRED_IGNORE_PATH = home(".global.gitignore")
const ensureGlobalGitIgnorePathConfigured = async () => {
try {
const existing = await exec("git config --global core.excludesfile")
debugger
if (existing.stdout.toString() !== DESIRED_IGNORE_PATH) {
await exec(`git config --global core.excludesfile "${DESIRED_IGNORE_PATH}"`)
await div(`Global gitconfig file configured to be at ${DESIRED_IGNORE_PATH}`)
}
} catch (err) {
await exec(`git config --global core.excludesfile "${DESIRED_IGNORE_PATH}"`)
await div(`Global gitconfig file configured to be at ${DESIRED_IGNORE_PATH}`)
}
}
await ensureGlobalGitIgnorePathConfigured()
const content = await ensureReadFile(DESIRED_IGNORE_PATH, "", { encoding: "utf-8" })
const updated = ensureDefaultEntriesPresent(content).trim()
if (updated !== content) {
await writeFile(DESIRED_IGNORE_PATH, updated, { encoding: "utf-8" })
await div("Inserted default entries and wrote to file!")
}
const edited = (await editor({ value: updated })).trim()
if (edited !== updated) {
await writeFile(DESIRED_IGNORE_PATH, edited, { encoding: "utf-8" })
startSpinner("dots", { initialMessage: "Writing..." })
await wait(1200)
await submit("done")
}
cache.defaultEntries = edited.split("\n")
await cache.write()
function ensureDefaultEntriesPresent(content: string): string {
const lines = content.split("\n")
for (const x of cache.defaultEntries) {
if (!lines.includes(x)) {
lines.unshift(x)
}
}
return lines.join("\n")
}