codetainer
Version:
A clean and simple CLI to manage and store code snippets with ease.
47 lines (39 loc) • 1.32 kB
JavaScript
import inquirer from "inquirer";
import chalk from "chalk";
import { loadSnippets, saveSnippets } from "../utils/snippetStore.js";
export function registerEditCommand(program) {
program
.command("edit <name>")
.description("Edit an existing snippet")
.option("-c, --code", "Edit the code content")
.option("-l, --language <language>", "Change the snippet's language")
.option("-t, --tags <tags...>", "Update tags (space-separated list)")
.action(async (name, options) => {
const snippets = loadSnippets();
if (!snippets[name]) {
console.log(chalk.red(`Snippet "${name}" not found.`));
return;
}
const snippet = snippets[name];
if (options.code) {
const { newCode } = await inquirer.prompt([
{
type: "editor",
name: "newCode",
message: "Edit the code:",
default: snippet.code,
},
]);
snippet.code = newCode;
}
if (options.language) {
snippet.language = options.language;
}
if (options.tags) {
snippet.tags = options.tags;
}
snippets[name] = snippet;
saveSnippets(snippets);
console.log(chalk.green(`Snippet "${name}" updated.`));
});
}