-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·134 lines (114 loc) · 3.49 KB
/
Copy pathindex.js
File metadata and controls
executable file
·134 lines (114 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env node
const { Command } = require('commander');
const fs = require('fs');
const path = require('path');
const program = new Command();
const NOTES_FILE = path.join(process.cwd(), 'notes.json');
const loadNotes = () => {
try {
const dataBuffer = fs.readFileSync(NOTES_FILE);
return JSON.parse(dataBuffer.toString());
} catch (error) {
return []; // Return an empty array if notes cannot be loaded
}
};
const saveNotes = (notes) => {
fs.writeFileSync(NOTES_FILE, JSON.stringify(notes, null, 2));
};
// Centralized logging function
const logMessage = (message, type = 'info') => {
const prefix = type === 'error' ? 'Error: ' : '';
console.log(`${prefix}${message}`);
};
// Input validation for index
const validateIndex = (index, notesLength) => {
const noteIndex = parseInt(index) - 1; // Convert index to zero-based
return noteIndex >= 0 && noteIndex < notesLength ? noteIndex : null;
};
// Add a new note
const addNote = (options) => {
if (!options.title || !options.content) {
logMessage('Both --title and --content are required.', 'error');
return;
}
const notes = loadNotes();
notes.push({ title: options.title, content: options.content });
saveNotes(notes);
logMessage('Note added successfully!');
};
program
.command('add')
.description('Add a new note with optional --title and --content')
.option('--title <title>', 'Title of the note')
.option('--content <content>', 'Content of the note')
.action(addNote);
// List all note titles
program
.command('list')
.description('List all note titles')
.action(() => {
const notes = loadNotes();
if (notes.length === 0) {
logMessage('No notes found.', 'error');
} else {
notes.forEach((note, index) => {
console.log(`${index + 1}. ${note.title}`);
});
}
});
// Show a single note by its index
program
.command('show <index>')
.description('Show a note by its index')
.action((index) => {
const notes = loadNotes();
const noteIndex = validateIndex(index, notes.length);
if (noteIndex !== null) {
console.log(`# ${notes[noteIndex].title}\n\n${notes[noteIndex].content}`);
} else {
logMessage('Note not found.', 'error');
}
});
// Edit a note
const editNote = (index, options) => {
const notes = loadNotes();
const noteIndex = validateIndex(index, notes.length);
if (noteIndex !== null) {
if (options.title) notes[noteIndex].title = options.title;
if (options.content) notes[noteIndex].content = options.content;
saveNotes(notes);
logMessage('Note updated successfully!');
} else {
logMessage('Note not found.', 'error');
}
};
program
.command('edit <index>')
.description('Edit a note by specifying what to change')
.option('--title <newTitle>', 'New title of the note')
.option('--content <newContent>', 'New content of the note')
.action(editNote);
// Delete a note
program
.command('delete <index>')
.description('Delete a note')
.action((index) => {
const notes = loadNotes();
const noteIndex = validateIndex(index, notes.length);
if (noteIndex !== null) {
notes.splice(noteIndex, 1);
saveNotes(notes);
logMessage('Note deleted successfully!');
} else {
logMessage('Note not found.', 'error');
}
});
// Clear all notes
program
.command('clear')
.description('Delete all notes')
.action(() => {
saveNotes([]); // Save an empty array to clear all notes
logMessage('All notes have been deleted!');
});
program.parse(process.argv);