<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anshuman Khanna on Medium]]></title>
        <description><![CDATA[Stories by Anshuman Khanna on Medium]]></description>
        <link>https://medium.com/@anshumankh01?source=rss-c05c3619740f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9czA9EMVCQbKnvgjObg6Lg.png</url>
            <title>Stories by Anshuman Khanna on Medium</title>
            <link>https://medium.com/@anshumankh01?source=rss-c05c3619740f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 22:59:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anshumankh01/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[I created a CLI, you should too, how?]]></title>
            <link>https://medium.com/@anshumankh01/i-created-a-cli-you-should-too-how-91edefc99797?source=rss-c05c3619740f------2</link>
            <guid isPermaLink="false">https://medium.com/p/91edefc99797</guid>
            <category><![CDATA[cli]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Anshuman Khanna]]></dc:creator>
            <pubDate>Tue, 25 Feb 2025 11:05:57 GMT</pubDate>
            <atom:updated>2025-02-25T11:05:57.303Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9czA9EMVCQbKnvgjObg6Lg.png" /><figcaption><a href="https://www.npmjs.com/package/snippet-generator">My baby</a></figcaption></figure><p>This is a follow up to my this <a href="https://medium.com/@anshumankh01/i-created-a-cli-you-should-too-why-2b56418126c6">post</a> which if you haven’t read, please read it first since I am going to be answering a lot of questions I left open ended there, today.</p><blockquote>How do I take arguments?</blockquote><p>This one is pretty easy. As I was using Node.js, I made a function for it and used process.argv</p><pre>function getArgs(): string[] {<br>    return process.argv.slice(2);<br>}<br></pre><p>process.argv basically collects everything you write in terminal while running the file. For eg: if you type in terminal node index.js 1 2 3 then the output of process.argv is [&quot;&lt;some-path&gt;/node&quot;, &quot;index.js&quot;, &quot;1&quot;, &quot;2&quot;, &quot;3&quot;] . From this you can see that the first two arguments are useless for us and we only require everything after that hence we use .slice(2) .</p><blockquote>How do you copy from the clipboard? Turns out there was a terminal command for that which is “powershell Get-Clipboard”. But how will I run a terminal command in my code?</blockquote><p>This one was very hectic, not because it was hard but because this is where I realized I’ll need a lot of code thrice since for each OS, these system commands would differ.</p><pre>import { homedir, platform as osPlatform } from &quot;os&quot;;<br><br>// platform specific commands for different platforms<br>const platformData = {<br>    darwin: {<br>        clipboard: &quot;pbpaste&quot;,<br>        snippetPath: [&quot;Library&quot;, &quot;Application&quot;, &quot;Support&quot;]<br>        // snippetPath will come in handy later on<br>    },<br>    win32: {<br>        clipboard: &quot;powershell Get-Clipboard&quot;,<br>        snippetPath: [&quot;AppData&quot;, &quot;Roaming&quot;]<br>    },<br>    linux: {<br>        clipboard: &quot;xclip -o&quot;,<br>        snippetPath: [&quot;.config&quot;]<br>    }<br>} as const;<br><br>async function getClipboard(): Promise&lt;OutputType&lt;string&gt;&gt; {<br>    const clipboardCommand = platformData[platform].clipboard;<br>    const output: OutputType&lt;string&gt; = {<br>        error: null,<br>        result: &quot;&quot;<br>    };<br><br>    // a tricky part since exec by default doesn&#39;t let you await it so<br>    // we have to wrap it in a Promise to use it properly<br>    return new Promise((res, rej) =&gt; {<br>        exec(clipboardCommand, (error, stdout, stderr) =&gt; {<br>            if (error || stderr) {<br>                console.error(error);<br>                console.error(stderr);<br>            } else {<br>                output.result = stdout;<br>            }<br><br>            res(output);<br>        });<br>    });<br>}</pre><blockquote>…how do you read a file and how do you write to it and make sure you aren’t repeating an existing snippet?</blockquote><p>This was one thing I felt was super easy since I technically knew how to do it. I had studied it but then never used it. Basically we will be doing file reading and writing operation and let JSON handle merging same snippets based on their titles.</p><pre>import { FileHandle, open } from &quot;fs/promises&quot;;<br><br>// a lot of code in between<br><br>// inside some function<br>let fileHandle: FileHandle;<br>const filePath = makeJsonFilePath(newSnippet.language);<br><br>try {<br>    fileHandle = await open(filePath, &quot;r+&quot;);<br>    <br>    const content = await fileHandle.readFile({ encoding: &quot;utf-8&quot; });<br>    const contentJSON = JSON.parse(cleanJsonComments(content));<br>    const date = (new Date()).toString();<br>    const newData = {<br>        [newSnippet.title ?? date]: {<br>            prefix: newSnippet.prefix,<br>            body: newSnippet.body,<br>            description: newSnippet.description ?? date<br>        }<br>    }<br><br>    await fileHandle.write(JSON.stringify({...contentJSON, ...newData}, null, 4), 0);<br><br>    console.log(`${newSnippet.language}.json has been modified`);<br>} catch (error) {<br>    console.error(error);<br>} finally {<br>    await fileHandle?.close();<br>}</pre><p>Here I am using fs/promises (which if you don’t know is the right way) but there are less convenient and more hectic ways that Node.js provides which you are free to try <em>(cough…callback…cough…synchronous…ugh synchronous)</em>.</p><p>Now does it seem so hard? I’m sure it doesn’t. There’s also the challenge of setting up TypeScript in Node.js yourself which I faced but thanks to ChatGPT…you were expecting I would say this, no I read <a href="https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html">TS Docs</a>.</p><p>Thanks to the docs I was able to easily setup everything in no time. I was actually too much interested in docs, since there are so many features but had to restrain myself so I could finish the project on time.</p><p>In the future I may compile it to a binary executable if I want to work on it more.</p><p>I’ll be posting more ‘why’ and ‘how’ soon (not like this time taking a month).</p><p>In the meantime, try out this tool at <a href="https://www.npmjs.com/package/snippet-generator">npm</a> and contribute to it, share suggestions or star it at <a href="https://github.com/Anshumankhanna/snippet-generator-cli">github</a> or just type ‘npx snip’ on your terminal to get started 😄 (if you have Node.js installed).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91edefc99797" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I created a CLI, you should too, why?]]></title>
            <link>https://medium.com/@anshumankh01/i-created-a-cli-you-should-too-why-2b56418126c6?source=rss-c05c3619740f------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b56418126c6</guid>
            <category><![CDATA[software]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[cli]]></category>
            <dc:creator><![CDATA[Anshuman Khanna]]></dc:creator>
            <pubDate>Sat, 25 Jan 2025 09:52:19 GMT</pubDate>
            <atom:updated>2025-01-25T09:52:19.727Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9czA9EMVCQbKnvgjObg6Lg.png" /><figcaption>My package</figcaption></figure><p>Hello, my name is Anshuman Khanna. I am a developer and a student, neither enforced by any institution. All my free will. Unlike you… probably.</p><p>If you are someone who has studied programming you may be aware of the great wall that is DSA vs Development. Why does this exist? Aren’t both things necessary? Also, you may be aware that many people struggle with DSA after doing development.</p><p>Imagine I train you to cut vegetables with a knife and then one day hand you a katana to cut carrots. You have a better tool, but you don’t know how to use it. You suddenly have to think about balance, weight distribution, force, also not cutting yourself.</p><p>This is what happens when people learning development (in some high language like Python or JavaScript) start studying DSA (in more basic languages like C++). You have a better tool, you just don’t know how to use it.</p><p>Now when I began development, I thought people should also study DSA in JavaScript instead of C++, as they are using JavaScript for development. That though not wrong technically, was a dumb idea. It is now that I realize, I should have learnt development in C or C++.</p><p>Why should I learn development in C? What’s the point? The same point you learn DSA for. The same point we have CS degrees for. The knowledge is not very useful itself but when you apply it while working on other things, it is almost a superpower.</p><p>Now how will you make a website or an app in C? It is too much effort. You’ll probably need a framework or tools like WASM (which if you don’t know is something you should check out). But this way you will study the language less and these tools more. However, a CLI can be built without requiring anything more than a compiler (or interpreter).</p><p>I made my CLI in TypeScript and before you call me a hypocrite, my goal was to study Node.js. But I ask you, can you create a CLI in Node.js? Further more can you use TypeScript in it? No you cannot (99% of you cannot).</p><p>My idea was to create a CLI that can create VS code snippets. If you don’t know what they are go to File -&gt; Preferences -&gt; Configure Snippets and you’ll see a list of options with this format “languagename.json”. In these .json files we can save our snippets. Basically, a snippet is a large code that you may repeatedly write which you can create a shortcut for.</p><p>I thought it is going to be so easy until I realized that a CLI has to take arguments like when you do “git add README.md”, “git” is an executable on the system and “add” and “README.md” are arguments. How do I take arguments?</p><p>Then to create snippets we need to have a title, prefix (the word to trigger snippet), body (the snippet in the form of array of strings) and a description. To create these snippets I had to use a site in which I used to paste the body of the snippet and then write the title, prefix and description myself as well. And then I had to copy the output from there and paste it manually into the file in which all snippets are stored. So basically I learnt how to create snippets to work faster but now every time I had to create one I was wasting more time. Because of which I kind of stopped using them as well.</p><p>To avoid this redundant copy and paste I thought what if I could fetch from the clipboard what has been copied and that’ll just be my body. How do you copy from the clipboard? Turns out there was a terminal command for that which is “powershell Get-Clipboard”. But how will I run a terminal command in my code?</p><p>Another issue I had with manually writing snippets was that I had switched to TypeScript and often even worked in .jsx and .tsx as well. My JavaScript snippets were useful here but they weren’t available here because each file extension has it’s own .json file. So every time I create a snippet in JavaScript, I would then have to copy it to three other files. So, I should have the ability to merge two files as well, how would I do that? We know they are in a .json file, but how do you read a file and how do you write to it and make sure you aren’t repeating an existing snippet?</p><p>I am sure many of you know that it is possible to read files but you don’t know how exactly and that’s the problem. We are Superman with amnesia, we don’t know how to use our powers!</p><p>This is what this project taught me, so much I didn’t know and that there is yet a lot more to know.</p><p>Firstly to answer one thing that you must be thinking, “Yeah but I don’t need to know this, what’s the use?” This is essentially knowledge that could further help in creating low level software or maybe working as DevOps engineer or most simply automation, which is what I made this for.</p><p>In this project I could have used some packages (like every Gen AI suggested me) but I didn’t because it wasn’t required and it would make a small lightweight project bulky for no reason. When was the last time you thought about the size of your project as a concern for the people using it?</p><p>Lastly, if you can’t answer all the questions above, that’s the answer to why you should create a CLI. It will also spark a confidence in you because you will know the depths of a language.</p><p>I will answer all the how’s in an upcoming post and this is how I will be posting my projects one post with why? and another with how?</p><p>In the meantime, try out this tool at npm <a href="https://www.npmjs.com/package/snippet-generator">https://www.npmjs.com/package/snippet-generator</a> and contribute to it, share suggestions or star it at <a href="https://github.com/Anshumankhanna/snippet-generator-cli">https://github.com/Anshumankhanna/snippet-generator-cli</a> or just type ‘npx snip’ on your terminal to get started 😄.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b56418126c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>