Created
December 5, 2024 17:54
-
-
Save JosXa/c4541fb6814a1f20d0078d0a998da1e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "@johnlindquist/kit" | |
import { startSpinner } from "@josxa/kit-utils" | |
import notifier from "node-notifier" | |
import { getKenvs } from "../../.kit" | |
metadata = { | |
name: "Sync Kenvs", | |
description: "git add & commit & rebase & push safely", | |
schedule: "0 */12 * * *", // every 12 hours | |
} | |
const log = (...args: string[]) => console.log(...args) | |
const kenvNames = [...(await getKenvs()).map((x) => path.parse(x).name), "main"] | |
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: "[email protected]" }, | |
}) | |
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...`, error) | |
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