Quickly test drive the creation of a Grunt plugin. Part 1.
Have an idea that you would like to wrap into a Grunt plugin?
That was my exact situation as I wanted to be able to use the auto reloading behavior from the bootstrap-chrome-extension project in my Chrome extension work without manually copying and hooking up the reloading code for each new undertaking. After a good deal of trial and error I was able to publish the grunt-crx-auto-reload plugin. From that learning experience emerged this 3-part step-by-step tutorial series explaining how to create a Grunt plugin, TDD style.
AIM: Create a grunt plugin that will work in conjunction with grunt watch to automatically reload a Chrome extension.
- Given that core Chrome extension code has been modified a timestamped
reload.html
should be created in the root of the extension directory. - Another file,
reload.js
must be created if it does not exist in the root extension directory. - The
reload.js
must be configured as a background script in themanifest.json
. - The
reload.js
file must checkreload.html
every 1 second to see if the timestamp has changed. If the timestamp has changed then issue achrome.runtime.reload()
onreload.html
.
- Start by using
npm search gruntplugin [search term]
for 2 purposes. - To see if there already is a Grunt plugin that solves this problem.
- To help decide on a plugin name.
- After not finding another similar plugin, I settled on the plugin name
grunt-crx-auto-reload
. - Create a working directory and install the Yeoman generator
generator-gruntplugin
. - Run the generator-gruntplugin
- The generator creates a scaffolding that includes all the required node modules, Gruntfile, tests, README, and a sample task.
- The tests provided spec that sample task.
- The default task in the Gruntfile provided lints the code and runs the tests.
- Let's map a vim leader that will save the file and run the
grunt
command in the right tmux window pane. - Open
test/crx_auto_load_test.js
, clean out the boilerplate code, and create your first test. - Comment out the test expectation for the custom_options test and run the test.
- Let's go back to the Gruntfile and see what is going on for these
default_options
andcustom_options
tests. - Edit
tasks/crx_auto_load.js
, remove the boilerplate code, and replace with the code in below figure. - Save the file and run the tests.
$ cd ~/github/labs $ mkdir grunt-crx-auto-reload && cd $_ $ npm install -g generator-gruntplugin
$ yo gruntplugin

yo gruntplugin
.


Gruntfile.js
and explore what tasks have been setup.
grunt
command.
,zl
is typed the file is saved and tests run on the right window pane.ts
command is a bash function that shortens the tmux send-keys
to right pane command. Here is the source:
function ts { args=$@ tmux send-keys -t right "$args" C-m }
reload.html
in the Chrome extension development directory. The Chrome extension directory will be supplied by the plugin user. The generator-gruntplugin uses the /tmp
as a scratch directory for testing file creation. After each grunt
run the /tmp
directory is cleaned out.

reload.html
creation.grunt.file.exists()
is the above example. Coming up, grunt.template
will also be leveraged. Check out the Grunt API for other helpful builtins.

test.expect()
will prevent you from achieving green if they are incorrect.



grunt.file
.
Conclusion
This is a good place to take a breather. Although, I haven't done anything spectecular as of yet we have established a solid Grunt plugin foundation. Tutorials part 2 and 3 are coming but you can watch the screencasts now.
- Pushed on 03/17/2014 by Christian