radashi-helper
Version:
Help with managing your own Radashi
185 lines (180 loc) • 4.68 kB
JavaScript
import {
stdio
} from "./chunk-GGEXN2BI.js";
import {
prompt
} from "./chunk-MJC327DM.js";
import {
EarlyExitError,
RadashiError,
exec,
forwardStderrAndRethrow
} from "./chunk-JLKIE3A2.js";
import {
proxied
} from "./chunk-VOEQFXI5.js";
// src/bot.ts
var bot = {
name: "Radashi Bot",
email: "175859458+radashi-bot@users.noreply.github.com"
};
async function botCommit(message, opts) {
if (opts.add.length > 0) {
await exec("git", ["add", ...opts.add], { cwd: opts.cwd }).catch(
forwardStderrAndRethrow
);
}
await exec(
"git",
["commit", "-m", message, "--author", `${bot.name} <${bot.email}>`],
{ cwd: opts.cwd, stdio }
);
}
// src/util/updateRadashiConfig.ts
import { readFileSync } from "node:fs";
import { writeFile } from "node:fs/promises";
async function updateRadashiConfig(env, newConfig) {
if (!env.configPath) {
throw new RadashiError("No radashi.json exists in this project");
}
const config = JSON.parse(readFileSync(env.configPath, "utf8"));
await writeFile(
env.configPath,
JSON.stringify({ ...config, ...newConfig }, null, 2)
);
Object.assign(env.config, newConfig);
}
// src/util/openInEditor.ts
var forcedEditor;
var openFile;
async function openInEditor(file, env, editor) {
if (openFile) {
return openFile(file);
}
editor ||= forcedEditor || env.config.editor;
if (editor?.[0] === "!") {
editor = editor.slice(1);
} else if (!forcedEditor) {
const displayNames = proxied((cmd) => {
switch (cmd) {
case "code":
return "VS Code";
case "code-insiders":
return "VS Code Insiders";
case "cursor":
return "Cursor";
case "vim":
return "Vim";
case "emacs":
return "Emacs";
case "sublime":
return "Sublime Text";
case "atom":
return "Atom";
}
return cmd;
});
const addAvailableEditors = async (choices) => {
if (process.env.EDITOR) {
choices.push({
title: `Open with $EDITOR (${process.env.EDITOR})`,
value: "$EDITOR"
});
}
for (const editor2 of [
"code",
"code-insiders",
"cursor",
"vim",
"emacs",
"sublime",
"atom"
]) {
try {
await exec("command", ["-v", editor2]);
choices.push({
title: `Open with ${displayNames[editor2]}`,
value: editor2
});
} catch {
}
}
choices.push({
title: "Open with custom command",
value: "custom"
});
};
while (true) {
const choices = [];
if (env.config.editor) {
choices.push({
title: `Open with ${displayNames[env.config.editor]}`,
value: env.config.editor
});
choices.push({
title: `Always open with ${displayNames[env.config.editor]}`,
value: "!" + env.config.editor
});
choices.push({
title: "Select another editor",
value: "other"
});
} else {
await addAvailableEditors(choices);
}
const response = await prompt({
type: "autocomplete",
name: "response",
message: "How would you like to open the file?",
choices
});
if (!response) {
throw new EarlyExitError("No editor selected. Exiting...");
}
if (response === "other") {
env.config.editor = void 0;
await addAvailableEditors(choices);
continue;
}
if (response === "custom") {
const customCommand = await prompt({
type: "text",
name: "customCommand",
message: "Enter the command to open the file:"
});
if (!customCommand) {
throw new EarlyExitError("No command provided. Exiting...");
}
editor = customCommand;
} else if (response[0] === "!") {
editor = response.slice(1);
} else if (response === "$EDITOR") {
editor = process.env.EDITOR;
} else {
editor = response;
}
forcedEditor = editor;
if (env.configPath) {
await updateRadashiConfig(env, {
editor: response === "custom" ? editor : response
});
await botCommit("chore: set preferred editor in radashi.json", {
cwd: env.root,
add: [env.configPath]
});
}
break;
}
}
if (editor) {
await exec(editor, [file], { stdio }).catch(forwardStderrAndRethrow);
}
}
function setFileOpener(handler) {
openFile = handler;
}
export {
botCommit,
openInEditor,
setFileOpener
};