-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
159 lines (138 loc) · 4.85 KB
/
index.js
File metadata and controls
159 lines (138 loc) · 4.85 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env node
const { Command } = require('commander');
const inquirer = require('inquirer');
const fs = require('fs');
const path = process.env.SNIPPETS_PATH || './snippets.json';
const program = new Command();
if (!fs.existsSync(path)) {
fs.writeFileSync(path, JSON.stringify([]));
}
const loadSnippets = () => {
return JSON.parse(fs.readFileSync(path));
};
const saveSnippets = (snippets) => {
fs.writeFileSync(path, JSON.stringify(snippets, null, 2));
};
const getCodeFromFile = async () => {
const { filePath } = await inquirer.prompt([
{ type: 'input', name: 'filePath', message: 'Enter the file path of your code snippet:' }
]);
if (!fs.existsSync(filePath)) {
console.log('File does not exist. Please check the file path and try again.');
process.exit(1);
}
return fs.readFileSync(filePath, 'utf8');
};
program
.command('add')
.description('Add a new code snippet')
.action(async () => {
try {
const answers = await inquirer.prompt([
{ type: 'input', name: 'title', message: 'Snippet Title:' },
{ type: 'input', name: 'description', message: 'Snippet Description:' },
{ type: 'input', name: 'language', message: 'Language:' },
{ type: 'input', name: 'tags', message: 'Tags (comma-separated):' },
]);
const code = await getCodeFromFile();
const snippets = loadSnippets();
const newSnippet = {
id: snippets.length ? snippets[snippets.length - 1].id + 1 : 1,
...answers,
code,
tags: answers.tags.split(',').map(tag => tag.trim())
};
snippets.push(newSnippet);
saveSnippets(snippets);
console.log('Snippet added successfully!');
} catch (error) {
console.error('Error adding snippet:', error);
}
});
program
.command('list')
.description('List all code snippets')
.action(() => {
try {
const snippets = loadSnippets();
const formattedSnippets = snippets.map(({ id, title, description, language, tags, code }) => ({
ID: id,
Title: title,
Description: description,
Language: language,
Tags: tags.join(', ')
}));
console.table(formattedSnippets);
snippets.forEach(({ id, code }) => {
console.log(`\nCode for Snippet ID ${id}:\n`);
console.log(code);
console.log('---------------------------------------------');
});
} catch (error) {
console.error('Error listing snippets:', error);
}
});
program
.command('search <tag>')
.description('Search for snippets by tag')
.action((tag) => {
try {
const snippets = loadSnippets();
const filtered = snippets.filter(snippet => snippet.tags.includes(tag));
const formattedSnippets = filtered.map(({ id, title, description, language, tags }) => ({
ID: id,
Title: title,
Description: description,
Language: language,
Tags: tags.join(', ')
}));
console.table(formattedSnippets);
filtered.forEach(({ id, code }) => {
console.log(`\nCode for Snippet ID ${id}:\n`);
console.log(code);
console.log('---------------------------------------------');
});
} catch (error) {
console.error('Error searching snippets:', error);
}
});
program
.command('delete <id>')
.description('Delete a snippet by ID')
.action((id) => {
try {
let snippets = loadSnippets();
snippets = snippets.filter(snippet => snippet.id !== parseInt(id, 10));
saveSnippets(snippets);
console.log('Snippet deleted successfully!');
} catch (error) {
console.error('Error deleting snippet:', error);
}
});
program
.command('edit <id>')
.description('Edit a snippet by ID')
.action(async (id) => {
try {
const snippets = loadSnippets();
const snippet = snippets.find(snippet => snippet.id === parseInt(id, 10));
if (!snippet) {
console.log('Snippet not found');
return;
}
const answers = await inquirer.prompt([
{ type: 'input', name: 'title', message: 'New Snippet Title:', default: snippet.title },
{ type: 'input', name: 'description', message: 'New Snippet Description:', default: snippet.description },
{ type: 'input', name: 'language', message: 'New Language:', default: snippet.language },
{ type: 'input', name: 'tags', message: 'New Tags (comma-separated):', default: snippet.tags.join(', ') },
]);
const code = await getCodeFromFile();
const index = snippets.findIndex(snippet => snippet.id === parseInt(id, 10));
snippets[index] = { id: parseInt(id, 10), ...answers, code, tags: answers.tags.split(',').map(tag => tag.trim()) };
saveSnippets(snippets);
console.log('Snippet updated successfully!');
} catch (error) {
console.error('Error editing snippet:', error);
}
});
program.parse(process.argv);