Skip to content

Instantly share code, notes, and snippets.

@JosXa
Created June 25, 2024 15:51
Show Gist options
  • Save JosXa/99fe775f46a443f6d7421e69e234e483 to your computer and use it in GitHub Desktop.
Save JosXa/99fe775f46a443f6d7421e69e234e483 to your computer and use it in GitHub Desktop.
import "@johnlindquist/kit"
import { startSpinner } from "@josxa/kit-utils"
import notifier from "node-notifier"
import { getKenvs } from "../../.kit"
export const metadata: Metadata = {
name: "Sync Kenvs",
description: "git add & commit & rebase & push safely",
schedule: "0 */12 * * *", // every 12 hours
}
// const log = (...args: string[]) => console.log(...args)
// biome-ignore lint/complexity/noVoid: <explanation>
const log = (...args: string[]) => void 0
const kenvNames = (await getKenvs()).map((x) => path.parse(x).name)
const KENVS_TO_PUSH = (
[
"main",
"jetbrains-configs",
"metadata-parser",
"chrome-bookmarks",
"git-repo-explorer",
"prompt-decision-tree",
"tv-kenv",
// "kitgpt",
] as const satisfies Array<"main" | string>
).filter((kenv) => kenvNames.includes(kenv))
let anyNotifications = false
const spinner = startSpinner("spaceX", {}, { alwaysOnTop: false })
let step = 0
let totalSteps = KENVS_TO_PUSH.length * 5
const incrementProgress = () => {
step++
spinner.progress = Math.round((step / totalSteps) * 100)
}
const promises = KENVS_TO_PUSH.map((kenvName) =>
(async () => {
const path = kenvName === "main" ? kenvPath() : kenvPath("kenvs", kenvName)
if (!(await pathExists(path))) {
log(`Skipping ${kenvName} (does not exist)...`)
totalSteps -= 5
return
}
log(`Adding (${kenvName})...`)
await exec("git add -A", { cwd: path })
incrementProgress()
const message = await fetch("https://whatthecommit.com/index.txt").then((x) => x.text())
log(`Committing (${kenvName})...`)
await git.commit(path, message, {
author: { name: "JosXa", email: "info@josxa.dev" },
})
incrementProgress()
log(`Fetching (${kenvName})...`)
await exec("git fetch origin", { cwd: path })
incrementProgress()
try {
log(`Rebasing (${kenvName})...`)
await exec("git rebase origin/main", { cwd: path })
} catch (error) {
console.warn(`Rebase conflicts detected (${kenvName}), cancelling push...`)
notifier.notify(
{
title: "Conflicts detected",
message: `There are conflicts in the ${kenvName} kenv that must be resolved manually.`,
actions: ["Open in Webstorm"],
timeout: 15,
},
async (_, action) => {
if (action === "open in webstorm") {
await exec("webstorm.cmd .", { cwd: path })
}
},
)
anyNotifications = true
await exec("git rebase --abort", { cwd: path })
return
} finally {
incrementProgress()
}
log(`Pushing (${kenvName})...`)
await exec("git push origin main", { cwd: path })
incrementProgress()
})(),
)
await Promise.allSettled(promises)
await hide()
if (anyNotifications) {
await wait(15000)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment