radashi-helper
Version:
Help with managing your own Radashi
185 lines (166 loc) • 5.75 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
var _chunk27XA2ZBLcjs = require('./chunk-27XA2ZBL.cjs');
var _chunk4WZBZWYDcjs = require('./chunk-4WZBZWYD.cjs');
var _chunkST3J6QTNcjs = require('./chunk-ST3J6QTN.cjs');
var _chunk5R2KEZLQcjs = require('./chunk-5R2KEZLQ.cjs');
// 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 _chunkST3J6QTNcjs.exec.call(void 0, "git", ["add", ...opts.add], { cwd: opts.cwd }).catch(
_chunkST3J6QTNcjs.forwardStderrAndRethrow
);
}
await _chunkST3J6QTNcjs.exec.call(void 0,
"git",
["commit", "-m", message, "--author", `${bot.name} <${bot.email}>`],
{ cwd: opts.cwd, stdio: _chunk27XA2ZBLcjs.stdio }
);
}
// src/util/updateRadashiConfig.ts
var _fs = require('fs');
var _promises = require('fs/promises');
async function updateRadashiConfig(env, newConfig) {
if (!env.configPath) {
throw new (0, _chunkST3J6QTNcjs.RadashiError)("No radashi.json exists in this project");
}
const config = JSON.parse(_fs.readFileSync.call(void 0, env.configPath, "utf8"));
await _promises.writeFile.call(void 0,
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 (_optionalChain([editor, 'optionalAccess', _ => _[0]]) === "!") {
editor = editor.slice(1);
} else if (!forcedEditor) {
const displayNames = _chunk5R2KEZLQcjs.proxied.call(void 0, (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 _chunkST3J6QTNcjs.exec.call(void 0, "command", ["-v", editor2]);
choices.push({
title: `Open with ${displayNames[editor2]}`,
value: editor2
});
} catch (e) {
}
}
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 _chunk4WZBZWYDcjs.prompt.call(void 0, {
type: "autocomplete",
name: "response",
message: "How would you like to open the file?",
choices
});
if (!response) {
throw new (0, _chunkST3J6QTNcjs.EarlyExitError)("No editor selected. Exiting...");
}
if (response === "other") {
env.config.editor = void 0;
await addAvailableEditors(choices);
continue;
}
if (response === "custom") {
const customCommand = await _chunk4WZBZWYDcjs.prompt.call(void 0, {
type: "text",
name: "customCommand",
message: "Enter the command to open the file:"
});
if (!customCommand) {
throw new (0, _chunkST3J6QTNcjs.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 _chunkST3J6QTNcjs.exec.call(void 0, editor, [file], { stdio: _chunk27XA2ZBLcjs.stdio }).catch(_chunkST3J6QTNcjs.forwardStderrAndRethrow);
}
}
function setFileOpener(handler) {
openFile = handler;
}
exports.botCommit = botCommit; exports.openInEditor = openInEditor; exports.setFileOpener = setFileOpener;