<?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: David Hill</title>
    <description>The latest articles on DEV Community by David Hill (@vidhill).</description>
    <link>https://dev.to/vidhill</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%2F22629%2Ff13b9749-33d6-4f43-a7b3-d314244f19ff.jpeg</url>
      <title>DEV Community: David Hill</title>
      <link>https://dev.to/vidhill</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vidhill"/>
    <language>en</language>
    <item>
      <title>Streamlining File Organisation in JS Projects: Automating file Nesting with Bash</title>
      <dc:creator>David Hill</dc:creator>
      <pubDate>Sun, 13 Oct 2024 19:14:39 +0000</pubDate>
      <link>https://dev.to/vidhill/bash-script-to-speed-up-folder-structure-revision-2f39</link>
      <guid>https://dev.to/vidhill/bash-script-to-speed-up-folder-structure-revision-2f39</guid>
      <description>&lt;p&gt;In a JS project often you start of with a single file for a component, or anything for that matter. &lt;/p&gt;

&lt;p&gt;At some stage you might find that you need additional files, for tests etc.&lt;/p&gt;

&lt;p&gt;e.g. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;my-component.tsx&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;my-component.spec.ts&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;my-component.module.css&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;my-component.stories.tsx&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I avoid that, &lt;br&gt;
I feel it is much tidier to put all related files inside a folder and use the &lt;code&gt;index&lt;/code&gt; file naming convention.&lt;/p&gt;

&lt;p&gt;So, as soon as I need a second file I would generally move &lt;code&gt;my-component.tsx&lt;/code&gt; into as&lt;br&gt;
 folder &lt;code&gt;my-component/index.tsx&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;index.js&lt;/code&gt; files
&lt;/h2&gt;

&lt;p&gt;For CommonJS and esm modules, these two files are equivalent &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;my-service.ts&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;&lt;code&gt;my-service/index.ts&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A nice feature of this, is that the import: &lt;code&gt;import { Foo } from "./my-service"&lt;/code&gt; will work with both &lt;code&gt;my-service.ts&lt;/code&gt; and &lt;code&gt;my-service/index.ts&lt;/code&gt; files, without requiring any changes to the import path&lt;/p&gt;
&lt;h2&gt;
  
  
  The &lt;del&gt;Problem&lt;/del&gt; Annoyance
&lt;/h2&gt;

&lt;p&gt;I find it a bit tiresome to do the dance of...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; components/my-service
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;git &lt;span class="nb"&gt;mv &lt;/span&gt;components/my-component.tsx components/my-component/index.tsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if I mis-remember whether the file is not under version control yet, I might get a&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fatal: not under version control, source=components/my-component.tsx,
 destination=components/my-component/index.tsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-more annoyance..&lt;/p&gt;

&lt;p&gt;Or perhaps more annoyingly, if I get it wrong the other way around and use &lt;code&gt;mv&lt;/code&gt;, I could end up with a &lt;code&gt;git status&lt;/code&gt; of&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Changes not staged for commit:
        deleted:    components/my-component.tsx

Untracked files:
        components/my-component/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the default &lt;code&gt;mv&lt;/code&gt; command gets treated as a deletion and creation of a new file by &lt;code&gt;git&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My Solution
&lt;/h2&gt;

&lt;p&gt;I have written a bash script to automate the dance &lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./nest.sh components/my-component.tsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;results in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;tree components
components
└── my-component
    └── index.tsx
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the file is under version control, the script uses &lt;code&gt;git mv&lt;/code&gt; otherwise uses plain old &lt;code&gt;mv&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2
&lt;/h3&gt;

&lt;p&gt;multiple files...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;./nest.sh components/my-component.tsx
&lt;span class="nv"&gt;$ &lt;/span&gt;./nest.sh components/my-component.spec.ts
&lt;span class="nv"&gt;$ &lt;/span&gt;./nest.sh components/my-component.css
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;results in&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;tree components
components
└── my-component
    └── index.tsx
    └── index.spec.ts
    └── index.css
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See the bash script in a &lt;a href="https://gist.github.com/vidhill/f596b44e79c1e089c17b395bca2fd622" rel="noopener noreferrer"&gt;Github Gist here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have the script named as &lt;code&gt;nest&lt;/code&gt; which is in a &lt;code&gt;bin&lt;/code&gt; folder in my &lt;code&gt;$PATH&lt;/code&gt; so I can use it as a command anywhere&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>bash</category>
      <category>cli</category>
    </item>
  </channel>
</rss>
