<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jarod Brennfleck</title>
    <description>The latest articles on DEV Community by Jarod Brennfleck (@thebrenny).</description>
    <link>https://dev.to/thebrenny</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F243044%2F19ab9a9a-6e97-47d7-b8ed-e8df3a535527.png</url>
      <title>DEV Community: Jarod Brennfleck</title>
      <link>https://dev.to/thebrenny</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thebrenny"/>
    <language>en</language>
    <item>
      <title>Using Node because DOSKEY hurts</title>
      <dc:creator>Jarod Brennfleck</dc:creator>
      <pubDate>Wed, 26 Aug 2020 08:03:42 +0000</pubDate>
      <link>https://dev.to/thebrenny/using-node-because-doskey-hurts-1mfa</link>
      <guid>https://dev.to/thebrenny/using-node-because-doskey-hurts-1mfa</guid>
      <description>&lt;h1&gt;
  
  
  Using NodeJS because DOSKEY hurts
&lt;/h1&gt;

&lt;p&gt;So I've tried for a few years now to enrich my experience at the windows command line by making it more unix-like.&lt;/p&gt;

&lt;p&gt;Specifically, I wanted a way to create persistent aliases, which lead me to &lt;code&gt;DOSKEY&lt;/code&gt;, a windows command-line tool that can create shortcut commands. Notice how I said "&lt;em&gt;shortcut&lt;/em&gt;"? Yeah, I've had experience with bash's &lt;code&gt;alias&lt;/code&gt; and &lt;code&gt;DOSKEY&lt;/code&gt; feels severely lacking - and so I was about to give up.&lt;/p&gt;

&lt;p&gt;But recently I realised, that you can register cross-platform commands in a &lt;code&gt;package.json&lt;/code&gt; file for an NPM project. The way it works is by NPM having a folder, within which, it creates small files with &lt;code&gt;.cmd&lt;/code&gt;, &lt;code&gt;.sh&lt;/code&gt; and &lt;code&gt;{filename}&lt;/code&gt; whenever an NPM package registers a command. This folder's location is added to the path so these files can be run directly from the command line. Interesting... Let's exploit this same process to make the Windows Command Prompt a little nicer - particularly for aliases.&lt;/p&gt;

&lt;p&gt;Obivously, the only pre-requisite is that you have Node installed (it doesn't even need to be on the path!)&lt;/p&gt;

&lt;h2&gt;
  
  
  (Not-Quite-A*) Pathing
&lt;/h2&gt;

&lt;p&gt;The first thing to set is the location of where we're going to place our alias files. To make this more portable, use something that's common across all accounts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nv"&gt;%USERPROFILE%&lt;/span&gt;\.aliases
&lt;span class="nb"&gt;setx&lt;/span&gt; &lt;span class="kd"&gt;ALIAS_PATH&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;%USERPROFILE%&lt;/span&gt;&lt;span class="s2"&gt;\.aliases"&lt;/span&gt;
&lt;span class="nb"&gt;setx&lt;/span&gt; &lt;span class="kd"&gt;PATH&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;%PATH%&lt;/span&gt;&lt;span class="s2"&gt;;&lt;/span&gt;&lt;span class="nv"&gt;%ALIAS_PATH%&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;code&gt;setx&lt;/code&gt; is the permanent version of &lt;code&gt;set&lt;/code&gt;. You can also edit the &lt;code&gt;PATH&lt;/code&gt; graphically, but install scripts are more fun. We're persisting our alias path because I have a hunch that we might need it later in the future... Even if we dont, it can't hurt to have it.&lt;/p&gt;
&lt;h2&gt;
  
  
  The first alias!
&lt;/h2&gt;

&lt;p&gt;Now that the folder is on the path, we can place files in here to run them without needing the directory. The first alias I made was &lt;code&gt;ls&lt;/code&gt;, just to see if it works, but also because I make the mistake when I switch back to Windows:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ls.cmd&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight batchfile"&gt;&lt;code&gt;&lt;span class="c"&gt;:: Just an alias for dir /B. /B makes it bare (no header info)&lt;/span&gt;
@dir &lt;span class="na"&gt;/B &lt;/span&gt;&lt;span class="err"&gt;%&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;There's no need to reload your terminal, too! You should be able to &lt;code&gt;&amp;gt; ls&lt;/code&gt; right away and see the output! Not quite the unix &lt;code&gt;ls&lt;/code&gt;, but at least it works!&lt;/p&gt;

&lt;p&gt;In fact, since we made our alias path an environment variable, we can exploit the fact that we can make aliases from anywhere!&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; cd
C:\Users\User\tmp
&amp;gt; echo :: Alias for move &amp;gt;&amp;gt; %ALIAS_PATH%\mv.cmd
&amp;gt; echo @move %* &amp;gt;&amp;gt; %ALIAS_PATH%\mv.cmd
&amp;gt; echo test &amp;gt;&amp;gt; a.txt
&amp;gt; ls
a.txt
&amp;gt; mv a.txt b.txt
1 file(s) moved.
&amp;gt; ls
b.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;🥳 Neat!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;But there's one small annoyance now... Creating the aliases isn't always as simple as that, and even then, why talk about Node just to not use it?&lt;/p&gt;

&lt;p&gt;I'm sure you can see the positive impact this will have on your terminal sessions for a start.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using Node for aliases instead of Batch
&lt;/h2&gt;

&lt;p&gt;But what about using Node for aliases? I mean, it's in the name of this post!&lt;/p&gt;

&lt;p&gt;Well, funny you should ask, because that's exactly what we're going to do!&lt;/p&gt;

&lt;p&gt;The first alias we're going to create is our own &lt;em&gt;custom&lt;/em&gt; alias function. This'll work exactly like how NPM's commands work: buy placing our generic &lt;code&gt;alias.cmd&lt;/code&gt; file (which execute our js files) in our PATHed alias folder!&lt;/p&gt;

&lt;p&gt;Here's our &lt;code&gt;alias.cmd&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;If you look close enough, you'll see it's more or less identical to the &lt;code&gt;.cmd&lt;/code&gt; files of NPM's commands. But you'll also notice that we're calling &lt;code&gt;"%curdir%\**js**\%fileCalled%.js"&lt;/code&gt;. We need a new subfolder within our &lt;code&gt;.aliases&lt;/code&gt; folder called &lt;code&gt;js&lt;/code&gt;. A smart move would be to also &lt;code&gt;npm init&lt;/code&gt; within it so we can access NPM packages without installing them globally! &lt;em&gt;(If you do this though, add &lt;code&gt;"private": true&lt;/code&gt; to the &lt;code&gt;package.json&lt;/code&gt; so you don't accidentally publish it to NPM!)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This makes our file structure look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.aliases
│   alias.cmd
│   ls.cmd
│   mv.cmd
│
└───js
    │   package.json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Finally, &lt;code&gt;cd js&lt;/code&gt;, and &lt;code&gt;echo console.log("hello alias!") &amp;gt;&amp;gt; alias.js&lt;/code&gt;. Now if execute alias, you'll see that node is running the &lt;code&gt;alias.js&lt;/code&gt; file! You can now employ JS to handle all your aliases!&lt;/p&gt;
&lt;h2&gt;
  
  
  A little something extra...
&lt;/h2&gt;

&lt;p&gt;So it's cool and all to have your own &lt;code&gt;alias.js&lt;/code&gt; file run, but it actually isn't too much use to us... At least not yet... Follow these commands and copy the file underneath, and you'll be amazed!&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nb"&gt;cd&lt;/span&gt;
&lt;span class="go"&gt;C:\Users\User\.aliases\js
&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;big-kahuna
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Copy the &lt;code&gt;alias.js&lt;/code&gt; file from &lt;a href="https://gist.github.com/TheBrenny/68a873085e7df07375c00addd18ee5f0"&gt;this gist&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;The file assumes that you're using VSCode as your editor. You can set it by changing &lt;a href="https://gist.github.com/TheBrenny/68a873085e7df07375c00addd18ee5f0#file-alias-js-L11"&gt;line 11&lt;/a&gt;. Note that it does open up the alias folder!&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping it up 🌯
&lt;/h2&gt;

&lt;p&gt;Awesome! We now have alias files &lt;strong&gt;that are persistent&lt;/strong&gt; and have (arguably) &lt;strong&gt;more functionality&lt;/strong&gt; than they're normal CMD counterparts! Anything that you can do in NodeJS is possible through this method!&lt;/p&gt;

&lt;p&gt;Of course, this is somewhat language-extensible by adding calling different programs instead of &lt;code&gt;node&lt;/code&gt;. For example, you can achieve the same result with &lt;code&gt;python&lt;/code&gt;, &lt;code&gt;rust&lt;/code&gt;, &lt;code&gt;dart&lt;/code&gt;, you name it! If the language can execute files from the command line, you can execute them as an alias!&lt;/p&gt;

&lt;p&gt;Enjoy! 🎂🎉&lt;/p&gt;

</description>
      <category>node</category>
      <category>alias</category>
      <category>doskey</category>
    </item>
  </channel>
</rss>
