<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>A Unique Blog Title</title>
 <link href="http://www.matthewstump.com/atom.xml" rel="self"/>
 <link href="http://www.matthewstump.com"/>
 <updated>2016-07-16T00:25:31+00:00</updated>
 <id>http://www.matthewstump.com</id>
 <author>
   <name>Matt Stump</name>
   <email>mstump@matthewstump.com</email>
 </author>

 
 <entry>
   <title>Writing Node.js modules in ClojureScript</title>
   <link href="http://www.matthewstump.com/misc/2012/06/04/writing-nodejs-modules-in-clojurescript"/>
   <updated>2012-06-04T00:00:00+00:00</updated>
   <id>http://www.matthewstump.com/misc/2012/06/04/writing-nodejs-modules-in-clojurescript</id>
   <content type="html">&lt;p&gt;At SourceNinja we recently expanded our &lt;a href=&quot;http://www.sourceninja.com/&quot;&gt;offering to track Node.js&lt;/a&gt;, and as part of that we released a &lt;a href=&quot;https://github.com/SourceNinja/sourceninja-node&quot;&gt;Node.js module&lt;/a&gt;. I love &lt;a href=&quot;http://clojure.com/&quot;&gt;Clojure&lt;/a&gt;, and used this opportunity to finally take a look at &lt;a href=&quot;https://github.com/clojure/clojurescript&quot;&gt;ClojureScript&lt;/a&gt;, and specifically using &lt;a href=&quot;http://nodejs.org/&quot;&gt;Node.js&lt;/a&gt; as a deploy target.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://mmcgrana.github.com/2011/09/clojurescript-nodejs.html&quot;&gt;There&lt;/a&gt; is some documenation for running ClojureScript on top of Node.js, but so far no documentation for deploying a module written in ClojureScript to npm. Think of this post as a report describing what worked, what didn’t and where we found gaps. I’m going to assume you know Clojure, and that you have installed Node.js. If you haven’t installed Node.js yet I highly recommend you use &lt;a href=&quot;https://github.com/creationix/nvm&quot;&gt;nvm&lt;/a&gt;, it’s the bee’s knees.&lt;/p&gt;

&lt;h2 id=&quot;getting-started&quot;&gt;Getting Started&lt;/h2&gt;
&lt;p&gt;Let’s get started. Create a new project directory. You’re going to need two sub-folders, one named src, and another named lib. src is for your raw ClojureScript, the lib direcory will contain the code generated by the ClojureScript compiler.&lt;/p&gt;

&lt;p&gt;Start out with a simple ClojureScript program&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2870806.js?file=stage1.cljs&quot;&gt; &lt;/script&gt;

&lt;p&gt;As stated in &lt;a href=&quot;https://github.com/clojure/clojurescript/wiki/Quick-Start&quot;&gt;other guides&lt;/a&gt;, in order to make this program run with Node.js you need to specify a main by setting the value of &lt;code class=&quot;highlighter-rouge&quot;&gt;*main-cli-fn*&lt;/code&gt; to some function.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2870806.js?file=stage2.cljs&quot;&gt; &lt;/script&gt;

&lt;p&gt;To compile the ClojureScript we use the incantation below. cljsc will take all &lt;code class=&quot;highlighter-rouge&quot;&gt;*.cljs&lt;/code&gt; files found in the src directory, compile them to js and spit the result out via stdout. We pipe the output to a js file matching the module name in the lib directory. The fact that we use the lib directory is important, because that’s where Node.js is going to look for code.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2870806.js?file=build.sh&quot;&gt; &lt;/script&gt;

&lt;p&gt;The only option which is stricly neccessary is &lt;code class=&quot;highlighter-rouge&quot;&gt;:target&lt;/code&gt; which should be set to &lt;code&gt;:nodejs&lt;/code&gt;. This option generates a main functions which in turn calls the function specified by &lt;code&gt;*main-cli-fn*&lt;/code&gt;. While the other are not required they will make this whole process go much smoother. We found that setting the optimizations level to anything other than &lt;code&gt;:simple&lt;/code&gt; produced unreliable code which was difficult to debug. For our purposes heavy optimization wasn’t neccessary so I didn’t invest much effort in getting it to work, give it a shot, your millage may vary.&lt;/p&gt;

&lt;p&gt;Most of your debugging time is going to be tracing through the generated JS code, for this reason we always enable :pretty-print which results in human-readable code being generated.&lt;/p&gt;
&lt;h2&gt;Export Functions&lt;/h2&gt;
&lt;p&gt;At this point we have a fully functional Node.js program. When we run the generated code we should see the following.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2870806.js?file=output.txt&quot;&gt; &lt;/script&gt;

&lt;p&gt;Great it works, but every time your module is included it’s going to run whatever function is bound to &lt;code class=&quot;highlighter-rouge&quot;&gt;*main-cli-fn*&lt;/code&gt;. Leaving it unbound isn’t an option so we need to bind it to a noop function. The final form is below.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2870806.js?file=stage3.cljs&quot;&gt; &lt;/script&gt;

&lt;p&gt;To turn this program into a module we need a couple small additions. The first is that we need to export/extern the public functions so that they are visible to node. We do this by adding a file &lt;code class=&quot;highlighter-rouge&quot;&gt;lib/main.js&lt;/code&gt;.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2870806.js?file=main.js&quot;&gt; &lt;/script&gt;

&lt;p&gt;You need to import the generated javascript code outputed by the ClojureScript compiler, this is done by the &lt;code class=&quot;highlighter-rouge&quot;&gt;require(&quot;./sample.js&quot;)&lt;/code&gt;. The actual export is done by the next line &lt;code&gt;exports.core = sample.sample.core&lt;/code&gt;, this makes the sample.core namespace defined by our ClojureScript program visible to node.js. All functions declared by your clojure namespace should now be accessible.&lt;/p&gt;

&lt;h2 id=&quot;publishing-the-module&quot;&gt;Publishing the Module&lt;/h2&gt;
&lt;p&gt;The last addition is the &lt;code class=&quot;highlighter-rouge&quot;&gt;packages.json&lt;/code&gt; file which lives at your project root. You can find &lt;a href=&quot;http://package.json.nodejitsu.com/&quot;&gt;lots&lt;/a&gt; &lt;a href=&quot;http://blog.nodejitsu.com/package-dependencies-done-right&quot;&gt;of documentation&lt;/a&gt; for this &lt;a href=&quot;http://npmjs.org/doc/json.html&quot;&gt;file&lt;/a&gt;, so I won’t spends lots of time on it, I’ll just emphasise a couple points.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Spend some time verifying which version of Node.js introduced the functionality you require.&lt;/strong&gt; For instance we use npm programatily, but the functionality wasn’t introduced until 6.0 which is newer than the verion deployed by Heroku. Node.js is very young, and is a bit of a moving target, spending a little time up front will prevent you from being burned. Using nvm becomes invaluable when attempting to validate your module against different versions of node. Oh, and please populate the repository and license fields, it’s just part of being a good open source citizen.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2870806.js?file=packages.json&quot;&gt; &lt;/script&gt;

&lt;p&gt;At this point from your project root you can execute &lt;code class=&quot;highlighter-rouge&quot;&gt;npm publish&lt;/code&gt; and npm will walk you through creating an account and publish your module to npm.&lt;/p&gt;

&lt;h2 id=&quot;pro-tips&quot;&gt;Pro Tips&lt;/h2&gt;
&lt;p&gt;When passing data from ClojureScript to Node.js your going to need to convert it from a ClojureScript data st
ructure to a javascript object. There have been &lt;a href=&quot;https://gist.github.com/1098417&quot;&gt;various&lt;/a&gt; &lt;a href=&quot;http://maurits.wordpress.com/2012/02/13/first-clojurescript-experiences-using-raphael/&quot;&gt;examples&lt;/a&gt; published of how to do this on the interwebs, but all were buggy. We settled on the following, which worked for us, but it is admittedly limited.&lt;/p&gt;

&lt;script src=&quot;https://gist.github.com/2870806.js?file=util.cljs&quot;&gt; &lt;/script&gt;

&lt;h2 id=&quot;testing&quot;&gt;Testing&lt;/h2&gt;
&lt;p&gt;Unit testing can be done using the built-in &lt;a href=&quot;http://richhickey.github.com/clojure/clojure.test-api.html&quot;&gt;clojure.test&lt;/a&gt; API, and that seems to be the recommended method. It should be possible to test functionality via one of the Node.js frameworks such as vows, but that’s another blog post. If you want to test the integration between other Node.js programs and your newly minted module you can create a test program which requires your module and then copy your module to the &lt;code class=&quot;highlighter-rouge&quot;&gt;node_modules&lt;/code&gt; sub-directory which allows you to do pre-publish testing. This is what we do as part of our last round of testing before we publish.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Don't Start a Company</title>
   <link href="http://www.matthewstump.com/misc/2010/10/11/dont-start-a-company"/>
   <updated>2010-10-11T00:00:00+00:00</updated>
   <id>http://www.matthewstump.com/misc/2010/10/11/dont-start-a-company</id>
   <content type="html">&lt;p&gt;I can’t start that new company. I don’t know how to do sales. I don’t know how to dev. I don’t understand business. I don’t understand financing. I don’t have enough money saved. The economy isn’t doing well. I don’t have enough time to work on my idea. Family life is too hectic. I’m the sole breadwinner. I’ve got young kids at home. Things are really interesting at work. I want to finish this one project. A bunch of people left and the team depends on me. A raise is right around the corner. Things are getting better. There is too much uncertainty. I’m scared.&lt;/p&gt;

&lt;h2 id=&quot;its-never-a-good-time&quot;&gt;It’s Never A Good Time&lt;/h2&gt;
&lt;p&gt;I’m not dismissing your reasons for not starting a company. You may have very valid reasons for not wanting to jump ship. Who am I to judge? The truth is, it’s never a good time to start a company. Life is never going to offer up that opportune time where all the planets align and all obstacles melt before you. Being successful is about making the best out of the hand you’re given. It’s about being determined/persistant/foolhardy enough to push through adversity and do what’s difficult.&lt;/p&gt;

&lt;p&gt;Two days after I quit my cushy corporate job my wife gave birth to our first son. Yeah, it’s been less than ideal and at times, it sucks. No matter what you do you are either neglecting your family or your business. My cofounder is the breadwinner for his family of three, and I guarantee that fact weighs on his conscious all the time. Both of us had jobs which paid us well, had coworkers we liked, and we were respected. I’m not telling you these things to show you how awesome we are, I’m telling you so that you know you’re not alone.&lt;/p&gt;

&lt;h2 id=&quot;dont-wait&quot;&gt;Don’t Wait&lt;/h2&gt;
&lt;p&gt;If you’re waiting for someone to tell you it’s going to be ok, and everything will work out, that person isn’t coming. You’re going to have to take that leap on your own. Be under no illusions, starting a company is hard. It’s the hardest thing I’ve done. In fact, it is the hardest thing I’ve done other than be a parent. But, if you have the stamina, the determination, and the will, do it.&lt;/p&gt;

&lt;p&gt;What’s the worst that can happen?&lt;/p&gt;

&lt;p&gt;If you fail, you haven’t. You have attempted something that few are willing to try. You can always get a corporate job and you will never say to yourself, “what if?”&lt;/p&gt;
</content>
 </entry>
 
 
</feed>