Skip to content

Commit c8aeb17

Browse files
committed
Added view and list snippet. currently working on search snippet (commented out). also storing snippets locally in snippet.json
1 parent 0f0bd98 commit c8aeb17

File tree

4 files changed

+435
-10
lines changed

4 files changed

+435
-10
lines changed

index.js

Lines changed: 87 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import { Command } from 'commander';
44
import readline from 'readline';
5-
import fs from 'fs';
5+
import fs, { read } from 'fs';
66
import path, { resolve } from 'path';
7+
import clipboardy from 'clipboardy';
78
import { json } from 'stream/consumers';
89
// const {PrismaClient}=require(`@prisma/client`);
910
const DATA_FILE=path.resolve('./snippet.json');
@@ -50,13 +51,21 @@ program
5051
.option(`-f, --file <path>`, `Path to the file containing the snippet`)
5152
.option(`--tags <tags>`, `Comma-separated tags`, val => val.split(`,`))
5253
.action((name, options) => {
53-
console.log(`Name:`, name);
54-
console.log(`Language:`, options.lang);
55-
console.log(`Description:`, options.desc);
56-
console.log(`File Path:`, options.file);
57-
console.log(`Tags:`, options.tags);
58-
});
54+
const snippets = loadSnippets();
55+
56+
const newSnippet = {
57+
name,
58+
lang: options.lang || '',
59+
desc: options.desc || '',
60+
file: options.file || '',
61+
tags: options.tags || [],
62+
};
5963

64+
snippets.push(newSnippet);
65+
savedSnippets(snippets);
66+
67+
console.log(` Snippet "${name}" saved successfully.`);
68+
});
6069

6170
//delete snippet
6271
program
@@ -104,7 +113,7 @@ program
104113
.option(`-l, --lang <language>`, `New programming language`)
105114
.option(`-d, --desc <description>`, `New description`)
106115
.option(`-f, --file <path>`, `Path to the file to overwrite`) // ✅ Fix is here
107-
.option(`--tags <tags>`, `New comma-separated tags`, val => val.split(`,`))
116+
.option(`--tags <tags>`, `New comma-separated tags`, val => val.split(`,`))
108117
.action((name, options) => {
109118
if (!options.file) {
110119
console.error(`Please provide the file path using -f or --file`);
@@ -155,5 +164,74 @@ snippets.forEach((snip,index) =>{
155164
});
156165

157166

158-
//view snippet pending
167+
//view snippet
168+
program
169+
.command('view-snippet <name>')
170+
.description('view specific snippets')
171+
.action((name)=>{
172+
const snippets= loadSnippets();
173+
const snippet=snippets.find(s=>s.name===name);
174+
175+
if(!snippet){
176+
console.log(`ERROR! snippets not found`);
177+
process.exit(1);
178+
}
179+
180+
console.log(`\n Snippet details:`);
181+
console.log(`Snippet Name: ${snippet.name}`);
182+
console.log(`Snippet Language: ${snippet.lang}`);
183+
console.log(`Snippet Description: ${snippet.desc}`);
184+
console.log(`Snippet Path: ${snippet.file}`);
185+
console.log(`Snippet Tags: ${snippet.tags?.join(',')||'None'}`);
186+
console.log(`\n Code:`);
187+
188+
//read and display code
189+
const filePath=path.resolve(snippet.file);
190+
if(!fs.existsSync(filePath)){
191+
console.log(`ERROR! File path ${filePath} not found`);
192+
process.exit(1);
193+
}
194+
const code= fs.readFileSync(filePath,'utf-8');
195+
console.log(code);
196+
197+
//reading the snippet and storing it
198+
const rl =readline.createInterface({
199+
input:process.stdin,
200+
output:process.stdout
201+
})
202+
//ask cli user to copy code to clipbaord (y/n options)
203+
rl.question(`do you want to copy the snippet to your cli? (y/n): `,answer =>{
204+
if(answer.toLowerCase==='y'){
205+
clipboardy.writeSync(code);
206+
console.log(`The snippet ${name} has been copied to your clipboard`);
207+
}else{
208+
console.log(`The snippet ${name} has not been copied to your clipboard. exiting view snippet mode.`);
209+
}
210+
rl.close();
211+
})
212+
});
213+
214+
//search snippet
215+
// program
216+
// .command('search-snippet')
217+
// .description('search for the snippet you are looking for on the basis of language,keywords and tags')
218+
// .option('-l,--lang<language>','Filter on the basis of programming language')
219+
// .option('-k,--key<keyword>','Filter on the basis of keywords used')
220+
// .option(`--tags <tags>`, `New comma-separated tags`, val => val.split(`,`))
221+
222+
// action((options)=>{
223+
// const snippets=loadSnippets;
224+
// const matches=snippets.filter(snippet=>{
225+
// const matchlang =options.lang? options.lang===snippet.lang:true;
226+
// const matchtag =options.tag?snippet.tag?.includes(options.tag):true;
227+
// const matchkey=options.keywords?fs.readFileSync(path.resolve(filePath),'utf-8').includes(options.keywords):true;
228+
229+
// return matchlang && matchtag && matchkey;
230+
// });
231+
// if(!matches.length) return console.log("Snippet not found");
232+
// matches.forEach((s,i)=>{
233+
// console.log(`\n ${s+1}.${name}[${s.lang}]`);
234+
// console.log(`Tags: `);
235+
// })
236+
// })
159237
program.parse(process.argv);

0 commit comments

Comments
 (0)