<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Useless Today</title>
  
  
  <link href="/atom.xml" rel="self"/>
  
  <link href="https://useless.today/"/>
  <updated>2019-06-22T01:41:13.000Z</updated>
  <id>https://useless.today/</id>
  
  <author>
    <name>Michael Russell</name>
    
  </author>
  
  <generator uri="http://hexo.io/">Hexo</generator>
  
  <entry>
    <title>Purging Cloudflare Cache With cURL</title>
    <link href="https://useless.today/purge-cloudfare-cache-with-curl/"/>
    <id>https://useless.today/purge-cloudfare-cache-with-curl/</id>
    <published>2018-10-07T00:00:00.000Z</published>
    <updated>2019-06-22T01:41:13.000Z</updated>
    
    <content type="html"><![CDATA[<p><a href="https://www.cloudflare.com/">Cloudflare</a> is an amazing CDN provider as well as a great DNS service, and many more things nowdays; It also gives you free TLS for your websites! Because of this it’s become my go-to for static sites like this one but, I dislike having to sign in to Cloudflare each time I make an update to my site. Thankfully they also have a nice API to work with and something we can utilize within our CI/CD pipelines to purge cache after we make updates.</p><a id="more"></a><p>There’s a whole slew of information about their API <a href="https://api.cloudflare.com">here</a>. What we’re interested in is the ‘zone’ portion of their API which allows for full site purges, by URL, and by cache-tags or host. I usually stick with the full site purge just because it’s easier to toss everything out and allow it to naturally flow back into their cache without missing anything.</p><p>To do this you’ll need your <code>zone id</code> of the domain, <code>api key</code> for your account, and an <code>email address</code> associated to the domain’s account. You can find your <code>zone id</code> by going to the overview of the domain you’re interested in, the <code>api key</code> can be found at the bottom of your account profile (using the top right drop-down), and the email is the one you use to sign in to your account. Once you have all that you can use the following <a href="https://github.com/curl/curl">cURL</a> command to purge your cache!</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">curl -X POST <span class="hljs-string">"https://api.cloudflare.com/client/v4/zones/<span class="hljs-variable">$CF_ZONE_ID</span>/purge_cache"</span> -H <span class="hljs-string">"X-Auth-Email:<span class="hljs-variable">$CF_EMAIL</span>"</span> -H <span class="hljs-string">"X-Auth-Key:<span class="hljs-variable">$CF_API_KEY</span>"</span> -H <span class="hljs-string">"Content-Type:application/json"</span> --data <span class="hljs-string">'&#123;"purge_everything":true&#125;'</span><br></code></pre></td></tr></table></figure><p>If it executed successfully you should see a response similar to <code>{&quot;result&quot;:{&quot;id&quot;:&quot;xxxxxxxxxxxxx&quot;},&quot;success&quot;:true,&quot;errors&quot;:[],&quot;messages&quot;:[]}</code>.</p><p>And that’s it!</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;&lt;a href=&quot;https://www.cloudflare.com/&quot;&gt;Cloudflare&lt;/a&gt; is an amazing CDN provider as well as a great DNS service, and many more things nowdays; It also gives you free TLS for your websites! Because of this it’s become my go-to for static sites like this one but, I dislike having to sign in to Cloudflare each time I make an update to my site. Thankfully they also have a nice API to work with and something we can utilize within our CI/CD pipelines to purge cache after we make updates.&lt;/p&gt;
    
    </summary>
    
    
      <category term="CLI" scheme="https://useless.today/tags/CLI/"/>
    
      <category term="Cloudflare" scheme="https://useless.today/tags/Cloudflare/"/>
    
  </entry>
  
  <entry>
    <title>Using Pantheon&#39;s Terminus CLI tool via Docker</title>
    <link href="https://useless.today/pantheon-terminus-docker/"/>
    <id>https://useless.today/pantheon-terminus-docker/</id>
    <published>2018-10-06T00:00:00.000Z</published>
    <updated>2019-06-22T01:41:13.000Z</updated>
    
    <content type="html"><![CDATA[<p>Pantheon’s <a href="https://pantheon.io/docs/terminus/">Terminus</a> CLI tool is a great way to interact with your sites. If you’re not familiar with Terminus here’s a quote from their docs:</p><blockquote><p>Our command line interface, Terminus, provides advanced interaction with Pantheon. Terminus enables you to do almost everything in a terminal that you can do in the Dashboard, and much more.</p></blockquote><a id="more"></a><h2 id="Docker-setup"><a href="#Docker-setup" class="headerlink" title="Docker setup"></a>Docker setup</h2><p>While Terminus makes deployments from your local machine simple I wanted the ability to run it from our CI/CD servers on git push. To do that I created a <a href="https://hub.docker.com/r/subhaze/terminus/">Docker image</a> that our servers can use; The Dockerfile looks like the following</p><figure class="highlight Dockerfile"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs Dockerfile"><span class="hljs-keyword">FROM</span> composer<br><span class="hljs-keyword">RUN</span><span class="bash"> composer global require pantheon-systems/terminus<br></span><span class="hljs-keyword">ENV</span> PATH=<span class="hljs-string">"/tmp/vendor/bin:$&#123;PATH&#125;"</span><br><span class="hljs-keyword">ENV</span> TERMINUS_USER_HOME=/tmp<br></code></pre></td></tr></table></figure><p>The composer image sets composers home to <code>/tmp</code> so we’re doing the same with Terminus as well as adding the <code>/tmp/vendor/bin</code> to that path. Now we can execute <code>terminus</code> by just referencing the name and not write out the full path to the bin file within the container.</p><h2 id="CI-CD-Pipeline-Setup"><a href="#CI-CD-Pipeline-Setup" class="headerlink" title="CI/CD Pipeline Setup"></a>CI/CD Pipeline Setup</h2><p>You can now user Terminus in your pipeline builds, such as within your <code>bitbucket-pipelines.yml</code>, it would look something like:</p><figure class="highlight yml"><figcaption><span>bitbucket-pipelines.yml</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><code class="hljs yml"><span class="hljs-attr">pipelines:</span><br><span class="hljs-attr">  master:</span><br>    <span class="hljs-comment"># Auto deploy to test server and clear cache</span><br><span class="hljs-attr">    - step:</span><br><span class="hljs-attr">        image:</span> <span class="hljs-string">subhaze/terminus</span><br><span class="hljs-attr">        deployment:</span> <span class="hljs-string">test</span><br><span class="hljs-attr">        name:</span> <span class="hljs-string">Deploying</span> <span class="hljs-string">to</span> <span class="hljs-string">dev</span> <span class="hljs-string">server</span> <span class="hljs-string">and</span> <span class="hljs-string">clearing</span> <span class="hljs-string">cache</span><br><span class="hljs-attr">        script:</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">git</span> <span class="hljs-string">remote</span> <span class="hljs-string">add</span> <span class="hljs-string">upstream</span> <span class="hljs-attr">ssh://codeserver.dev.$SITE_ID@codeserver.dev.$SITE_ID.drush.in:2222/~/repository.git</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">git</span> <span class="hljs-string">push</span> <span class="hljs-string">upstream</span> <span class="hljs-string">master</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">auth:login</span> <span class="hljs-bullet">--machine-token=$PANTHEON_USER_MACHINE_TOKEN</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">env:clear-cache</span> <span class="hljs-string">$SITE_NAME.dev</span><br><br>    <span class="hljs-comment"># Wait for user input to sync production data/files to dev</span><br><span class="hljs-attr">    - step:</span><br><span class="hljs-attr">        image:</span> <span class="hljs-string">subhaze/terminus</span><br><span class="hljs-attr">        name:</span> <span class="hljs-string">Syncing</span> <span class="hljs-string">files/data</span> <span class="hljs-string">from</span> <span class="hljs-string">production</span> <span class="hljs-string">and</span> <span class="hljs-string">clear</span> <span class="hljs-string">cache</span><br><span class="hljs-attr">        trigger:</span> <span class="hljs-string">manual</span><br><span class="hljs-attr">        script:</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">auth:login</span> <span class="hljs-bullet">--machine-token=$PANTHEON_USER_MACHINE_TOKEN</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">env:clone-content</span> <span class="hljs-string">$SITE_NAME.live</span> <span class="hljs-string">dev</span> <span class="hljs-bullet">-y</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">env:clear-cache</span> <span class="hljs-string">$SITE_NAME.dev</span><br><br>    <span class="hljs-comment"># Deploy to test server and clear cache</span><br><span class="hljs-attr">    - step:</span><br><span class="hljs-attr">        image:</span> <span class="hljs-string">subhaze/terminus</span><br><span class="hljs-attr">        name:</span> <span class="hljs-string">Staging</span> <span class="hljs-string">Deploy</span><br><span class="hljs-attr">        deployment:</span> <span class="hljs-string">staging</span><br><span class="hljs-attr">        trigger:</span> <span class="hljs-string">manual</span><br><span class="hljs-attr">        script:</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">auth:login</span> <span class="hljs-bullet">--machine-token=$PANTHEON_USER_MACHINE_TOKEN</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">env:deploy</span> <span class="hljs-string">$SITE_NAME.test</span> <span class="hljs-bullet">--cc</span> <span class="hljs-bullet">--note="Build</span> <span class="hljs-attr">Number:$BITBUCKET_BUILD_NUMBER;</span> <span class="hljs-string">Commit</span> <span class="hljs-attr">SHA:$BITBUCKET_COMMIT;"</span><br>    <span class="hljs-comment"># Wait for user input to sync production data/files to test</span><br><span class="hljs-attr">    - step:</span><br><span class="hljs-attr">        image:</span> <span class="hljs-string">subhaze/terminus</span><br><span class="hljs-attr">        name:</span> <span class="hljs-string">Syncing</span> <span class="hljs-string">files/data</span> <span class="hljs-string">from</span> <span class="hljs-string">production</span> <span class="hljs-string">and</span> <span class="hljs-string">clear</span> <span class="hljs-string">cache</span><br><span class="hljs-attr">        trigger:</span> <span class="hljs-string">manual</span><br><span class="hljs-attr">        script:</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">auth:login</span> <span class="hljs-bullet">--machine-token=$PANTHEON_USER_MACHINE_TOKEN</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">env:clone-content</span> <span class="hljs-string">$SITE_NAME.live</span> <span class="hljs-string">test</span> <span class="hljs-bullet">-y</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">env:clear-cache</span> <span class="hljs-string">$SITE_NAME.test</span><br><br>    <span class="hljs-comment"># Deploy to production and clear cache</span><br><span class="hljs-attr">    - step:</span><br><span class="hljs-attr">        image:</span> <span class="hljs-string">subhaze/terminus</span><br><span class="hljs-attr">        name:</span> <span class="hljs-string">Production</span> <span class="hljs-string">Deploy</span><br><span class="hljs-attr">        deployment:</span> <span class="hljs-string">production</span><br><span class="hljs-attr">        trigger:</span> <span class="hljs-string">manual</span><br><span class="hljs-attr">        script:</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">auth:login</span> <span class="hljs-bullet">--machine-token=$PANTHEON_USER_MACHINE_TOKEN</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">env:deploy</span> <span class="hljs-string">$SITE_NAME.live</span> <span class="hljs-bullet">--cc</span> <span class="hljs-bullet">--note="Build</span> <span class="hljs-attr">Number:$BITBUCKET_BUILD_NUMBER;</span> <span class="hljs-string">Commit</span> <span class="hljs-attr">SHA:$BITBUCKET_COMMIT;"</span><br>          <span class="hljs-comment"># the --cc flag "should" clear the cache but it's proven to be kind of flaky, so adding one more CC for good measure</span><br><span class="hljs-bullet">          -</span> <span class="hljs-string">terminus</span> <span class="hljs-attr">env:clear-cache</span> <span class="hljs-string">$SITE_NAME.live</span><br></code></pre></td></tr></table></figure><p>Above we’re using three environment variables that you would set with your specific info.</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-variable">$SITE_NAME</span> = <span class="hljs-string">'&lt;unique name of your pantheon site&gt;'</span><br><span class="hljs-variable">$SITE_ID</span> = <span class="hljs-string">"&lt;the id of your site, it's formatted like xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&gt;"</span><br><span class="hljs-variable">$PANTHEON_USER_MACHINE_TOKEN</span> = <span class="hljs-string">'&lt;token you generated with your user profile&gt;'</span><br></code></pre></td></tr></table></figure><h2 id="Using-Docker-Image-Locally"><a href="#Using-Docker-Image-Locally" class="headerlink" title="Using Docker Image Locally"></a>Using Docker Image Locally</h2><p>You can also use this image to run terminus on your local machine. This can be handy if you don’t want deal with installing all the requirements for Terminus. To do this simply run the following (docker is required):</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">docker run --rm -it --volume ~/.terminus:/tmp/.terminus subhaze/terminus terminus [args...]<br></code></pre></td></tr></table></figure><p>Here we’re mounting <code>${HOME}/.terminus:/tmp/.terminus</code> so that we can cache our auth token so we can access our remote sites. You could set this up as a bash function and just call it via <code>terminus</code> with the following:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs bash"><span class="hljs-function"><span class="hljs-title">terminus</span></span>()&#123;<br>  docker run -it --rm --volume <span class="hljs-variable">$&#123;HOME&#125;</span>/.terminus:/tmp/.terminus --entrypoint=terminus subhaze/terminus <span class="hljs-string">"<span class="hljs-variable">$&#123;@&#125;</span>"</span><br>&#125;<br></code></pre></td></tr></table></figure><p>The function above sets <code>--entrypoint=terminus</code> so you can use it more naturally, this essentially tells docker to behave like an executable and accept arguments to pass to that executable.</p><p>Now you can use it as if it were installed locally: <code>terminus --version</code>, <code>terminus auth:login --machine-token=&lt;your token&gt;</code>, <code>terminus site:list</code>.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Pantheon’s &lt;a href=&quot;https://pantheon.io/docs/terminus/&quot;&gt;Terminus&lt;/a&gt; CLI tool is a great way to interact with your sites. If you’re not familiar with Terminus here’s a quote from their docs:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Our command line interface, Terminus, provides advanced interaction with Pantheon. Terminus enables you to do almost everything in a terminal that you can do in the Dashboard, and much more.&lt;/p&gt;
&lt;/blockquote&gt;
    
    </summary>
    
    
      <category term="Pantheon" scheme="https://useless.today/tags/Pantheon/"/>
    
      <category term="Docker" scheme="https://useless.today/tags/Docker/"/>
    
      <category term="CLI" scheme="https://useless.today/tags/CLI/"/>
    
  </entry>
  
  <entry>
    <title>CodeKit 3 - Hooking Into Browserify</title>
    <link href="https://useless.today/codekit-3-hooking-into-browserify/"/>
    <id>https://useless.today/codekit-3-hooking-into-browserify/</id>
    <published>2016-11-10T00:00:00.000Z</published>
    <updated>2019-06-22T01:41:13.000Z</updated>
    
    <content type="html"><![CDATA[<p>With <a href="https://codekitapp.com/help/whats-new/">the new dependency tracking</a> CodeKit 3 has for ES Modules, we can write a Hook that triggers on one specific file when it or any of its dependencies change. We can do that by mixing the new dependency tracking with the ability to process files in place (to do ESLinting and minifying) to bundle all our code up with Browserify.<br><a id="more"></a></p><p>The first thing we need to do is <code>npm install</code> a couple third-party dependencies to use in our Hooks; We’ll need Browserify and babelify. After we have these dependencies added we can easily write a hook that will run browserify on our ES Modules with the added bonus of being able to easily require other JS dependencies from npm.</p><h3 id="Setting-up-the-code-to-Hook-into"><a href="#Setting-up-the-code-to-Hook-into" class="headerlink" title="Setting up the code to Hook into"></a>Setting up the code to Hook into</h3><p>I prefer using npm scripts and triggering those via Hooks, this allows npm to handle the paths and we don’t have to references things like <code>./node_modules/.bin/browserify</code> to use browserify on the CLI. So, our first step will be writing an npm script in our package.json file.</p><p>It should look similar to the following:</p><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs json">&#123;<br>  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"codekit_3_browserify"</span>,<br>  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,<br>  <span class="hljs-attr">"description"</span>: <span class="hljs-string">""</span>,<br>  <span class="hljs-attr">"main"</span>: <span class="hljs-string">""</span>,<br>  <span class="hljs-attr">"scripts"</span>: &#123;<br>    <span class="hljs-attr">"build"</span>: <span class="hljs-string">"browserify js/main.js -t [ babelify --presets [ latest ] ]&gt; js/bundle.js"</span><br>  &#125;,<br>  <span class="hljs-attr">"author"</span>: <span class="hljs-string">""</span>,<br>  <span class="hljs-attr">"license"</span>: <span class="hljs-string">"ISC"</span>,<br>  <span class="hljs-attr">"dependencies"</span>: &#123;&#125;,<br>  <span class="hljs-attr">"devDependencies"</span>: &#123;<br>    <span class="hljs-attr">"babel-preset-latest"</span>: <span class="hljs-string">"6.16.0"</span>,<br>    <span class="hljs-attr">"babelify"</span>: <span class="hljs-string">"7.3.0"</span>,<br>    <span class="hljs-attr">"browserify"</span>: <span class="hljs-string">"13.1.1"</span>,<br>    <span class="hljs-attr">"rxjs"</span>: <span class="hljs-string">"^5.0.0-rc.2"</span><br>  &#125;<br>&#125;<br></code></pre></td></tr></table></figure><p>Here we’re</p><ul><li>executing <code>browserify</code> and passing <code>js/main.js</code> as the entry file.</li><li>passing a <code>-t</code> babel transform, so that babel will transform our code into ES 5, and modules to CommonJS style.</li><li>passing presets to babel</li><li>then passing the bundled code to stdout which is piped into <code>js/bundle.js</code></li></ul><p>You could also include uglifyjs and pipe browserifies content to it, but since we can trigger that via CodeKit I see no reason to add that extra dependency since it’s provided. But there may be some case where you might want this, so it’s completely possible to handle minification here as well.</p><h3 id="Run-down-of-file-and-directory-content"><a href="#Run-down-of-file-and-directory-content" class="headerlink" title="Run down of file and directory content"></a>Run down of file and directory content</h3><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs undefined">js/<br>  bizz<span class="hljs-selector-class">.js</span><br>  bundle<span class="hljs-selector-class">.js</span> <span class="hljs-comment">// &lt;-- exit file</span><br>  foo<span class="hljs-selector-class">.js</span><br>  main<span class="hljs-selector-class">.js</span> <span class="hljs-comment">// &lt;-- entry file</span><br><br>node_modules/<br>  rxjs<span class="hljs-comment">/**<br>  [...other dependencies]<br><br>index.html<br>package.json</span><br></code></pre></td></tr></table></figure><p>The above are just simple files to demonstrate small modules that can be imported and bundled up, we do however have <code>rxjs</code> that was installed via npm. I’ve included this to show the ease of including thrid-party libs using this work flow.</p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// bizz.js</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> bizz = <span class="hljs-function">(<span class="hljs-params">buzz=<span class="hljs-string">''</span></span>) =&gt;</span> <span class="hljs-string">`fizz <span class="hljs-subst">$&#123;buzz&#125;</span> bizz`</span>;<br></code></pre></td></tr></table></figure><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// foo.js</span><br><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> foo = <span class="hljs-function">(<span class="hljs-params">something=<span class="hljs-string">''</span></span>) =&gt;</span> <span class="hljs-string">`foo <span class="hljs-subst">$&#123;something&#125;</span>`</span>;<br></code></pre></td></tr></table></figure><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// node_modules/rxjs/Observable.js</span><br><span class="hljs-meta">"use strict"</span>;<br><span class="hljs-keyword">var</span> root_1 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./util/root'</span>);<br><span class="hljs-keyword">var</span> toSubscriber_1 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./util/toSubscriber'</span>);<br><span class="hljs-keyword">var</span> observable_1 = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./symbol/observable'</span>);<br><span class="hljs-comment">/**<br> * A representation of any set of values over any amount of time. This the most basic building block<br> * of RxJS.<br> *<br> * @class Observable&lt;T&gt;<br> */</span><br><span class="hljs-keyword">var</span> Observable = (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br><span class="hljs-comment">// and lots more code ...</span><br>&#125;)()<br></code></pre></td></tr></table></figure><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// main.js</span><br><span class="hljs-keyword">import</span> &#123;foo&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">'./foo'</span>;<br><span class="hljs-keyword">import</span> &#123;bizz&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">'./bizz'</span>;<br><br><span class="hljs-keyword">let</span> &#123;<span class="hljs-built_in">console</span>&#125; = <span class="hljs-built_in">window</span>;<br><br><span class="hljs-built_in">console</span>.log(foo(<span class="hljs-string">'this'</span>));<br><span class="hljs-built_in">console</span>.log(bizz(<span class="hljs-string">'bar'</span>));<br><br><br><span class="hljs-keyword">import</span> &#123;Observable&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">'rxjs/Observable'</span>;<br><span class="hljs-comment">// patch Observable with appropriate methods</span><br><span class="hljs-keyword">import</span> <span class="hljs-string">'rxjs/add/observable/of'</span>;<br><br>Observable.of(<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>).subscribe( <span class="hljs-function"><span class="hljs-params">a</span> =&gt;</span> <span class="hljs-built_in">console</span>.log(a) );<br></code></pre></td></tr></table></figure><p>In <code>main.js</code> we can see that we’re requiring our own ES Modules with a relative path <code>./</code>. This is important so that browserify knowns that the file is relative to the file it’s being imported within and not a dependency that’s within <code>node_modules</code>.</p><p>As for <code>Observable</code> we’re importing it with no relative path, doing this browserify will do a lookup in your <code>node_modules</code> folder to find the dependencies needed. You also may notice that we’re using the  new ES Module syntax to import the <code>Observable</code>, even though it’s built via the CommonJS pattern. This is possible because Babel’s presets will use the CommonJS style when transforming your code, which is nice since we can code in the standard syntax while still requiring dependencies that are built using the CommonJS format.</p><p>Another added benefit is that <code>import {Observable} from &#39;rxjs/Observable&#39;</code> is converted to <code>var Observable = require(&#39;rxjs/Observable&#39;).Observable</code>. Note the added <code>.Observable</code> needed using the CommonJS style; It’s a nice benefit of not having to do this with the ES Module syntax.</p><h3 id="CodeKit-project-setup"><a href="#CodeKit-project-setup" class="headerlink" title="CodeKit project setup"></a>CodeKit project setup</h3><h5 id="JS-Files"><a href="#JS-Files" class="headerlink" title="JS Files"></a>JS Files</h5><p>You’ll want to setup each js file to be processed, but, <strong>ensure</strong> that you set <code>Transpile With:</code> to <code>Nothing</code>; This is because we’re going to be processing this file to itself. You can, however, setup up the linter of your choice, I’ve been moving over the ESLint.</p><p><img src="/images/codekit-3-hooking-into-browserify/browserify-js-settings.png" alt=""></p><blockquote><p>If you’re using ESLint, make sure you’ve set <code>Source Type</code> to be <code>Module</code> and have checked the <code>ES6</code> box.</p></blockquote><p><img src="/images/codekit-3-hooking-into-browserify/browserify-eslint.png" alt=""></p><p>In the following image you’ll see that CodeKit is listing our custom ES Module files that <code>main.js</code> is importing but, you wont see the items that are required from <code>node_modules</code> here due to that folder being skipped by default. Which is a nice perk since you’ll really only want to mess with files that you’re writing yourself and leave the third-party dependencies alone.</p><p><img src="/images/codekit-3-hooking-into-browserify/browserify-main-imports.png" alt=""></p><p>Earlier I wrote about either piping Browserify’s content to uglifyjs yourself, or, letting CodeKit handle the minification. Here you can see that it’s as simple as checking a box, one thing to note though, source maps will not work due to us working outside of CodeKit’s baked in transpiling. If source maps are important to you, this will probably the moment you want to uglify the content yourself as well and setup Browserify to build out the source maps too.</p><p><img src="/images/codekit-3-hooking-into-browserify/browserify-bundle.png" alt=""></p><p>Now, the step that ties everything together and builds out your bundle anytime a dependencies of your entry file changes; The Hook. As you can see below it’s quite simple, just three words. This is because we’re using npm to handle the rest of the logic, so if you want to make modifications to your bundle processes remember that <code>package.json</code> is where all the work is happening.</p><p><img src="/images/codekit-3-hooking-into-browserify/browserify-hook.png" alt=""></p><p>If you find yourself in a situation where npm is not working out for you and your team, maybe due to some folks not having npm and/or node; Or maybe you’re experimenting with different versions of node and npm. If that’s the case you can setup a Hook that uses CodeKits bundled version of node and a custom JS file written on your end to setup Browserify.</p><blockquote><p><strong>::Warning::</strong> Using this method relies on that fact that everyone has CodeKit installed in the same location and that CodeKit itself doesn’t change the location of node, which is possible as this is not documented. So… enter at your own risk.</p></blockquote><p>Your Hook would look something like:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs bash">node=/Applications/CodeKit.app/Contents/Resources/engines/node/node<br><span class="hljs-variable">$node</span> browserify.js &gt; js/bundle.js<br><span class="hljs-built_in">echo</span> <span class="hljs-string">'Built js/bundle.js'</span><br></code></pre></td></tr></table></figure><p>and <code>browserify.js</code> would look something like:</p><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// browserify.js</span><br><br><span class="hljs-keyword">var</span> browserify = <span class="hljs-built_in">require</span>(<span class="hljs-string">'browserify'</span>);<br>browserify(<span class="hljs-string">'./js/main.js'</span>)<br>  .transform(<span class="hljs-string">"babelify"</span>, &#123;<span class="hljs-attr">presets</span>: [<span class="hljs-string">"latest"</span>]&#125;)<br>  .bundle()<br>  .pipe(process.stdout);<br></code></pre></td></tr></table></figure><h3 id="Github-repo"><a href="#Github-repo" class="headerlink" title="Github repo"></a>Github repo</h3><p>You can find the sample project with all the above files at <a href="https://github.com/subhaze/CodeKit_3_Browserify">https://github.com/subhaze/CodeKit_3_Browserify</a></p><p>When you preview the project you should see the following in your console.</p><figure class="highlight lsl"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs undefined">foo this<br>fizz bar bizz<br><span class="hljs-number">1</span><br><span class="hljs-number">2</span><br><span class="hljs-number">3</span><br></code></pre></td></tr></table></figure><p>Happy coding!</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;With &lt;a href=&quot;https://codekitapp.com/help/whats-new/&quot;&gt;the new dependency tracking&lt;/a&gt; CodeKit 3 has for ES Modules, we can write a Hook that triggers on one specific file when it or any of its dependencies change. We can do that by mixing the new dependency tracking with the ability to process files in place (to do ESLinting and minifying) to bundle all our code up with Browserify.&lt;br&gt;
    
    </summary>
    
    
      <category term="CodeKit" scheme="https://useless.today/tags/CodeKit/"/>
    
      <category term="CodeKit Hooks" scheme="https://useless.today/tags/CodeKit-Hooks/"/>
    
      <category term="Browserify" scheme="https://useless.today/tags/Browserify/"/>
    
  </entry>
  
  <entry>
    <title>CodeKit 3 - ES Module Bundling w/TypeScript</title>
    <link href="https://useless.today/codekit-3-ES-Module-Bundling-with-TypeScript/"/>
    <id>https://useless.today/codekit-3-ES-Module-Bundling-with-TypeScript/</id>
    <published>2016-10-29T15:08:04.000Z</published>
    <updated>2019-06-22T01:41:13.000Z</updated>
    
    <content type="html"><![CDATA[<p>Now that CodeKit 3 has launched it’s time to dig into some new functionality it provides; One of my favorites being, the ability for CodeKit to now track ES Module imports in your JavaScript and TypeScript files! This means we now have a true path on bundling up our JavaScript files without hooks.</p><p>While some will want a more flushed out experience via Browserify or Rollup bundling, using TypeScript isn’t that bad, especially when you’re really only using one .ts to do the bundling while the rest of your code can be pure JavaScript.<br><a id="more"></a></p><h3 id="The-file-structure"><a href="#The-file-structure" class="headerlink" title="The file structure"></a>The file structure</h3><figure class="highlight stylus"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs undefined">js/<br>  nested/<br>    maths<span class="hljs-selector-class">.js</span><br>  bundle<span class="hljs-selector-class">.js</span><br>  bundle<span class="hljs-selector-class">.ts</span> &lt;-- the files that provides bundling<br>  onload<span class="hljs-selector-class">.js</span><br>  text<span class="hljs-selector-class">.js</span><br>index.html<br></code></pre></td></tr></table></figure><p>From the above example you’ll see one TypeScript file <code>bundle.ts</code> and its sole purpose is to import our top level JavaScript files and bundle them up into <code>bundle.js</code>. The reason for TypeScript is because it provides built-in bundling so long as you select either the AMD or System format, in this example we’ll be using the System format.</p><p>The contents of each file:</p><figure class="highlight js"><figcaption><span>bundle.ts</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">import</span> <span class="hljs-string">'./onload'</span>;<br></code></pre></td></tr></table></figure><p>Yep, just one line, as <code>onload.js</code> is the top-level file in this project, you will most likely have more.</p><figure class="highlight js"><figcaption><span>onload.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">import</span> &#123;upper, title&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">'./text'</span>;<br><span class="hljs-keyword">import</span> &#123;add, increment&#125; <span class="hljs-keyword">from</span> <span class="hljs-string">'./nested/maths'</span>;<br> <br><span class="hljs-built_in">console</span>.log( upper(<span class="hljs-string">'yell a message here.'</span>) );<br><span class="hljs-built_in">console</span>.log( title(<span class="hljs-string">'title case message here.'</span>) );<br> <br><span class="hljs-built_in">console</span>.log(<span class="hljs-string">`2 + 3 = <span class="hljs-subst">$&#123;add( <span class="hljs-number">2</span>, <span class="hljs-number">3</span> )&#125;</span>`</span>);<br><span class="hljs-built_in">console</span>.log(<span class="hljs-string">`increment 4 by 1 = <span class="hljs-subst">$&#123;increment( <span class="hljs-number">4</span> )&#125;</span>`</span>);<br><span class="hljs-built_in">console</span>.log(<span class="hljs-string">`increment 4 by 3 = <span class="hljs-subst">$&#123;increment( <span class="hljs-number">4</span>, <span class="hljs-number">3</span> )&#125;</span>`</span>);<br></code></pre></td></tr></table></figure><p>You can see here that <code>onload.js</code> loads in our other two files and uses them to log their output to the console.</p><figure class="highlight js"><figcaption><span>text.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> upper = <span class="hljs-function">(<span class="hljs-params">text=<span class="hljs-string">''</span></span>) =&gt;</span> text.toUpperCase();<br><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> title = <span class="hljs-function">(<span class="hljs-params">text=<span class="hljs-string">''</span></span>) =&gt;</span> text<br>  .toLowerCase()<br>  .split(<span class="hljs-string">' '</span>)<br>  .map( <span class="hljs-function"><span class="hljs-params">s</span> =&gt;</span> <span class="hljs-string">`<span class="hljs-subst">$&#123;s[<span class="hljs-number">0</span>].toUpperCase()&#125;</span><span class="hljs-subst">$&#123;s.slice(<span class="hljs-number">1</span>)&#125;</span>`</span>)<br>  .join(<span class="hljs-string">' '</span>);<br></code></pre></td></tr></table></figure><figure class="highlight js"><figcaption><span>nested/maths.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> add = <span class="hljs-function">(<span class="hljs-params"> a, b </span>) =&gt;</span> a + b;<br><span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> increment = <span class="hljs-function">(<span class="hljs-params"> value, by=<span class="hljs-number">1</span> </span>) =&gt;</span> value + by;<br></code></pre></td></tr></table></figure><p>So, with all of the above coded imported into <code>onload.js</code> and it being imported by <code>bundle.ts</code> anytime you save any one of those files CodeKit is now smart enough to build your new <code>bundle.js</code> which will produce ES 5 code in the System format.</p><p>Here you can see CodeKit showing you what files are linked.<br><img src="/images/codekit-3-ES-Module-Bundling-with-TypeScript/onload.js-linked-files.png" alt=""></p><figure class="highlight js"><figcaption><span>bundle.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><code class="hljs js">System.register(<span class="hljs-string">"text"</span>, [], <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">exports_1, context_1</span>) </span>&#123;<br><span class="hljs-meta">  "use strict"</span>;<br>  <span class="hljs-keyword">var</span> __moduleName = context_1 &amp;&amp; context_1.id;<br>  <span class="hljs-keyword">var</span> upper, title;<br>  <span class="hljs-keyword">return</span> &#123;<br>    <span class="hljs-attr">setters</span>:[],<br>    <span class="hljs-attr">execute</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>&#123;<br>      exports_1(<span class="hljs-string">"upper"</span>, upper = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">text</span>) </span>&#123;<br>        <span class="hljs-keyword">if</span> (text === <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>) &#123; text = <span class="hljs-string">''</span>; &#125;<br>        <span class="hljs-keyword">return</span> text.toUpperCase();<br>      &#125;);<br>      exports_1(<span class="hljs-string">"title"</span>, title = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">text</span>) </span>&#123;<br>        <span class="hljs-keyword">if</span> (text === <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>) &#123; text = <span class="hljs-string">''</span>; &#125;<br>        <span class="hljs-keyword">return</span> text<br>          .toLowerCase()<br>          .split(<span class="hljs-string">' '</span>)<br>          .map(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">s</span>) </span>&#123; <span class="hljs-keyword">return</span> (<span class="hljs-string">""</span> + s[<span class="hljs-number">0</span>].toUpperCase() + s.slice(<span class="hljs-number">1</span>)); &#125;)<br>          .join(<span class="hljs-string">' '</span>);<br>      &#125;);<br>    &#125;<br>  &#125;<br>&#125;);<br>System.register(<span class="hljs-string">"nested/maths"</span>, [], <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">exports_2, context_2</span>) </span>&#123;<br><span class="hljs-meta">  "use strict"</span>;<br>  <span class="hljs-keyword">var</span> __moduleName = context_2 &amp;&amp; context_2.id;<br>  <span class="hljs-keyword">var</span> add, increment;<br>  <span class="hljs-keyword">return</span> &#123;<br>    <span class="hljs-attr">setters</span>:[],<br>    <span class="hljs-attr">execute</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>&#123;<br>      exports_2(<span class="hljs-string">"add"</span>, add = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">a, b</span>) </span>&#123; <span class="hljs-keyword">return</span> a + b; &#125;);<br>      exports_2(<span class="hljs-string">"increment"</span>, increment = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">value, by</span>) </span>&#123;<br>        <span class="hljs-keyword">if</span> (by === <span class="hljs-keyword">void</span> <span class="hljs-number">0</span>) &#123; by = <span class="hljs-number">1</span>; &#125;<br>        <span class="hljs-keyword">return</span> value + by;<br>      &#125;);<br>    &#125;<br>  &#125;<br>&#125;);<br>System.register(<span class="hljs-string">"onload"</span>, [<span class="hljs-string">"text"</span>, <span class="hljs-string">"nested/maths"</span>], <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">exports_3, context_3</span>) </span>&#123;<br><span class="hljs-meta">  "use strict"</span>;<br>  <span class="hljs-keyword">var</span> __moduleName = context_3 &amp;&amp; context_3.id;<br>  <span class="hljs-keyword">var</span> text_1, maths_1;<br>  <span class="hljs-keyword">return</span> &#123;<br>    <span class="hljs-attr">setters</span>:[<br>      <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">text_1_1</span>) </span>&#123;<br>        text_1 = text_1_1;<br>      &#125;,<br>      <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">maths_1_1</span>) </span>&#123;<br>        maths_1 = maths_1_1;<br>      &#125;],<br>    <span class="hljs-attr">execute</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>&#123;<br>      <span class="hljs-built_in">console</span>.log(text_1.upper(<span class="hljs-string">'yell a message here.'</span>));<br>      <span class="hljs-built_in">console</span>.log(text_1.title(<span class="hljs-string">'title case message here.'</span>));<br>      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"2 + 3 = "</span> + maths_1.add(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>));<br>      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"increment 4 by 1 = "</span> + maths_1.increment(<span class="hljs-number">4</span>));<br>      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"increment 4 by 3 = "</span> + maths_1.increment(<span class="hljs-number">4</span>, <span class="hljs-number">3</span>));<br>    &#125;<br>  &#125;<br>&#125;);<br>System.register(<span class="hljs-string">"bundle"</span>, [<span class="hljs-string">"onload"</span>], <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">exports_4, context_4</span>) </span>&#123;<br><span class="hljs-meta">  "use strict"</span>;<br>  <span class="hljs-keyword">var</span> __moduleName = context_4 &amp;&amp; context_4.id;<br>  <span class="hljs-keyword">return</span> &#123;<br>    <span class="hljs-attr">setters</span>:[<br>      <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">_1</span>) </span>&#123;&#125;],<br>    <span class="hljs-attr">execute</span>: <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>&#123;<br>    &#125;<br>  &#125;<br>&#125;);<br></code></pre></td></tr></table></figure><p>Now that we have our code bundled up via the System module format there’s one final step. As you can see above it’s using a global called <code>System</code> which “registers” each file into its own scope and passes in its dependencies. Because of this, we’ll need to load in part of the SystemJS library, I say part because we only need the “register” portion of it which we can grab from a CDN or load it locally.</p><figure class="highlight html"><figcaption><span>index.html</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">script</span><br>    <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span><br>    <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.40/system-register-only.js"</span>&gt;</span><span class="undefined"><br>  </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br>  <span class="hljs-tag">&lt;<span class="hljs-name">script</span><br>    <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span><br>    <span class="hljs-attr">src</span>=<span class="hljs-string">"js/bundle.js"</span><br>    <span class="hljs-attr">onload</span>=<span class="hljs-string">"System.import('bundle')"</span>&gt;</span><span class="undefined"><br>  </span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure><p>When we load in our <code>bundle.js</code> files we need to set the <code>onload</code> event to tell SystemJS to import our bundled up code so that it get executed.</p><h3 id="Setting-up-CodeKit-file-options"><a href="#Setting-up-CodeKit-file-options" class="headerlink" title="Setting up CodeKit file options"></a>Setting up CodeKit file options</h3><p>We’ve gone over each role of the files provided but we need to adjust CodeKit slightly for all of this to work correctly.</p><blockquote><p>If you enjoy this work flow you’ll probably want to edit CodeKits “project defaults” instead of doing this for every single new file or project.</p></blockquote><h4 id="TypeScript-settings"><a href="#TypeScript-settings" class="headerlink" title="TypeScript settings"></a>TypeScript settings</h4><p>We’ll want</p><ul><li><code>Output Module Type</code> System</li><li><code>ECMAScript Target Version</code> ES5</li><li><code>When This File Changes Or Builes</code> Compile it<br><img src="/images/codekit-3-ES-Module-Bundling-with-TypeScript/bundle.ts-options.png" alt=""></li></ul><h4 id="JavaScript-settings-only-if-you-want-to-lint-it-otherwise-set-it-to-ignore"><a href="#JavaScript-settings-only-if-you-want-to-lint-it-otherwise-set-it-to-ignore" class="headerlink" title="JavaScript settings (only if you want to lint it, otherwise set it to ignore)"></a>JavaScript settings (only if you want to lint it, otherwise set it to ignore)</h4><ul><li><code>Check Syntax With</code> ESLint</li><li><code>Transpile With</code> Nothing</li><li><code>When This File Changes Or Builes</code> Process it</li><li><code>To This Path</code> The exact path to the JavaScript file you’re processing<br><img src="/images/codekit-3-ES-Module-Bundling-with-TypeScript/onload.js-options.png" alt=""></li></ul><h4 id="ESLint-settings"><a href="#ESLint-settings" class="headerlink" title="ESLint settings"></a>ESLint settings</h4><ul><li><code>Source Type</code> Module</li><li><code>Environments</code> Check “ES6”<br><img src="/images/codekit-3-ES-Module-Bundling-with-TypeScript/ck-settings-eslint.png" alt=""></li></ul><h3 id="Github-sample-project"><a href="#Github-sample-project" class="headerlink" title="Github sample project"></a>Github sample project</h3><p>The code you see above is <a href="https://github.com/subhaze/CodeKit_3_ES_Module_Bundling_TS">on Github</a> with a CodeKit config file, I hope this helps folks that are looking for a way to write in the new ES syntax as well as start using ES Module imports to bundle up their code.</p><p>Happy coding!</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;Now that CodeKit 3 has launched it’s time to dig into some new functionality it provides; One of my favorites being, the ability for CodeKit to now track ES Module imports in your JavaScript and TypeScript files! This means we now have a true path on bundling up our JavaScript files without hooks.&lt;/p&gt;
&lt;p&gt;While some will want a more flushed out experience via Browserify or Rollup bundling, using TypeScript isn’t that bad, especially when you’re really only using one .ts to do the bundling while the rest of your code can be pure JavaScript.&lt;br&gt;
    
    </summary>
    
    
      <category term="CodeKit" scheme="https://useless.today/tags/CodeKit/"/>
    
      <category term="TypeScript" scheme="https://useless.today/tags/TypeScript/"/>
    
      <category term="ES Modules" scheme="https://useless.today/tags/ES-Modules/"/>
    
      <category term="Bundling JS" scheme="https://useless.today/tags/Bundling-JS/"/>
    
  </entry>
  
  <entry>
    <title>CodeKit + babel</title>
    <link href="https://useless.today/codekit-babel/"/>
    <id>https://useless.today/codekit-babel/</id>
    <published>2016-10-29T15:00:00.000Z</published>
    <updated>2019-06-22T01:41:13.000Z</updated>
    
    <content type="html"><![CDATA[<p><strong>[UPDATE]</strong> CodeKit 3 is now available and supports babel natively.</p><p>Currently <a href="https://incident57.com/codekit/">CodeKit</a> does not support <a href="https://babeljs.io/">babeljs</a> transpiling, <a href="https://github.com/bdkjones/CodeKit/issues/554#issuecomment-116611607">but appears it will at some point in the future</a>. This doesn’t mean you can’t use CodeKit to transpile babel, you’ll just need to install babel locally and use a hook to do so.<br><a id="more"></a><br>There’s one unfortunate thing with this hook, since we can’t trigger a hook on file save _(unless you’re running JSLint/JSHint, but the file name is not passed to the <code>CK_INPUT_PATHS</code> variable)_ we’ll need to create specific output files for our babel files so that CodeKit will trigger hooks. This adds a minor bit of complexity, but doable with a small amount of work.</p><p>With this hook there are three variables of interest to you, <code>SRC</code>, <code>DIST</code>, and <code>SUFFIX</code>.</p><p>SRC: </p><p>Is set to use a directory named <code>es6</code> so if you create your files in some other directory you’ll want to change that.</p><p>DIST: </p><p>Is where babel will save the transpiled files, currently that is set to <code>js</code>. Also, this folder <strong>must</strong> already exist, I may update this hook later so that it will create that folder if it does not already exist.</p><p>SUFFIX: </p><p>This one is needed since we can’t set CodeKit to trigger hooks on file save, hence adding a minor bit of complexity to this hook. So for each file you’re wanting this hook to trigger on, you need to set the output path to <code>*-babel.js</code> <em>(or whatever you change this var to)</em>.</p><p>Also, make sure when you set the output file it’s not saving the output in the root of the project but within the <code>es6</code> directory (or whatever directory you’re using). </p><p>Here’s the hook criteria:</p><ul><li><code>Any</code> of the following are true</li><li><code>the output path of any processed file</code> <code>ends with</code> <code>-babel.js</code></li><li>Run the following <code>Shell Script (/bin/bash)</code></li></ul><p>Here’s the hook code:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs bash"><br><span class="hljs-comment"># set CK_OUTPUT_PATHS to an empty string if it is not present</span><br><span class="hljs-variable">$&#123;CK_OUTPUT_PATHS:=""&#125;</span><br><br>PWD=`<span class="hljs-built_in">pwd</span>` <span class="hljs-comment"># path to directory CodeKit is watching  </span><br>SRC=<span class="hljs-string">"es6"</span> <span class="hljs-comment"># where our babel/es6 files will be  </span><br>DIST=<span class="hljs-string">"js"</span> <span class="hljs-comment"># where we want the transpiled files to go  </span><br>SUFFIX=<span class="hljs-string">"-babel.js"</span> <span class="hljs-comment"># the suffix we're using to trigger CodeKit Hooks</span><br><br><span class="hljs-comment"># split string into an array via ':'</span><br>CK_OUTPUT_PATHS=(<span class="hljs-variable">$&#123;CK_OUTPUT_PATHS//:/ &#125;</span>)<br><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-string">"<span class="hljs-variable">$&#123;CK_OUTPUT_PATHS[@]&#125;</span>"</span>  <br><span class="hljs-keyword">do</span>  <br>    <span class="hljs-comment"># We have are absolute file paths, what we want is relative paths</span><br>    relative_path=<span class="hljs-string">"<span class="hljs-variable">$&#123;i##$PWD/&#125;</span>"</span><br><br>    <span class="hljs-comment"># We need to put together an output path for the file</span><br>    <span class="hljs-comment"># So we prefix the the file with our $DIST folder after we've</span><br>    <span class="hljs-comment"># remove the path to our working directory and our source directory</span><br>    output_path=<span class="hljs-variable">$DIST</span>/<span class="hljs-string">"<span class="hljs-variable">$&#123;i##$PWD/$SRC/&#125;</span>"</span><br><br>    <span class="hljs-comment"># now we're just running the babel CLI command against the relative</span><br>    <span class="hljs-comment"># file path and telling it to save the file into our destination path</span><br>    <span class="hljs-comment"># but, remove the '-bable.js' suffix work around we're using to trigger hooks</span><br>    babel <span class="hljs-string">"<span class="hljs-variable">$&#123;relative_path&#125;</span>"</span> --out-file <span class="hljs-string">"<span class="hljs-variable">$&#123;output_path%$SUFFIX&#125;</span>"</span>.js<br><br>    <span class="hljs-comment"># now we're just cleaning up the workaround file we must create in order to use</span><br>    <span class="hljs-comment"># this hook within CodeKit</span><br>    rm <span class="hljs-string">"<span class="hljs-variable">$i</span>"</span><br><span class="hljs-keyword">done</span><br></code></pre></td></tr></table></figure><h5 id="Concat-amp-Minifying-the-Results"><a href="#Concat-amp-Minifying-the-Results" class="headerlink" title="Concat &amp; Minifying the Results"></a>Concat &amp; Minifying the Results</h5><p>You could create a <code>main.js</code> file that’s used to concat all your babel transpiled files and minify in one go.</p><p>The contents would look something like:</p><figure class="highlight js"><figcaption><span>main.js</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><br><span class="hljs-comment">// @codekit-append "path/to/js/file-one.js"</span><br><span class="hljs-comment">// @codekit-append "path/to/js/file-two.js"</span><br><span class="hljs-comment">// @codekit-append "path/to/js/file-three.js"</span><br></code></pre></td></tr></table></figure><p>From there you can set an output file for <code>main.js</code> and adjust the <code>Output style:</code> to your liking.</p><p><div class="tip"><br>I have ran into issues with the above concat/minify file not triggering, usually another save to my source file will trigger it.<br></div><br>Hopefully this will help you get up and running with babel and CodeKit or has inspired you to create your own hook with some other CLI tool.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;&lt;strong&gt;[UPDATE]&lt;/strong&gt; CodeKit 3 is now available and supports babel natively.&lt;/p&gt;
&lt;p&gt;Currently &lt;a href=&quot;https://incident57.com/codekit/&quot;&gt;CodeKit&lt;/a&gt; does not support &lt;a href=&quot;https://babeljs.io/&quot;&gt;babeljs&lt;/a&gt; transpiling, &lt;a href=&quot;https://github.com/bdkjones/CodeKit/issues/554#issuecomment-116611607&quot;&gt;but appears it will at some point in the future&lt;/a&gt;. This doesn’t mean you can’t use CodeKit to transpile babel, you’ll just need to install babel locally and use a hook to do so.&lt;br&gt;
    
    </summary>
    
    
      <category term="CodeKit" scheme="https://useless.today/tags/CodeKit/"/>
    
      <category term="CodeKit Hooks" scheme="https://useless.today/tags/CodeKit-Hooks/"/>
    
      <category term="babeljs" scheme="https://useless.today/tags/babeljs/"/>
    
  </entry>
  
  <entry>
    <title>CodeKit + HTML gzip</title>
    <link href="https://useless.today/codekit-html-gzip/"/>
    <id>https://useless.today/codekit-html-gzip/</id>
    <published>2016-10-29T15:00:00.000Z</published>
    <updated>2019-06-22T01:41:13.000Z</updated>
    
    <content type="html"><![CDATA[<p><strong>[UPDATE]</strong> CodeKit 3 is now available and hooks work a bit differently now. This “should” still work, but, I’ll try to have an update on this using the new hooks.</p><p>This is a quick write-up on how to gzip your HTML files with <a href="http://incident57.com/codekit/index.html">CodeKit</a> Version 2.3+ hooks; Jade is used in the example but any of the HTML processors available should work the same.<br><a id="more"></a><br>First you’ll need to setup a hook, the hook I’ve used is:</p><ul><li><code>Any</code> of the following are true</li><li><code>The filename of any processed file</code> <code>ends with</code> <code>.jade</code></li></ul><p>Now you’ll need to select <code>Shell Script (/bin/sh)</code> from the drop down and paste the follow into the textarea:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs bash"><br><span class="hljs-comment"># set CK_OUTPUT_PATHS to an empty string if it is not present</span><br><span class="hljs-variable">$&#123;CK_OUTPUT_PATHS:=""&#125;</span><br><br><span class="hljs-comment"># split string into an array via ':'</span><br>CK_OUTPUT_PATHS=(<span class="hljs-variable">$&#123;CK_OUTPUT_PATHS//:/ &#125;</span>)<br><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-string">"<span class="hljs-variable">$&#123;CK_OUTPUT_PATHS[@]&#125;</span>"</span>  <br><span class="hljs-keyword">do</span>  <br>    gzip -nc <span class="hljs-string">"<span class="hljs-variable">$i</span>"</span> &gt; <span class="hljs-string">"<span class="hljs-variable">$i</span>.gz"</span><br><span class="hljs-keyword">done</span><br></code></pre></td></tr></table></figure><p>The above code accesses a newly available environment variable that CodeKit is now setting <code>CK_OUTPUT_PATHS</code>. This variable is a <code>:</code> delimited string of all the files that were produced by the action defined in the hook above.</p><p>The line <code>gzip -nc $i &gt; $i.gz</code> takes the current HTML file, gzips it, and saves a new version of it with the <code>.gz</code> extension. If you just want to replace the current HTML file with no <code>.gz</code> extension all you should have to do is use <code>gzip -nc $i</code> instead which will convert the current html files into a gzipped version.</p><p>With this hook setup, if you save a jade file that only effects one file then only one file will be gzipped and if you change a layout that’s extended by many other files then all of those files will be gzipped.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;&lt;strong&gt;[UPDATE]&lt;/strong&gt; CodeKit 3 is now available and hooks work a bit differently now. This “should” still work, but, I’ll try to have an update on this using the new hooks.&lt;/p&gt;
&lt;p&gt;This is a quick write-up on how to gzip your HTML files with &lt;a href=&quot;http://incident57.com/codekit/index.html&quot;&gt;CodeKit&lt;/a&gt; Version 2.3+ hooks; Jade is used in the example but any of the HTML processors available should work the same.&lt;br&gt;
    
    </summary>
    
    
      <category term="CodeKit" scheme="https://useless.today/tags/CodeKit/"/>
    
      <category term="CodeKit Hooks" scheme="https://useless.today/tags/CodeKit-Hooks/"/>
    
      <category term="gzip" scheme="https://useless.today/tags/gzip/"/>
    
  </entry>
  
  <entry>
    <title>CodeKit + timestamps</title>
    <link href="https://useless.today/codekit-timestamps/"/>
    <id>https://useless.today/codekit-timestamps/</id>
    <published>2016-10-29T15:00:00.000Z</published>
    <updated>2019-06-22T01:41:13.000Z</updated>
    
    <content type="html"><![CDATA[<p><strong>[UPDATE]</strong> CodeKit 3 is now available and hooks work a bit differently now. This “should” still work, but, I’ll try to have an update on this using the new hooks.</p><p>Here is another quick post on using the new Hook variables added in <a href="http://incident57.com/codekit/index.html">CodeKit</a> 2.3, this time looking at adding timestamps to CSS files.<br><a id="more"></a><br>Even though this is focused on SCSS/CSS you should be able to take this example and modify it to work with any of the other preprocessing abilities  CodeKit has to offer. The main take away is, adding a unique placeholder for the timestamp in your source files and using <a href="https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/sed.1.html">sed</a> to replace it with a real timestamp when they’re processed by CodeKit.</p><p>The hook settings used are:</p><ul><li><code>Any</code> of the following are true</li><li><code>The filename of any processed file</code> <code>ends with</code> <code>.scss</code></li></ul><p>Then, we’ll need to select <code>Shell Script (/bin/sh)</code> from the select box.</p><p>Once that’s all set add the following to the textarea:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs bash"><br><span class="hljs-comment"># set CK_OUTPUT_PATHS to an empty string if it is not present</span><br><span class="hljs-variable">$&#123;CK_OUTPUT_PATHS:=""&#125;</span><br><br><span class="hljs-comment"># split string into an array via ':'</span><br>CK_OUTPUT_PATHS=(<span class="hljs-variable">$&#123;CK_OUTPUT_PATHS//:/ &#125;</span>)<br><br>now=$(date)<br><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-string">"<span class="hljs-variable">$&#123;CK_OUTPUT_PATHS[@]&#125;</span>"</span>  <br><span class="hljs-keyword">do</span>  <br>    sed -i <span class="hljs-string">''</span> -e <span class="hljs-string">"s/&#123;&#123;TIMESTAMP&#125;&#125;/<span class="hljs-variable">$&#123;now&#125;</span>/"</span> <span class="hljs-string">"<span class="hljs-variable">$i</span>"</span><br><span class="hljs-keyword">done</span>  <br><br></code></pre></td></tr></table></figure><p>We’re creating the timestamp with <code>now=$(date)</code> so if you wish to tweak the timestamp format you can <a href="http://www.cyberciti.biz/faq/linux-unix-formatting-dates-for-display/">find more information here</a>.</p><p>The line <code>sed -i -e &quot;s/{{TIMESTAMP}}/${now}/&quot; &quot;$i&quot;</code> is using sed to execute a find/replace inline so that we overwrite the current file <code>$i</code> with the timestamp we created above. Right now the script is expecting to find <code>{{TIMESTAMP}}</code> somewhere within the CSS file so that it can replace it.</p><p>So your SCSS file could look something like:</p><figure class="highlight scss"><figcaption><span>site.scss</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs scss"><br><span class="hljs-comment">/* &#123;&#123;TIMESTAMP&#125;&#125; */</span><br>@<span class="hljs-keyword">import</span> <span class="hljs-string">'somthing'</span>;<br>@<span class="hljs-keyword">import</span> <span class="hljs-string">'more'</span>;<br><br><span class="hljs-selector-tag">body</span>&#123;  <br>  ...<br>&#125;<br></code></pre></td></tr></table></figure><p>and you will end up with a CSS file like:</p><figure class="highlight scss"><figcaption><span>site.css</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs scss"><br><span class="hljs-comment">/* Sun Apr 26 09:52:32 EDT 2015 */</span><br><span class="hljs-selector-class">.something-css-here</span>&#123;<br>    ...<br>&#125;<br><span class="hljs-selector-class">.more-css-here</span>&#123;<br>    ...<br>&#125;<br><br><span class="hljs-selector-tag">body</span>&#123;<br>    ...<br>&#125;<br></code></pre></td></tr></table></figure>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;&lt;strong&gt;[UPDATE]&lt;/strong&gt; CodeKit 3 is now available and hooks work a bit differently now. This “should” still work, but, I’ll try to have an update on this using the new hooks.&lt;/p&gt;
&lt;p&gt;Here is another quick post on using the new Hook variables added in &lt;a href=&quot;http://incident57.com/codekit/index.html&quot;&gt;CodeKit&lt;/a&gt; 2.3, this time looking at adding timestamps to CSS files.&lt;br&gt;
    
    </summary>
    
    
      <category term="CodeKit" scheme="https://useless.today/tags/CodeKit/"/>
    
      <category term="CodeKit Hooks" scheme="https://useless.today/tags/CodeKit-Hooks/"/>
    
      <category term="timestamps" scheme="https://useless.today/tags/timestamps/"/>
    
  </entry>
  
  <entry>
    <title>Simple Ghost Blog Theme deploys with rsync</title>
    <link href="https://useless.today/simple-ghost-blog-theme-deploys-with-rsync/"/>
    <id>https://useless.today/simple-ghost-blog-theme-deploys-with-rsync/</id>
    <published>2015-04-26T13:51:10.000Z</published>
    <updated>2019-06-22T01:41:13.000Z</updated>
    
    <content type="html"><![CDATA[<p>So, I’ve finally jumped onto the Ghost Blog platform to give blogging a try. The first thing I wanted was an easy way to make updates to a theme and push those updates to my server and take care of the ghost restart.<br><a id="more"></a><br>This write-up assumes you’re using Ubuntu and upstart to handle the Ghost restart, however, that line of code can be easily modified to use whatever you have setup to restart Ghost on your server. The hard requirements are rsync on your local machine and server, ability to run Make, and SSH access to your server.</p><blockquote><p>By <strong>deploy</strong> I mean, syncing what’s on your local machine to a remote machine. So anything that’s been updated gets pushed to the remote server and anything you’ve deleted on your local gets removed on the remote server.</p></blockquote><p>For myself, the most basic way to deploy something is via <a href="http://en.wikipedia.org/wiki/Rsync">rsync</a> which basically just keeps files and folders synced between two computers. I’m using a Makefile to easily trigger a few CLI commands by typing <code>make</code> in my terminal when I want to deploy. This also allows me to add more features pretty easily when I find the need to.</p><p>So, first off, let’s look at what it takes to sync theme files from our local machine to our remote server which is the following one liner:</p><p><code>rsync -a --delete ./ username@example.com:/var/www/ghost/content/themes/YOUR_THEME</code></p><p>In the above we’re asking rsync to run in ‘<a href="http://serverfault.com/questions/141773/what-is-archive-mode-in-rsync">archive move</a>‘ and delete files on the remote that are not found on the local machine. You’ll need to update <code>username</code> to the user you SSH in as, <code>example.com</code> to the domain of the server you’re deploying to, and <code>/var/www/ghost/content/themes/YOUR_THEME</code> to the path of your theme.</p><p>So, we’ve got a nice one-liner for deploying files, but Ghost doesn’t know that we’ve made these changes. SSH to the rescue, we can make another one-liner to restart the Ghost service on our remote server which will look something like this:</p><p><code>ssh username@example.com service ghost restart</code></p><p>Remember this is assuming your using something like upstart and have a service setup to start/stop/restart Ghost. If you’re using a <a href="https://www.digitalocean.com/community/tutorials/how-to-use-the-digitalocean-ghost-application">Digital Ocean 1-Click Application</a> you’ll get this all setup for you.</p><p>Now that we have those two lines we can create a Makefile to trigger them for quick deploys. To do this, create a file called <code>Makefile</code> and save it along with your theme files, its contents should look something like this:</p><figure class="highlight bash"><figcaption><span>Makefile</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs bash"><br>deploy:  <br>    rsync -a --delete ./ username@example.com:/var/www/ghost/content/themes/YOUR_THEME<br>    ssh username@example.com service ghost restart<br><br>.PHONY: deploy<br></code></pre></td></tr></table></figure><p>With that file in place we can now quickly deploy via the terminal. Just fire up your terminal, <code>cd</code> into your theme directory you’re wanting to deploy and then <code>make</code> or <code>make deploy</code> and your files will get synced up on your remote server and Ghost will be restarted.</p><p>If you’re not familiar with Makefiles <a href="http://www.sitepoint.com/using-gnu-make-front-end-development-build-tool/">sitepoint has a nice write-up</a> on using them in the front-end world.</p>]]></content>
    
    <summary type="html">
    
      &lt;p&gt;So, I’ve finally jumped onto the Ghost Blog platform to give blogging a try. The first thing I wanted was an easy way to make updates to a theme and push those updates to my server and take care of the ghost restart.&lt;br&gt;
    
    </summary>
    
    
      <category term="deploy" scheme="https://useless.today/tags/deploy/"/>
    
      <category term="rsync" scheme="https://useless.today/tags/rsync/"/>
    
      <category term="make" scheme="https://useless.today/tags/make/"/>
    
      <category term="ghost-theme" scheme="https://useless.today/tags/ghost-theme/"/>
    
  </entry>
  
</feed>
