Using Codepainter to Format your Node.js App

I previously posted about using jshint to catch style errors in your code. To be sure, it’s way, way, way better to have a style formatter automatically apply your chosen style right within your editor–or at least flag your errors as soon as possible after you make them. But, neither of these will help you if you have a bunch of code that you need to get into shape; then you need a tool that will actually transform your code in abatch. Codepainter will apply selected transforms to your code as a grunt task–giving you improved code as a result. There are a few caveats, but it worked for me. First, install codepainter and grunt-codepainter using npm. Codepainter gets configured in grunt like this:


codepainter: {
static: {
options: {
editorConfig: false,
style: {
indent_style: 'tab',
trim_trailing_whitespace : true,
indent_size : 1,
max_line_length : 100,
quote_type : 'single',
curly_bracket_next_line : false,
spaces_around_operators : true,
space_after_control_statements : true,
space_after_anonymous_functions : true,
spaces_in_brackets : false
}
},
files: [{
expand: true, // Enable dynamic expansion
cwd: 'app/', // Src matches are relative to this path
src: ['+(controllers|services)/**/*.js'], // Actual patterns to match
dest: 'styled/' // Destination path prefix
}]
},
},

Add a line to register the task:

grunt.registerTask('styleme', 'codepainter');

That’s it. When your run grunt styleme, codepainter will apply the specified style transforms to the files that it finds using the cwd: and src: entries, and puts the transformed files in the directory you specify in dest:. It preserves the directory structure in the output, so I ended up with styled/controllers and styled/services, including subdirectories. Copy these directories back into the directory you specified in cwd:, and you now have the styled files in your project. You should, of course, back up those directories beforehand, copy the files over, and then run all of your tests afterwards. You can use grunt-contrib-copy and set up a chain of tasks to format, copy, and test.

But use extreme caution, here. This is not something I can recommend that you do regularly–if you do–please set it up so that it’s automated and goof-proof! The real holy grail for me remains something that will correct my style as I write, and flag errors that it can’t correct. But codepainter will save you from having to make thousands of style corrections by hand, if you ever face such a situation!

Advertisements

About jeffmershon

Director of Program Management at SiriusXM.
This entry was posted in Software and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s