Testing Whether an NPM Module Is Installed

Node doesn’t have a well documented way of checking whether a module is available. This is usually fine. You just include a package.json in your project, and expect people to run npm install. This is how most popular Node frameworks work.

But what if you have a devDependency entry that you want to include only if it is installed? I searched for an answer and didn’t find anything conclusive, so I’m documenting what I did find in case I forget later.

Buried in NPM’s package.json documentation is an example for handling a package that may or may not be installed. In short, you use a try/catch. Here’s an example:

var program;

try {
    program = require('commander');
} catch (er) {
    // Any code to run if this module doesn't exist
}

if (program) {
    program.version('0.0.1') .option('-n, --name[name]', 'Your name')
         .parse(process.argv);
}

Pass Dynamic Flags to NuGet With PowerShell Splatting

While working on a PowerShell script for packaging multiple NuGet packages, I discovered a peculiarity in calling NuGet with PowerShell splatting. I wanted to do something like the following:

The problem is, even though I’ve “Splatted” $nugetArgs into my NuGet call, the way NuGet handles arguments causes it to choke on the HashSet of arguments, and throw an error like the following:

Continue reading

Using “Controller as” Syntax in Angular Routes

I’ve been using the new “Controller as” syntax in Angular for a few months, but today I realized I didn’t know how to use the syntax for route controllers. After spending a few minutes scratching my head and “Googling with Bing”, I figured it out. The $routeProvider supports a “controllerAs” property for routes:

$routeProvider
    .when('/', {
        templateUrl: 'Views/Home/Index.html',
        controller: 'HomeCtrl',
        controllerAs: 'vm'
    })
    .otherwise({ redirectTo: '/' });

That’s it. Not very complicated, but hopefully it will save someone a few minutes of research.

Creating an Angular Markdown Preview

I’ve been exploring Angular over the last few months, and I really like the way it works.

Because I like to use Markdown for things like my dev journal, I have been wanting to implement my own Markdown preview tool that I could customize to work just the way I want. When I first started learning Angular, I decided to see how easy it would be to write an Angular Markdown preview tool using Angular. Turns out it’s super simple.

If you’d rather skip straight to the demo, you can view the finished markdown preview.

I won’t go into great detail about the purpose of Angular, since it’s readily available elsewhere. In this project, I just wanted a simple way to bind to a textbox so that I could update an HTML Markdown preview. Angular’s declarative binding makes it an excellent choice for this.

Rendering an Angular Markdown Preview

Since I didn’t want to write my own markdown converter, I decided to look for an existing one. I quickly settled on this markdown npm package. I installed the package, then moved the converter code into my ‘lib/js’ folder so that it would be accessible from my web app.

To make the front-end as simple as possible, I decided to create an Angular filter to provide the conversion from Markdown to HTML.

The code is pretty simple. The markdown npm package adds a “markdown” object to the “window” global, so it’s easy to access the converter.

var app = angular.module('app', [])

    .filter('markdown', function() {
        return function(text) {
            if (typeof text == "undefined") {
                return "";
            }
            return markdown.toHTML(String(text));
        }
    });

New Plugin: Minimum Password Strength

A couple of years ago I released a plugin called “Minimum Password Length“. It’s been pretty popular, mostly thanks to some link love by David Peralty. Ever since I released the plugin, I’ve been thinking “wouldn’t it be nice to do something similar, but harness the WordPress password strength meter?”. Well, I finally took the time to do that, and so we have the Minimum Password Strength plugin.

This is also a special plugin, because it’s the first one I’ve simultaneously published on GitHub and my site. I plan on uploading my other plugins to GitHub at some point (in fact, I uploaded Short Comment Filter, another popular plugin, a few days ago), but Minimum Password Strength is the first one that’s there from the start.

Working With GitHub in Windows

I’m working with GitHub for a school project this quarter. It’s the first time I’ve done anything but dabble with Git, so it took some time for me to figure it out. The commands weren’t that bad, because they’re pretty similar to SVN (which I work with from the command line all the time), but getting set up in Windows was a bit of a pain. Turns out if you install Git for Windows with the “Use PuTTY” option, it uses PuTTY ssh keys to authenticate with Git. This wouldn’t be a big deal, except PuTTY uses a special private key format, and I already have a keypair that I use on my Mac. I finally figured out how to convert that to PuTTY format using PuTTYgen, and then I figured out that I needed to start Pageant and manually add the key in order to use it for authentication.

Anyway, despite all this pain, I decided to start using GitHub to host my open source WordPress plugins. This gives me some more experience with Git, plus it provides public source control, which is better for open source projects than the private SVN repository I was already using.

Long story short, if you want to see what I’ve uploaded so far, check out my GitHub page.