33import { Command } from 'commander' ;
44import readline from 'readline' ;
55import fs from 'fs' ;
6- import path from 'path' ;
7- // const {PrismaClient}=require('@prisma/client');
6+ import path , { resolve } from 'path' ;
7+ import { json } from 'stream/consumers' ;
8+ // const {PrismaClient}=require(`@prisma/client`);
9+ const DATA_FILE = path . resolve ( './snippet.json' ) ;
810
911const program = new Command ( ) ;
1012// const prisma = new PrismaClient();
1113
14+ //for loading snippet from JSON file
15+ function loadSnippets ( ) {
16+ if ( ! fs . existsSync ( DATA_FILE ) ) {
17+ return [ ] ;
18+ }
19+ const data = fs . readFileSync ( DATA_FILE , 'utf-8' ) ;
20+ return JSON . parse ( data ) ;
21+ }
1222
23+ //save snippets to JSON file
24+ function savedSnippets ( snippets ) {
25+ fs . writeFileSync ( DATA_FILE , JSON . stringify ( snippets , null , 2 ) , 'utf-8' )
26+ }
27+
28+
29+ //cli code starts here
1330program
1431. name ( 'snippet-cli' )
1532. description ( 'My Code Snippet CLI program' )
@@ -18,37 +35,37 @@ program
1835
1936//greeting snippet
2037program
21- . command ( ' hi <name>' )
22- . description ( ' Says hi to the user' )
38+ . command ( ` hi <name>` )
39+ . description ( ` Says hi to the user` )
2340. action ( ( name ) => {
2441 console . log ( `Hello ${ name } , Welcome to the CLI: what are we here for today:` ) ;
2542} ) ;
2643
2744//add snippet
2845program
29- . command ( ' add-snippet <name>' )
30- . description ( ' Add your code snippet in the language of your choice' )
31- . option ( ' -l, --lang <language>' , ' Specify the programming language' )
32- . option ( ' -d, --desc <description>' , ' Describe the code snippet' )
33- . option ( ' -f, --file <path>' , ' Path to the file containing the snippet' )
34- . option ( ' --tags <tags>' , ' Comma-separated tags' , val => val . split ( ',' ) )
46+ . command ( ` add-snippet <name>` )
47+ . description ( ` Add your code snippet in the language of your choice` )
48+ . option ( ` -l, --lang <language>` , ` Specify the programming language` )
49+ . option ( ` -d, --desc <description>` , ` Describe the code snippet` )
50+ . option ( ` -f, --file <path>` , ` Path to the file containing the snippet` )
51+ . option ( ` --tags <tags>` , ` Comma-separated tags` , val => val . split ( `,` ) )
3552 . action ( ( name , options ) => {
36- console . log ( ' Name:' , name ) ;
37- console . log ( ' Language:' , options . lang ) ;
38- console . log ( ' Description:' , options . desc ) ;
39- console . log ( ' File Path:' , options . file ) ;
40- console . log ( ' Tags:' , options . tags ) ;
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 ) ;
4158 } ) ;
4259
4360
4461//delete snippet
4562program
46- . command ( ' delete-snippet <name>' )
47- . description ( ' Delete the snippet that you would like to discard' )
48- . option ( ' -f, --file <path>' , ' Path to the file containing the snippet' )
63+ . command ( ` delete-snippet <name>` )
64+ . description ( ` Delete the snippet that you would like to discard` )
65+ . option ( ` -f, --file <path>` , ` Path to the file containing the snippet` )
4966 . action ( ( name , options ) => {
5067 if ( ! options . file ) {
51- console . error ( '❌ Please provide a file path using -f or --file' ) ;
68+ console . error ( ` Please provide a file path using -f or --file` ) ;
5269 process . exit ( 1 ) ;
5370 }
5471
@@ -60,61 +77,83 @@ program
6077 output : process . stdout ,
6178 } ) ;
6279
63- rl . question ( `⚠️ Are you sure you want to delete "${ name } " at ${ filePath } ? (y/n): ` , ( answer ) => {
80+ rl . question ( `Are you sure you want to delete "${ name } " at ${ filePath } ? (y/n): ` , ( answer ) => {
6481 rl . close ( ) ;
6582
66- if ( answer . toLowerCase ( ) === 'y' || answer . toLowerCase ( ) === ' yes' ) {
83+ if ( answer . toLowerCase ( ) === `y` || answer . toLowerCase ( ) === ` yes` ) {
6784 fs . unlink ( filePath , ( err ) => {
6885 if ( err ) {
69- console . error ( `❌ Failed to delete file: ${ filePath } ` ) ;
86+ console . error ( `Failed to delete file: ${ filePath } ` ) ;
7087 console . error ( err . message ) ;
7188 } else {
72- console . log ( `✅ Snippet "${ name } " deleted from ${ filePath } ` ) ;
89+ console . log ( `Snippet "${ name } " deleted from ${ filePath } ` ) ;
7390 }
7491 } ) ;
7592 } else {
76- console . log ( '❎ Deletion cancelled.' ) ;
93+ console . log ( ` Deletion cancelled.` ) ;
7794 }
7895 } ) ;
7996 } ) ;
8097
8198
99+
82100//edit-snippet
83101program
84- . command ( ' edit-snippet <name>' )
85- . description ( ' Edit an existing snippet' )
86- . option ( ' -l, --lang <language>' , ' New programming language' )
87- . option ( ' -d, --desc <description>' , ' New description' )
88- . option ( ' -f, --file <path>' , ' Path to the file to overwrite' ) // ✅ Fix is here
89- . option ( ' --tags <tags>' , ' New comma-separated tags' , val => val . split ( ',' ) )
102+ . command ( ` edit-snippet <name>` )
103+ . description ( ` Edit an existing snippet` )
104+ . option ( ` -l, --lang <language>` , ` New programming language` )
105+ . option ( ` -d, --desc <description>` , ` New description` )
106+ . option ( ` -f, --file <path>` , ` Path to the file to overwrite` ) // ✅ Fix is here
107+ . option ( ` --tags <tags>` , ` New comma-separated tags` , val => val . split ( `,` ) )
90108. action ( ( name , options ) => {
91109 if ( ! options . file ) {
92- console . error ( '❌ Please provide the file path using -f or --file' ) ;
110+ console . error ( ` Please provide the file path using -f or --file` ) ;
93111 process . exit ( 1 ) ;
94112 }
95113
96114 const filePath = path . resolve ( options . file ) ;
97115
98116 if ( ! fs . existsSync ( filePath ) ) {
99- console . error ( `❌ File not found: ${ filePath } ` ) ;
117+ console . error ( `File not found: ${ filePath } ` ) ;
100118 process . exit ( 1 ) ;
101119 }
102120
103- console . log ( `✏️ Editing snippet "${ name } " at ${ filePath } ` ) ;
121+ console . log ( `Editing snippet "${ name } " at ${ filePath } ` ) ;
104122
105123 if ( options . lang ) {
106- console . log ( `🧠 Language updated to: ${ options . lang } ` ) ;
124+ console . log ( `Language updated to: ${ options . lang } ` ) ;
107125 }
108126 if ( options . desc ) {
109- console . log ( `📝 Description updated to: ${ options . desc } ` ) ;
127+ console . log ( `Description updated to: ${ options . desc } ` ) ;
110128 }
111129 if ( options . tags ) {
112- console . log ( `🏷️ Tags updated to: ${ options . tags . join ( ', ' ) } ` ) ;
130+ console . log ( `Tags updated to: ${ options . tags . join ( `, ` ) } ` ) ;
113131 }
114132
115- console . log ( '✅ Edit command completed (metadata displayed, file not changed)' ) ;
133+ console . log ( ` Edit command completed (metadata displayed, file not changed)` ) ;
134+ } ) ;
135+
136+ //list snippet
137+ program
138+ . command ( `list-snippet` )
139+ . description ( `List all your snippets` )
140+ . action ( ( ) => {
141+ const snippets = loadSnippets ( ) ;
142+ if ( snippets . length === 0 ) {
143+ console . log ( `No snippets saved yet` ) ;
144+ return ;
145+ }
146+ console . log ( `Saved snippets` ) ;
147+ snippets . forEach ( ( snip , index ) => {
148+ console . log ( `Snippet- ${ index + 1 } ` ) ;
149+ console . log ( ` Name: ${ snip . name } ` ) ;
150+ console . log ( `language: ${ snip . lang } ` ) ;
151+ console . log ( `Description: ${ snip . desc } ` ) ;
152+ console . log ( `File path: ${ snip . filepath } ` ) ;
153+ console . log ( `Tags: ${ snip . tags ?. join ( `, ` ) } ` ) ;
154+ } ) ;
116155} ) ;
117156
118- //list and view snippet pending
119157
158+ //view snippet pending
120159program . parse ( process . argv ) ;
0 commit comments