quicknote-cli
Version:
A simple command-line tool to take quick notes directly in your terminal — now with persistent storage using JSON!
89 lines (77 loc) • 2.24 kB
JavaScript
import { Command } from "commander";
import chalk from "chalk";
import fs from "fs";
import path from "path";
const program = new Command();
const log = console.log;
const title = chalk.bold.underline.cyan;
const success = chalk.green;
const warning = chalk.yellow;
const danger = chalk.red;
const dim = chalk.gray;
const NOTES_FILE = path.resolve(process.cwd(), "notes.json");
// Utility to read notes
function readNotes() {
try {
const data = fs.readFileSync(NOTES_FILE, "utf8");
return JSON.parse(data);
} catch (err) {
return [];
}
}
// Utility to write notes
function writeNotes(notes) {
fs.writeFileSync(NOTES_FILE, JSON.stringify(notes, null, 2));
}
// Program config
program
.name('quicknote')
.description(chalk.magentaBright('CLI for simple quick note'))
.version('1.0.0')
.usage('<command> [options]')
.helpOption('-h, --help', chalk.blue('Show custom help'))
.addHelpText('beforeAll', `
${title('Welcome to QuickNote CLI')}
Take temporary notes in your terminal session.
${dim('Notes will be saved in notes.json in the current folder.')}
`)
.addHelpText('afterAll', `
Examples:
${chalk.green('quicknote add "Add your special package"')}
${chalk.green('quicknote ls')}
${chalk.green('quicknote list')}
`);
// Add note command
program
.command('add <note>')
.description('Add a new note')
.alias('a')
.action((note) => {
const notes = readNotes();
notes.push(note);
writeNotes(notes);
console.log(success(`Note added: "${note}"`));
});
// List notes command
program
.command('list')
.alias('ls')
.description('List all notes')
.action(() => {
const notes = readNotes();
if (notes.length === 0) {
console.log(warning('No notes added yet.'));
} else {
console.log(title('\nYour notes:'));
notes.forEach((note, index) => {
console.log(`${chalk.cyan(index + 1)}. ${note}`);
});
}
});
// Default: show help
program
.action(() => {
program.outputHelp();
});
program.parse(process.argv);