Skip to content

Instantly share code, notes, and snippets.

@edrabc
Last active January 1, 2016 20:39
Show Gist options
  • Save edrabc/8198493 to your computer and use it in GitHub Desktop.
Save edrabc/8198493 to your computer and use it in GitHub Desktop.
Grunt template to build JS modules, with pretty-printing and code static validation.
module.exports = function (grunt) {
grunt.loadNpmTasks("grunt-git-changedfiles");
grunt.loadNpmTasks("grunt-jsbeautifier");
grunt.loadNpmTasks("grunt-contrib-jshint");
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
code: ["Gruntfile.js", "src/**/*.js", "lib/**/*.js", "test/**/*.js"],
// Check https://github.com/einars/js-beautify#options
jsbeautifier_options_js: {
braceStyle: "collapse",
breakChainedMethods: false,
indentChar: " ",
indentLevel: 0,
indentSize: 2,
indentWithTabs: false,
jslintHappy: true,
keepArrayIndentation: false,
keepFunctionIndentation: false,
maxPreserveNewlines: 10,
preserveNewlines: true,
spaceBeforeConditional: true,
spaceInParen: false,
unescapeStrings: false,
wrapLineLength: 140
},
jsbeautifier: {
"default": {
src: "<%= code %>",
options: {
js: "<%= jsbeautifier_options_js %>"
}
},
"changed": {
src: [],
options: {
js: "<%= jsbeautifier_options_js %>"
}
},
"verify": {
src: "<%= code %>",
options: {
mode: "VERIFY_ONLY",
js: "<%= jsbeautifier_options_js %>"
}
}
},
jshint: {
// Check http://www.jshint.com/docs/options
options: {
curly: true,
eqeqeq: true,
eqnull: true,
node: true,
indent: 2,
maxlen: 140,
maxcomplexity: 10,
globals: {},
},
all: "<%= code %>"
}
});
grunt.registerTask("_beautify_changes", "", function () {
// Make sure git_changed task has run before.
grunt.task.requires("git_changedfiles");
var changed = grunt.config.get("git.changed");
if (changed && changed[0]) {
grunt.config.set('jsbeautifier.changed.src', changed);
grunt.task.run("jsbeautifier:changed");
} else {
grunt.log.writeln("No files to prettify.");
}
});
grunt.registerTask("_beautify", "", function (args) {
if (grunt.option('file')) {
grunt.config.set('jsbeautifier.changed.src', grunt.option('file'));
grunt.task.run("jsbeautifier:changed");
} else {
grunt.task.run("jsbeautifier:default");
}
});
grunt.registerTask("verify", "Verify ALL files have the proper quality standards to be committed.", [
"jsbeautifier:verify",
"jshint:all"
]);
grunt.registerTask("beautify", "Beautify a single file (with --file flag) or ALL JS files.", ["_beautify"]);
grunt.registerTask("beautify-changed", "Beautify the code of only modified files.", ["git_changedfiles", "_beautify_changes"]);
};
{
...
"devDependencies": {
"grunt": "0.4.5",
"grunt-git-changedfiles": "0.0.2",
"grunt-jsbeautifier": "0.2.7",
"grunt-contrib-jshint": "0.10.0",
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment