Author Archives: Mode54

How To Create NodeJS CLI Module And Published To NPM

I recently published my first public NodeJS module a couple months ago. It wasn’t my first CLI module but the first time I published something to NPM repository. I figured it would be a good idea to document and share the process.

The module I created is based on a command line script I put together a while back to scratch an itch I had with JSON files. I deal with JSON files a lot and most times they’re minified. There are a bunch of tools to prettify JSON but most of them are web based or in text editors. To avoid having to copy/paste JSON between screens I started searching for CLI scripts. Most of the ones I found either did too much or too little. I wanted a simple tool to prettify or minify JSON. So I wrote one called pretty-mini-json.

The code used to prettify and minify JSON was extremely simple. NodeJS has a built in JSON parser conveniently called JSON. It can be used to parse JSON strings into a proper JSON object and convert them back into strings. The JSON.stringify() function is used to convert them back but it can also generate a pretty or minified version. That’s all I needed at the core of this script. The rest is exposing this to the CLI, making it a proper NodeJS module and publishing it to NPM repository.

Create NodeJS Command Line Interface
The first thing I had to include in my script to make it command line friendly was the shebang line.

#!/usr/bin/env node

If your not familiar with the shebang and how it’s used then check out the wiki page.

Next I needed to parse the arguments passed into my script. This can be done from scratch by parsing process.argv. If your tool is simple then I recommend that route. But I wanted to add a number of options to my script so I choose to use an existing module to help parse arguments and make it easy to present those options. A module called commander fit perfectly.

So I added commander to my package.json file (more on that in a bit). Then added the module to my script using this line…

var program = require("commander");

Now I can chain together the way I want to parse the arguments using the commender object I stored as program.

program
    .version(pkgJSON.version)
    .usage("[options] [file ...]")
    .description("A simple CLI tool to shrink/minify or prettify JSON data.")
    .option("-p, --pretty", "prettify JSON data", "true")
    .option(
        "-o, --outputFile ",
        "write output to  instead of stdout"
    )
    .option(
        "-v, --verbose",
        "makes stdout more verbose/talkative. Mostly useful for debugging.",
        "true"
    )
    .parse(process.argv);

I won’t go into all of the functions available via the commander object. The main two to focus on for now are the option() and parse() functions. If you want to learn more about commander then check out the module readme.

The option() function is pretty straight forward. It defines all the options your script will process. The first argument defines the option letter and name. Second argument is the description and the third is the default value.

The last function called is parse() which is also straight forward. It will parse the process.argv and store the results in the program object using the option names defined in the option() function calls.

Now I can use the stored option values to return either prettified or minified JSON data. The only other CLI related bit that’s in the module is the standard input check and event handlers. I use those to check if there was any standard data stream passed into the script, collect the data and then process it. NodeJS provides the process.stdin for this. I also don’t output any logging data via console.log since many CLI users want to pipe the output of one script to another or some other process. For debugging I added the -v or --verbose option if user wants logs displayed.

That’s it! Now the script is CLI ready. Next I need to wrap this up into a package and publish to NPM repo.

Create NPM Package
NPM is included in the NodeJS install. You can update it separately by running sudo npm install npm -g. It started out as the Node Package Manager but now it’s used for many types of JavaScript packages. But I’m gonna create a traditional NodeJS package. The first thing I need to do is create a package.json file.

You can create the package.json file manually or via the npm init command. I prefer to do it manually in my text editor. Make sure the file is created in the directory that contains your NodeJS module. Typically this directory would have the same name as your module. The package.json file only requires two fields…

{
    "name": "pretty-mini-json",
    "version": "1.0.0"
}

Both are pretty straight forward. Keep in mind that name value should be the same as the parent directory and the version value should be based on semantic versioning. This is all that’s required but I had some more fields I wanted to use since this would be published to NPM repo.

{
  "name": "pretty-mini-json",
  "description": "A simple CLI tool to shrink/minify or prettify JSON data.",
  "keywords": [
    "cli",
    "util",
    "json",
    "minify",
    "shrink",
    "pretty",
    "prettify",
    "pipe",
    "stdout",
    "stdin"
  ],
  "author": "Mode54 <info@mode54.com> (http://Mode54.com)",
  "repository": {
    "type": "git",
    "url": "git://github.com/Mode54/pretty-mini-json.git"
  },
  "bugs": {
    "url": "https://github.com/Mode54/pretty-mini-json/issues"
  },
  "main": "./pretty-mini-json.js",
  "version": "1.0.0",
  "dependencies": {
    "commander": "2.8.x"
  },
  "license": "MIT",
  "bin": {
    "pretty-mini-json": "pretty-mini-json.js",
    "pmj": "pretty-mini-json.js"
  }
}

I won’t go into each field. You can checkout this handy interactive guide to find out what each field is for. The two I want to point out are the dependencies field which includes any modules the script will depend on (in my case the commander module) and the bin field which contains binary scripts to expose to the CLI. I’m using the bin field as a way to create a couple aliases to the same script.

Now I’m ready to publish this bad boy to the public NPM repo!

Publish NodeJS Package To NPM
I need to register with NPM in order to publish to the repository. This can be done online using the sign up form but I just did it from the CLI since I’d have to run the same command either way.

npm adduser

I enter my username, password and email (this email will be visible to the public). Then check to make sure I have an account created on the NPM repo by going to https://www.npmjs.com/~mode54. Replace mode54 with your own user name. Also made sure user info was stored locally by running this CLI command:

npm config ls

Once the user info is set then I can run the publish command to send my package up to NPM repository

npm publish

Yay!! My first NodeJS CLI script published to NPM!!

Top 15 Movies Programmers Should Watch

This is a follow up to my list of documentaries programmers should watch. Movies are a bit different since they don’t always have a direct connection to programming but have a lot to do with hacker lifestyles. Most of the movies in my list are oldies but goodies. Enjoy!

  1. Matrix (1999)
    I’m sure you’re not surprised that this is my top pick. If you are a programmer and you haven’t seen this movie then something is terribly wrong with you. Or maybe you just don’t like movies.
  2. Tron (1982)
    This use to be my top pick until Matrix came along. It’s old but the content is still good. The funny thing is this has a lot in common with the Matrix. In this movie the programmer gets zapped into the computer world. The effects used in this movie were a big deal back in the day.
  3. Primer (2004)
    A movie about a group of geeks building a startup in a garage that takes a strange turn. This is the type of movie you have to watch a couple times to understand but it’s worth it. I like subtle sci-fi movies like this.
  4. Pirates of Silicon Valley (1999)
    Probably the best movie about how Apple, Microsoft and the rivalry between them got started. This movie even impressed the real Steve Jobs. He even had the actor that played him in the movie kick off one of his his Macworld keynotes. If you haven’t seen this then you need to fire up Netflix right now and enjoy. Probably only available as DVD though.
  5. Ghost in the Shell (1995)
    This not your average cartoon. If you’re into anime then this is a must see. My guess is if you like anime you’ve seen this already but for those new to anime then this is a good start.
  6. The Social Network (2010)
    If you can get past the many inaccurate parts of this movie in relation to the real world Marc Zuckerberg then this movie will be entertaining. I do like the fact that they did get the hacking in the movie right. It’s not easy to make programming fun to watch on screen but they pulled it off a bit in this movie. It kinda helped my wife understand what I do.
  7. Girl with the Dragon Tattoo (2011)
    A hacker movie that takes on the dark thriller genre. It’s based on the popular novel about a hacker girl with social issues helping a journalist solve a murder mystery. Here’s the original Swedish version (2009) of the movie. This should be a trilogy but it doesn’t look like the US version will continue. The Swedish version was made into a trilogy.
  8. Sneakers (1992) This is a hacker movie that stars Robert Redford, Sidney Poitier, Dan Aykroyd and the late River Phoenix. Even Mary McDonnell, who later played President Laura Roslin in Battlestar Galactica, has a role in this movie.
  9. Office Space (1999) By far the best programmer comedy movie out there. The only other movie I watch more than this one is probably The Matrix.
  10. War Games (1983) Probably the most popular hacker movie from the 80s. Pure fiction but it probably convinced a lot of people that kids can somehow start wars from home using computers.
  11. Weird Science (1985) The only other comedy movie on this list. Staring Anthony Michael Hall who was in a lot of movies I enjoyed from the 80s and is also in Pirates of Silicon Valley. Also look out for Bill Paxton and young Robert Downey Jr. There’s not much hacking in this movie but if you were into programming when you were young like I was… one of your dreams was being able to use your skill to create what the kids in this movie created.
  12. Catch Me If You Can (2002) I know, this movie has nothing to do with programmers. But it has a lot to do with hacking. Social engineering is a way to hack humans and this movie is all about that. Based on a true story which makes it even more interesting. It stars two of the biggest names in Hollywood, Tom Hanks and Leonardo DiCaprio. More importantly its directed by Steven Spielberg.
  13. Tron: Legacy (2010) This movie falls short of a good story but it makes up for it with great visuals and a cool soundtrack. Plus I got a thing for Tron and this sequel includes some of the original actors.
  14. Takedown (2000) Now this is a controversial one. Based on the life of famous hacker Kevin Mitnick, this movie attempts to tell the story of how Kevin became a world famous hacker. But it’s filled with holes and doesn’t really tell the real story. So if you are a Kevin Mitnick fan then just like The Social Network try to ignore the inaccurate depiction of the real person. If you do that then this is a decent movie. If you want to know the true story of Kevin then read his book.
  15. Antitrust (2001) I don’t know why I like this movie. It kinda sucks but I keep watching it. Maybe it has something to do with the jabs at Bill Gates or the dot com hype. Most likely is probably because it reminds me of my dot com days. I really shouldn’t like this but I do. Let me know what you think.

There it is! I left out crapy movies like Hackers and The Net but if you liked those movies or others then let me know. Maybe you can convince me to change my top 15.

Our New Titanium Mobile Module: ViewScrollr Pro

About a month ago we released a free Titanium Mobile Module on GitHub which added some much needed features to Ti.UI.ScrollableView object. Before it’s release I had a number features I wanted to add based on a couple projects I was working on. ViewScrollr Pro is the result of those ideas.

ViewScrollr Pro adds YouTube and Vimeo slides simply by adding the video id. There is also support for animated view blocks in each slide. You can have them slide in from the top, bottom, left or right. This works much like many JavaScript browser sliders (ex. Royal Slider). Both iOS and Android devices are supported.

I can’t wait to see what apps are created with this module. Please contact us if you have any feature request or bug reports.

Download ViewScrollr Pro from CodeCanyon.

StoreApp v1.4 Released… Now Supports Android!

StoreApp 1.4

Hello again!

I know it’s been awhile but we finally got StoreApp version 1.4 done. This is a big deal because this release now adds support Android devices. This is something we promised awhile back and now it’s here.

Along with Android support we also updated the PayPal calls to use the latest versions of the PayPal module. Version 1.6.1 for iOS and 2.2.1 for Android.

Ten Documentaries Programmers Should Watch

I was motivated to post this list after talking to some young programmers. They had a number of questions about the lifestyle of programmers and how successful and unsuccessful we can be. Most of the advice I gave them were from the documentaries listed below and some from my personal experience. I added links to the ones available on the web. Others will have links to IMDB.

  1. Triumph of the Nerds: The Rise of Accidental Empires
    One of the best documentaries about the old days (70s-90s). It covers the stories of a couple major computer companies (software & hardware).
  2. Nerds 2.0.1: A Brief History of the Internet
    This is the follow up to Triumph of the Nerds. Some content is the same as the first but still worth watching.
  3. Tetris: From Russia with Love
    Video games is what got me into programming. I tried to write arcade games for my Tandy 1000EX. This documentary is about a talented Russian programmer and the crazy world of video game development in the 70s & 80s.
  4. Project Code Rush
    This is the story of the amazing rise and fall of Netscape. Need I say more?
  5. The Pixar Story
    A motivating look at how engineers and artists worked together to create one of the most successful tech entertainment companies ever.
  6. id Software – All Your History
    Not sure you can really classify this as a documentary but I had to add it. The programmers at id changed the gaming world with some clever code.
  7. E-Dreams
    Not every story has a good ending. Kozmo.com was a rushed and over funded startup. Not a bad idea though… Amazon is testing this model in a couple markets.
  8. Startup.com
    More from the bubble days. Learn this well… an idea and vc money does not equal solid business.
  9. The History of Hacking
    At the end of the day we are all hackers. Learn your history!
  10. Revolution OS
    I was a big fan of the Linux OS back in the days. Now I get my command line action from OS X but I still got love for the OS that had Micro$oft shaking.

I also have a list for some of the best programmer movies that are very different from whats already out there. Look out for that list coming soon.

If you have any feedback or want to debate this list then please leave a comment below.

**UPDATE**
It’s been a while since I thought about this list. Looking at it now I feel I need to add a couple new documentaries that I like. I don’t have enough for a new list so I’ll just mention them here…

  1. Indie Game: The Movie
    If I had to re-write this blog post then this would be my top documentary. This is a great view into the indie world of game programers and designers. I’d love to see a documentary like this for non-game programming. Might not be as popular but still… interesting.
  2. Downloaded
    Remember Napster? This documentary tells the story of the programmers behind the controversial app and the company they started. The thing I love about this story is the fact that a kid wrote a piece of software that forced a huge industry to change its ways. The change wasn’t complete until Apple came along with iTunes but Napster did the hard work and paid for it in the end.
  3. Atari: Game Over
    Awesome doc about the history of Atari and one if the most talked about crappy games made for the Atari 2600. The best part is the story about the programmer who wrote that game. It pretty much ended his career even though he made some of the biggest hits for Atari. Guess you’re only as good as your last project.
  4. Terms and Conditions May Apply
    Does anyone really read the terms and conditions for most of the software or web services they use? This one might scare you.
  5. Print The Legend
    Not really related to programming but a great insight into hacking of physical objects. I’m really interested in how this will allow programmers to build physical things from there computer in their office.

**UPDATE 2**
I posted my list of movies programmers should watch. Fifteen movies all together… check it out!

StoreApp Template v1.3

We released a new version of our StoreApp Titanium template (v1.3) with a couple major bug fixes. This version also has some code re-factoring in preparation for the Android release. Here’s the details:

Version 1.3.0

  • [Fix] When two matching items with different options are in the cart and one is deleted… the last to be added is removed which may not be the one selected for deletion. This is fixed in this version
  • [Fix] App would crash when multiple items are removed using the edit mode in the cart. This is also fixed
  • Re-factor code in preparation for Android version

 Download this update here

Go Ahead, Fork Us!

We created a GitHub account to host all our freebie scripts and apps. To get things started we added our first freebie… a Titanium Mobile module called Indicator.js.

This module gives your app a simple indicator modal complete with spinner and message. You can size, position and update the message as you see fit.

Copy the module file to your project Resources folder. You can include it in your app using the require function:

var myIndicator = require('Indicator');

Then display the indicator modal when you need it:

// display with default settings
myIndicator.show();

And hide it when your done:

myIndicator.hide();

The show() method accepts a settings object to adjust the modal. Here’s an example of each setting:

myIndicator.show({
  message : "One Sec...",
  width : 100,
  height : 100,
  top : 50,
  bottom : 50,
  left : 50,
  right : 50
});

The size and position settings work the same as you would expect in a Titanium view or window. Currently this module will only work for iOS apps but we will update with Android support soon.

Enjoy!

Envato Twins

We created a twin account on the Envato marketplace called ClearCaseToo.

Why?
Appcelerator introduced their new marketplace back in September and we believe it’s time to start selling our products there. Envato has been great to us but we need to branch out and generate enough revenue to continue to support and build new modules and templates. In order to keep our new products on CodeCanyon we had to create this new account with a non-exclusive author setting. So expect to see all of our new Titanium modules and templates on ClearCaseToo and our upcoming Appcelerator marketplace account.

What about the StoreApp template and Location Proximity module?
Those products will remain CodeCanyon exclusive. We don’t plan on moving them to the new account mainly because there isn’t a clean way to do so. Envato doesn’t provide a way to transfer them without abandoning existing buyers (we have about 101 so far). We also can’t flag some items as exclusive and others as non-exclusive.

So there it is… One new marketplace account and another one coming up on Appcelerator. We’ll keep you posted once we have a new product and we’ll also share the stats since Appcelerator doesn’t make them public.

 

Hello world!

We finally have our blog up and running. Soon this will be open to the world.

Our two products continue to do well in the CodeCanyon marketplace. We will continue to support them and release new versions as soon as possible. Here’s a brief description of each product for anyone new to Mode54.

StoreApp Template
StoreApp is a complete iOS store application template for Appcelerator Titanium projects. This template is great for retail business owners or consultants who build solutions for retail business owners. Both hard and soft goods. Use this template to quickly create a mobile store for your business or for your client.

Location Proximity Module
This module quickly gives your Titanium mobile application the ability to get the distance between two points on the globe. You can even track the device location and get updated distance value as it moves.

Check them out and let us know what you think.