// 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") }