Skip to content

Commit cdfa9ad

Browse files
committed
search snippet function added. can filter on the basis of language, tags and any specific keywords
1 parent c8aeb17 commit cdfa9ad

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

index.js

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@ function loadSnippets(){
1818
return[];
1919
}
2020
const data =fs.readFileSync(DATA_FILE,'utf-8');
21-
return JSON.parse(data);
21+
22+
try{
23+
const parsed =JSON.parse(data);
24+
if(Array.isArray(parsed)){
25+
return parsed;
26+
}else{
27+
console.log(`Snippet DB is not an array`);
28+
return[];
29+
}
30+
}catch(err){
31+
console.error(`JSON not passed error`.err.message);
32+
return[];
33+
}
2234
}
2335

2436
//save snippets to JSON file
@@ -201,7 +213,7 @@ program
201213
})
202214
//ask cli user to copy code to clipbaord (y/n options)
203215
rl.question(`do you want to copy the snippet to your cli? (y/n): `,answer =>{
204-
if(answer.toLowerCase==='y'){
216+
if(answer.toLowerCase()==='y'){
205217
clipboardy.writeSync(code);
206218
console.log(`The snippet ${name} has been copied to your clipboard`);
207219
}else{
@@ -211,27 +223,29 @@ program
211223
})
212224
});
213225

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-
// })
226+
// search snippet
227+
program
228+
.command('search-snippet')
229+
.description('search for the snippet you are looking for on the basis of language,keywords and tags')
230+
.option('-l,--lang<language>','Filter on the basis of programming language')
231+
.option('-k,--key<keyword>','Filter on the basis of keywords used')
232+
.option(`--tags <tags>`, `New comma-separated tags`, val => val.split(`,`))
233+
234+
.action((options)=>{
235+
const snippets=loadSnippets();
236+
const matches=snippets.filter(snippet=>{
237+
const matchlang =options.lang? options.lang===snippet.lang:true;
238+
const matchtag =options.tag?snippet.tag?.includes(options.tag):true;
239+
const matchkey=options.keywords?fs.readFileSync(path.resolve(filePath),'utf-8').includes(options.keywords):true;
240+
241+
return matchlang && matchtag && matchkey;
242+
});
243+
if(!matches.length) return console.log("Snippet not found");
244+
matches.forEach((s,i)=>{
245+
console.log(`\n ${i+1}. ${s.name}[${s.lang}]`);
246+
console.log(`Tags: ${snippets.tags?.join(', ')}`);
247+
console.log(`File: ${s.file}`);
248+
console.log('-----------------------------');
249+
})
250+
})
237251
program.parse(process.argv);

0 commit comments

Comments
 (0)