codetainer
Version:
A clean and simple CLI to manage and store code snippets with ease.
39 lines (32 loc) • 1.23 kB
JavaScript
import fs from "fs";
import chalk from "chalk";
import { loadSnippets } from "../utils/snippetStore.js";
import { fileExtensions } from "../data/extension.js";
export function registerExportAllCommand(program) {
program
.command("export-all")
.description(
"Export all snippets to files, optionally filtered by language"
)
.option("-l, --language <language>", "Filter by language")
.action((options) => {
const snippets = loadSnippets();
const extMap = fileExtensions;
const entries = Object.entries(snippets).filter(([_, data]) =>
options.language
? data.language?.toLowerCase() === options.language.toLowerCase()
: true
);
if (!entries.length) {
console.log(chalk.yellow("No snippets matched your filter."));
return;
}
entries.forEach(([name, data]) => {
const ext = extMap[data.language?.toLowerCase()] || "txt";
const fileName = `${name}.${ext}`;
fs.writeFileSync(fileName, data.code);
console.log(chalk.green(`💾 Exported ${fileName}`));
});
console.log(chalk.green(`✅ Exported ${entries.length} snippet(s).`));
});
}