How Do I Release My Own Grunt Plugin?
Come up with a thing you need
If you're anything like me, doing anything twice is excruciating. Automation is wonderful and is forever, so let's take advantage of it. Maybe you've got a part of your build system where you want to use pngcrush to compress those image files down to the smallest they can be, and you're perfectly ok writing a node wrapper for the binary. Awesome. Let's turn this thing into a Grunt plugin.
For scaffolding the best (only?) tool out there is grunt-init
To get grunt-init running, it's as easy as using npm install
npm install -g grunt-init
Then you just need to make folder for your grunt-init templates. You can do this by going to your home directory, and creating a hidden folder called .grunt-init.
$ mkdir .grunt-init
You then just need to clone the templates you want to use into that folder. The most commonly used templates are available here.
They even have instructions on the template pages for this.
You want the Gruntfile one though, so you can run this to install (on Unix systems):
git clone https://github.com/gruntjs/grunt-init-gruntfile.git ~/.grunt-init/gruntfile
Now, you can make that project!
$ mkdir grunt-pngcrush
$ cd !$
$ grunt-init gruntplugin
Grunt-init will ask you a set of questions about the project, for naming/testing/publishing purposes. You can just hit enter if you want take the suggested answer.
Now, let's make sure it worked!
$ npm install
Git To It
$ git init
$ git add .gitignore
$ git ci -m "Initial commit"
$ git add --all
$ git ci -m "Scaffolding!"
Using git to keep track of your things during this whole process is important. I suggest making your first commit your .gitignore file. That way, if you messed something up during scaffolding, you can revert everything going back to the .gitignore. Believe me, this is recommended.
Let's talk about tests. I used to tell new developers at a company I once worked that handing over code without attached tests is like telling them to screw off and that your time is more important than theirs. I'm obviously into testing things. Your plugin should be no exception. Here's the thing though, you likely won't be writing a lot of small and easy to maintain functions for your task. It will likely be a glued together set of a few testable functions that are part of a library where you've already written said tests.
For this circumstance, I tend to use a couple of integration tests posed at unit tests. Luckily, the gruntplugin scaffolding already gave us some examples! Pop into the tests to check it out.
When you have looked those over, figure out what you're going to do, and maybe written a test or two because TDD is amazing, hack away! In my case, the file where the magic is happening is in "tasks/pngcrush.js." Yours should be somewhere similar.
Once you have your tests written and everything working swimmingly, go ahead and test the package by creating a fake project in another folder, then running npm install on this folder.
$ cd ..
$ mkdir test-grunt-pngcrush
$ cd !$
$ npm install ../grunt-pngcrush/
If this installs for you and runs from your other directory, it's time to publish this thing to NPM!
Publishing to NPM!
First thing's first, create an account at https://npmjs.org.
Next up, set up the account on your machine.
$ npm adduser
Now follow the prompts.
Time to publish!
$ npm publish
And you're done! Tell everybody about it! Freak out a little bit! Your code is on the internet! You can find more details for publishing here.