<?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: Kamil Ogórek</title>
    <description>The latest articles on DEV Community by Kamil Ogórek (@kamilogorek).</description>
    <link>https://dev.to/kamilogorek</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%2F616%2F1523305.jpeg</url>
      <title>DEV Community: Kamil Ogórek</title>
      <link>https://dev.to/kamilogorek</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kamilogorek"/>
    <language>en</language>
    <item>
      <title>Setting up a Minimal, Yet Useful JavaScript Dev Environment </title>
      <dc:creator>Kamil Ogórek</dc:creator>
      <pubDate>Mon, 21 Nov 2016 21:25:48 +0000</pubDate>
      <link>https://dev.to/corgibytes/setting-up-a-minimal-yet-useful-javascript-dev-environment</link>
      <guid>https://dev.to/corgibytes/setting-up-a-minimal-yet-useful-javascript-dev-environment</guid>
      <description>&lt;p&gt;In an era of omnipresent frameworks, libraries and tooling, it may be hard to decide what tool to use and when.&lt;/p&gt;

&lt;p&gt;I know from experience, that the first thing you do, once you decide to write a module or CLI tool, is set up an environment. Some people love it, some hate it. But no matter on which side you are, you'll most likely end up spending way too much time doing it, polishing every aspect of the setup.&lt;/p&gt;

&lt;p&gt;Sure, you could use webpack, eslint, jasmine or even TypeScript to get great compile error messages. The truth is though, most of the time, as developers, we can get by with tools that require almost no configuration. These "out-of-the-box" tools are usually perfectly acceptable, and will help us jump straight to solving the problem, while providing an almost instant feedback loop.&lt;/p&gt;

&lt;p&gt;When talking about the minimal setup, things that come to mind are testing, linting, watching changes and making sure that you won't break anything before committing changes.&lt;/p&gt;

&lt;p&gt;Here's a step-by-step to help you go from having nothing, to being productive in five minutes or less (depending on NPM's mood).&lt;/p&gt;

&lt;h2&gt;
  
  
  Init Node.js project and GIT repository
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Create a directory and cd into it (#protip – $_ holds argument of your last command)&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;awesome-module&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;cd&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="bp"&gt;$_&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Initialize Node.js project with default settings&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;npm&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;--yes&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Create initial folders and files&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;mkdir&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;lib&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;touch&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;index.js&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;lib/meaningOfLife.js&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;test/index.test.js&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;test/meaningOfLife.test.js&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;span class="c"&gt;# Initialize GIT repository and create initial commit&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;git&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;git&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-A&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;git&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;commit&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nt"&gt;-am&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Awesome Module, day one"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Install tools
&lt;/h2&gt;

&lt;p&gt;Here, we'll use four simple modules, each having a single purpose. &lt;a href="https://github.com/avajs/ava"&gt;Ava&lt;/a&gt; for testing, &lt;a href="https://github.com/feross/standard"&gt;Standard&lt;/a&gt; for linting, &lt;a href="https://github.com/kimmobrunfeldt/chokidar-cli"&gt;Chokidar-cli&lt;/a&gt; for file watching and &lt;a href="https://github.com/nlf/precommit-hook"&gt;Precommit-hook&lt;/a&gt; for automatically running npm scripts.&lt;/p&gt;

&lt;p&gt;Why those tools? Because they don't require any configuration and take the cognitive load from your brain. One less thing to think and worry about.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm i --save-dev ava standard chokidar-cli precommit-hook
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember to create a &lt;code&gt;.gitignore&lt;/code&gt; file and add &lt;code&gt;node_modules&lt;/code&gt; to it! We don't want it in our repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set up tools
&lt;/h2&gt;

&lt;p&gt;Open &lt;code&gt;package.json&lt;/code&gt; and add those scripts to your file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "test": "ava",
  "lint": "standard",
  "dev": "chokidar '**/*.js' -c 'standard &amp;amp;&amp;amp; ava'"
},
"pre-commit": ["test", "lint"],
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Aaaand you are done! Once you run &lt;code&gt;npm run dev&lt;/code&gt; you'll get all of your JS files linted by Standard.js and tests run by Ava. There's nothing more to it, and you can start working right away.&lt;/p&gt;

&lt;p&gt;The same goes for creating GIT commits. You won't be able to do so, unless all of your tests are green and linter is happy.&lt;/p&gt;

&lt;p&gt;Two things worth noting:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You don't have to install &lt;code&gt;standard&lt;/code&gt; or &lt;code&gt;ava&lt;/code&gt; globally, as they are run from within the &lt;code&gt;node&lt;/code&gt; context.&lt;/li&gt;
&lt;li&gt;Because we use &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; instead of &lt;code&gt;;&lt;/code&gt; here in the &lt;code&gt;dev&lt;/code&gt; script, tests won't be triggered unless you pass linter rules. It makes the feedback loop even faster.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Working with the setup
&lt;/h2&gt;

&lt;p&gt;Because the environment assumes you'll work in TDD style (and you probably should!), once you run your &lt;code&gt;dev&lt;/code&gt; script, you can create tests and they will be added to the test suite, without any need for restarting a watcher or rebuilding anything.&lt;/p&gt;

&lt;p&gt;Let's create the first test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// test/meaningOfLife.test.js
const test = require('ava')
const meaningOfLife = require('../lib/meaningOfLife')

test('Real meaning of life', (t) =&amp;gt; {
  t.is(meaningOfLife(), 42)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you save the file, you'll get instantly notified that one of your tests is failing. Let's fix it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// lib/meaningOfLife.js
module.exports = () =&amp;gt; 42
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we are back to green! It's as simple as that. Let's create another module that'll multiply a numeric parameter, unit test this module and see whether it'll work nicely with our “meaning of life” (note that it's already an integration test, not unit!).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// lib/multiply.js
module.exports = (number) =&amp;gt; {
  if (typeof number !== 'number') throw new TypeError('Only numbers can be multiplied!')
  return number * 2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// test/multiply.test.js
const test = require('ava')
const multiply = require('../lib/multiply')

test('Can multiply numbers', (t) =&amp;gt; {
  t.is(multiply(10), 20)
})

test('Throws when you try to multiply non-number value', (t) =&amp;gt; {
  t.throws(() =&amp;gt; multiply('ohai!'), 'Only numbers can be multiplied!')
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to test it all together:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// test/index.test.js
const test = require('ava')
const awesomeModule = require('../index')

test('Works nicely together', (t) =&amp;gt; {
  t.is(awesomeModule(), 84)
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// index.js
const meaningOfLife = require('./lib/meaningOfLife')
const multiply = require('./lib/multiply')

module.exports = () =&amp;gt; multiply(meaningOfLife())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works! In just a few minutes, we got everything up and running.&lt;/p&gt;

&lt;p&gt;We, as developers, are often charmed by shiny new tools. We seem to forget that those things should make our work easier, faster and less error prone. The simplest solutions are more than enough, more often than we think. Instead of spending an enormous amount of time on the setup, we should spend it on writing the software itself. And following these steps will allow you to do just that.&lt;/p&gt;

&lt;p&gt;Once your project starts to grow, you may find yourself in need of something more complex. In most cases, however, it just won't happen. And those tools will suit your needs quite well for a long, long time.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>environment</category>
      <category>tools</category>
    </item>
  </channel>
</rss>
