From 437d0fcbe0d2a59a331a20d89d01fb46fac7bd39 Mon Sep 17 00:00:00 2001 From: Vishal Kadam Date: Sun, 8 Dec 2013 13:20:33 -0500 Subject: [PATCH] 0.2.4: Support custom file types. Always use latest for dependencies --- Gruntfile.js | 29 +++++-- README.md | 30 ++++++- package.json | 14 ++-- tasks/jsbeautifier.js | 78 ++++++++++++------- test/configFile_test.js | 10 +-- test/fileMapping_test.js | 30 +++++++ .../configFile/expected/test_expected.css | 3 + .../expected/test_expected.html | 0 .../expected/test_expected.js | 0 .../withGruntFileOptions/test_expected.css | 3 + .../withGruntFileOptions/test_expected.html | 0 .../withGruntFileOptions/test_expected.js | 0 .../jsbeautifyrc.json | 0 .../jsbeautifyrc_flat.json | 0 .../{config_file => configFile}/test.css | 0 .../{config_file => configFile}/test.html | 0 .../{config_file => configFile}/test.js | 0 .../config_file/expected/test_expected.css | 3 - .../withGruntFileOptions/test_expected.css | 3 - .../fileMapping/expected/beautified.css.erb | 3 + .../fileMapping/expected/beautified.html.erb | 12 +++ .../fileMapping/expected/beautified.js.erb | 4 + .../fileMapping/not-beautified.css.erb | 1 + .../fileMapping/not-beautified.html.erb | 7 ++ .../fileMapping/not-beautified.js.erb | 4 + 25 files changed, 178 insertions(+), 56 deletions(-) create mode 100644 test/fileMapping_test.js create mode 100644 test/fixtures/configFile/expected/test_expected.css rename test/fixtures/{config_file => configFile}/expected/test_expected.html (100%) rename test/fixtures/{config_file => configFile}/expected/test_expected.js (100%) create mode 100644 test/fixtures/configFile/expected/withGruntFileOptions/test_expected.css rename test/fixtures/{config_file => configFile}/expected/withGruntFileOptions/test_expected.html (100%) rename test/fixtures/{config_file => configFile}/expected/withGruntFileOptions/test_expected.js (100%) rename test/fixtures/{config_file => configFile}/jsbeautifyrc.json (100%) rename test/fixtures/{config_file => configFile}/jsbeautifyrc_flat.json (100%) rename test/fixtures/{config_file => configFile}/test.css (100%) rename test/fixtures/{config_file => configFile}/test.html (100%) rename test/fixtures/{config_file => configFile}/test.js (100%) delete mode 100644 test/fixtures/config_file/expected/test_expected.css delete mode 100644 test/fixtures/config_file/expected/withGruntFileOptions/test_expected.css create mode 100644 test/fixtures/fileMapping/expected/beautified.css.erb create mode 100644 test/fixtures/fileMapping/expected/beautified.html.erb create mode 100644 test/fixtures/fileMapping/expected/beautified.js.erb create mode 100644 test/fixtures/fileMapping/not-beautified.css.erb create mode 100644 test/fixtures/fileMapping/not-beautified.html.erb create mode 100644 test/fixtures/fileMapping/not-beautified.js.erb diff --git a/Gruntfile.js b/Gruntfile.js index 8ae409a..a9c8c03 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -18,24 +18,40 @@ module.exports = function(grunt) { mode: "VERIFY_ONLY" } }, + fileMapping: { + src: ["tmp/fileMapping/not-beautified.js.erb", + "tmp/fileMapping/not-beautified.css.erb", "tmp/fileMapping/not-beautified.html.erb" + ], + options: { + js: { + fileTypes: [".js.erb"] + }, + css: { + fileTypes: [".css.erb"] + }, + html: { + fileTypes: [".html.erb"] + } + } + }, configFile: { - src: ["tmp/config_file/test.js", - "tmp/config_file/test.css", "tmp/config_file/test.html" + src: ["tmp/configFile/test.js", + "tmp/configFile/test.css", "tmp/configFile/test.html" ], options: { - config: "tmp/config_file/jsbeautifyrc.json" + config: "tmp/configFile/jsbeautifyrc.json" } }, configFileFlat: { src: "<%= jsbeautifier.configFile.src %>", options: { - config: "tmp/config_file/jsbeautifyrc_flat.json" + config: "tmp/configFile/jsbeautifyrc_flat.json" } }, configFileWithGruntFileOptions: { src: "<%= jsbeautifier.configFile.src %>", options: { - config: "tmp/config_file/jsbeautifyrc_flat.json", + config: "tmp/configFile/jsbeautifyrc_flat.json", js: { indentSize: 3 }, @@ -50,7 +66,7 @@ module.exports = function(grunt) { }, copy: { tmp: { - src: "config_file/**", + src: ["configFile/**", "fileMapping/**"], dest: "tmp", cwd: "test/fixtures", expand: true @@ -74,7 +90,6 @@ module.exports = function(grunt) { boss: true, eqnull: true, node: true, - es5: true, camelcase: true } } diff --git a/README.md b/README.md index 93a0351..339abbf 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ grunt.loadNpmTasks('grunt-jsbeautifier'); ``` ### Grunt `~0.3.0` -Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: +Install this grunt plugin next to your project's [grunt.js gruntfile][getting_started] with: ``` npm install grunt-jsbeautifier@0.1.4 @@ -92,7 +92,7 @@ Type: `String` The file path (relative to your process's cwd) to the config file. This is usually in `JSON` format, like the one supported by js-beautify itself ### Default options from [js-beautify](https://github.com/einars/js-beautify#options) can be used -```javascript +```javascript "jsbeautifier": { files: ["src/**/*.js"], options: { @@ -138,7 +138,7 @@ Only specifiy options to overwrite. **NOTE:** All options can be specified similar to [js-beautify](https://github.com/einars/js-beautify#options) using underscore. -### Tips +### FAQ/Tips #### 1. Exclude files All files from foo folder except bar.js ```javascript @@ -149,7 +149,29 @@ jsbeautifier: { #### 2. Use specific version of js-beautify If you want use specific version of js-beautify instead of latest, please refer to [npm shrinkwrap](https://npmjs.org/doc/shrinkwrap.html) +#### 3. Beautify files other than js, json, css & html +If you need to beautify files other than js, json, css & html, it can be done by passing **fileTypes**. + +For example, beautifying **'.js.erb'** files **along with '.js'**, beautifying **'.less'** files **along with '.css'**, beautifying **'.html.erb'** files **along with '.html'**. +``` +jsbeautifier: { + files: ['foo/css/*.css', 'bar/css/*.less', 'foo/js/*.js', 'bar/js/*.js.erb', 'foo/html/*.html', 'bar/html/*.html.erb'], + options: { + js: { + fileTypes: ['.js.erb'] + }, + css: { + fileTypes: ['.less'] + }, + html: { + fileTypes: ['.html.erb'] + } + } +} +``` + ## Release History +* 0.2.4: Support custom file types other than js, json, css & html. Use latest versions for dependencies [23](https://github.com/vkadam/grunt-jsbeautifier/issues/23), [26](https://github.com/vkadam/grunt-jsbeautifier/issues/26) * 0.2.3: Always use latest version of js-beautify. [21](https://github.com/vkadam/grunt-jsbeautifier/issues/21) * 0.2.2: Configuration is now lint happy. Options can be specified in cameCase, [17](https://github.com/vkadam/grunt-jsbeautifier/issues/17) * 0.2.1: Added Support for flat config file @@ -167,5 +189,5 @@ If you want use specific version of js-beautify instead of latest, please refer * 0.1.0: Initial version. ## License -Copyright (c) 2012 Vishal Kadam +Copyright (c) 2012 Vishal Kadam Licensed under the MIT license. \ No newline at end of file diff --git a/package.json b/package.json index 1b32e72..fd7fae2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "grunt-jsbeautifier", "description": "jsbeautifier.org for grunt", - "version": "0.2.3", + "version": "0.2.4", "homepage": "https://github.com/vkadam/grunt-jsbeautifier", "author": { "name": "Vishal Kadam", @@ -23,13 +23,15 @@ }, "dependencies": { "grunt": "~0.4.1", - "js-beautify": "~1.4.0" + "js-beautify": "latest", + "lodash": "latest", + "underscore.string": "latest" }, "devDependencies": { - "grunt-contrib-jshint": "0.1.1", - "grunt-contrib-nodeunit": "0.1.2", - "grunt-contrib-copy": "0.4.1", - "grunt-contrib-clean": "0.5.0" + "grunt-contrib-jshint": "latest", + "grunt-contrib-nodeunit": "latest", + "grunt-contrib-copy": "latest", + "grunt-contrib-clean": "latest" }, "scripts": { "test": "grunt test -v" diff --git a/tasks/jsbeautifier.js b/tasks/jsbeautifier.js index 9f84e0b..1a6841b 100644 --- a/tasks/jsbeautifier.js +++ b/tasks/jsbeautifier.js @@ -3,6 +3,8 @@ module.exports = function(grunt) { var path = require('path'), jsBeautifier = require('js-beautify'), + lodash = require('lodash'), + stringUtils = require('underscore.string'), jsbeautifier = jsBeautifier.js, cssbeautifier = jsBeautifier.css, htmlbeautifier = jsBeautifier.html; @@ -15,7 +17,10 @@ module.exports = function(grunt) { grunt.task.registerMultiTask('jsbeautifier', 'jsbeautifier.org for grunt', function() { var params = this.options({ - mode: "VERIFY_AND_WRITE" + mode: "VERIFY_AND_WRITE", + js: {}, + css: {}, + html: {} }); var fileCount = 0; @@ -32,12 +37,10 @@ module.exports = function(grunt) { function convertCamelCaseToUnderScore(config) { var underscoreKey; - grunt.util._.each([config.js, config.css, config.html], function(conf) { - grunt.util._.each(conf, function(value, key) { - underscoreKey = key.replace(/([A-Z])/g, function($1) { - return "_" + $1.toLowerCase(); - }); - if (key !== underscoreKey) { + lodash.forEach([config.js, config.css, config.html], function(conf) { + lodash.forEach(conf, function(value, key) { + underscoreKey = stringUtils.underscored(key); + if ("fileTypes" !== key && key !== underscoreKey) { conf[underscoreKey] = value; delete conf[key]; } @@ -59,18 +62,21 @@ module.exports = function(grunt) { css: {}, html: {} }; - grunt.util._.extend(config.js, baseConfig); - grunt.util._.extend(config.css, baseConfig); - grunt.util._.extend(config.html, baseConfig); - grunt.util._.extend(config.js, baseConfig.js); - grunt.util._.extend(config.css, baseConfig.css); - grunt.util._.extend(config.html, baseConfig.html); - grunt.util._.extend(config.js, params.js); - grunt.util._.extend(config.css, params.css); - grunt.util._.extend(config.html, params.html); + lodash.extend(config.js, baseConfig); + lodash.extend(config.css, baseConfig); + lodash.extend(config.html, baseConfig); + lodash.extend(config.js, baseConfig.js); + lodash.extend(config.css, baseConfig.css); + lodash.extend(config.html, baseConfig.html); + lodash.extend(config.js, params.js); + lodash.extend(config.css, params.css); + lodash.extend(config.html, params.html); } else { config = params; } + config.js.fileTypes = lodash.union(config.js.fileTypes, ['.js', '.json']); + config.css.fileTypes = lodash.union(config.css.fileTypes, ['.css']); + config.html.fileTypes = lodash.union(config.html.fileTypes, ['.html']); grunt.verbose.writeln('Beautify config before converting camelcase to underscore: ' + JSON.stringify(config)); @@ -104,14 +110,15 @@ module.exports = function(grunt) { return; } - var beautifier = setup[0]; - config = setup[1]; + var beautifier = setup[0], + beautifyConfig = setup[1], + addNewLine = setup[2]; var original = grunt.file.read(file); grunt.verbose.write('Beautifing ' + file.cyan + '...'); - var result = beautifier(original, config); + var result = beautifier(original, beautifyConfig); // jsbeautifier would skip the line terminator for js files - if (['.js', '.json'].indexOf(path.extname(file)) !== -1) { + if (addNewLine) { result += '\n'; } grunt.verbose.ok(); @@ -121,18 +128,33 @@ module.exports = function(grunt) { } } + function getFileType(file, config) { + var fileType = null, + fileMapping = { + 'js': config.js.fileTypes, + 'css': config.css.fileTypes, + 'html': config.html.fileTypes + }; + lodash.forEach(fileMapping, function(extensions, type) { + fileType = type; + return -1 === lodash.findIndex(extensions, function(ext) { + return stringUtils.endsWith(file, ext); + }); + }); + return fileType; + } + function getBeautifierSetup(file, config) { - var ext = path.extname(file); - switch (ext) { - case '.js': - case '.json': - return [jsbeautifier, config.js]; - case '.css': + var fileType = getFileType(file, config); + switch (fileType) { + case 'js': + return [jsbeautifier, config.js, true]; + case 'css': return [cssbeautifier, config.css]; - case '.html': + case 'html': return [htmlbeautifier, config.html]; default: - grunt.fail.warn('Cannot beautify ' + file.cyan + ' (only .js, .css and .html are beautifiable)'); + grunt.fail.warn('Cannot beautify ' + file.cyan + ' (only js, css and html files can be beautified)'); return null; } } diff --git a/test/configFile_test.js b/test/configFile_test.js index d52f4ba..17be874 100644 --- a/test/configFile_test.js +++ b/test/configFile_test.js @@ -6,14 +6,14 @@ var grunt = require("grunt"), function beautifyAndassert(test, task, actualFile, expectedFile) { exec("grunt " + task, function() { - var actual = grunt.file.read("tmp/config_file/" + actualFile), - expected = grunt.file.read("tmp/config_file/" + expectedFile); + var actual = grunt.file.read("tmp/configFile/" + actualFile), + expected = grunt.file.read("tmp/configFile/" + expectedFile); test.equal(actual, expected, "should beautify js " + actualFile + " using config file"); test.done(); }); } - exports["config_file_test"] = { + exports["configFile_test"] = { "beautification of js file using settings from config file": function(test) { test.expect(1); beautifyAndassert(test, "jsbeautifier:configFile", "test.js", "expected/test_expected.js"); @@ -27,7 +27,7 @@ var grunt = require("grunt"), beautifyAndassert(test, "jsbeautifier:configFile", "test.html", "expected/test_expected.html"); } }; - exports["config_file_flat_test"] = { + exports["configFile_flat_test"] = { "beautification of js file using settings from flat config file": function(test) { test.expect(1); beautifyAndassert(test, "jsbeautifier:configFileFlat", "test.js", "expected/test_expected.js"); @@ -41,7 +41,7 @@ var grunt = require("grunt"), beautifyAndassert(test, "jsbeautifier:configFileFlat", "test.html", "expected/test_expected.html"); } }; - exports["config_file_with_gruntfile_options_test"] = { + exports["configFile_with_gruntfile_options_test"] = { "beautification of js file using settings from config file and gruntfile": function(test) { test.expect(1); beautifyAndassert(test, "jsbeautifier:configFileWithGruntFileOptions", "test.js", "expected/withGruntFileOptions/test_expected.js"); diff --git a/test/fileMapping_test.js b/test/fileMapping_test.js new file mode 100644 index 0000000..9d3eb36 --- /dev/null +++ b/test/fileMapping_test.js @@ -0,0 +1,30 @@ +"use strict"; +var grunt = require("grunt"), + exec = require("child_process").exec; + +(function() { + + function beautifyAndassert(test, task, actualFile, expectedFile) { + exec("grunt " + task, function(error, stdout, stderr) { + var actual = grunt.file.read("tmp/fileMapping/" + actualFile), + expected = grunt.file.read("tmp/fileMapping/" + expectedFile); + test.equal(actual, expected, "should beautify js " + actualFile + " using config file"); + test.done(); + }); + } + + exports["fileMapping_test"] = { + "beautification of js file using file mapping": function(test) { + test.expect(1); + beautifyAndassert(test, "jsbeautifier:fileMapping", "not-beautified.js.erb", "expected/beautified.js.erb"); + }, + "beautification of css file using file mapping": function(test) { + test.expect(1); + beautifyAndassert(test, "jsbeautifier:fileMapping", "not-beautified.css.erb", "expected/beautified.css.erb"); + }, + "beautification of html file using file mapping": function(test) { + test.expect(1); + beautifyAndassert(test, "jsbeautifier:fileMapping", "not-beautified.html.erb", "expected/beautified.html.erb"); + } + }; +})(); diff --git a/test/fixtures/configFile/expected/test_expected.css b/test/fixtures/configFile/expected/test_expected.css new file mode 100644 index 0000000..46bc3d3 --- /dev/null +++ b/test/fixtures/configFile/expected/test_expected.css @@ -0,0 +1,3 @@ +body { + font-size: 14px; +} \ No newline at end of file diff --git a/test/fixtures/config_file/expected/test_expected.html b/test/fixtures/configFile/expected/test_expected.html similarity index 100% rename from test/fixtures/config_file/expected/test_expected.html rename to test/fixtures/configFile/expected/test_expected.html diff --git a/test/fixtures/config_file/expected/test_expected.js b/test/fixtures/configFile/expected/test_expected.js similarity index 100% rename from test/fixtures/config_file/expected/test_expected.js rename to test/fixtures/configFile/expected/test_expected.js diff --git a/test/fixtures/configFile/expected/withGruntFileOptions/test_expected.css b/test/fixtures/configFile/expected/withGruntFileOptions/test_expected.css new file mode 100644 index 0000000..6876e98 --- /dev/null +++ b/test/fixtures/configFile/expected/withGruntFileOptions/test_expected.css @@ -0,0 +1,3 @@ +body { + font-size: 14px; +} \ No newline at end of file diff --git a/test/fixtures/config_file/expected/withGruntFileOptions/test_expected.html b/test/fixtures/configFile/expected/withGruntFileOptions/test_expected.html similarity index 100% rename from test/fixtures/config_file/expected/withGruntFileOptions/test_expected.html rename to test/fixtures/configFile/expected/withGruntFileOptions/test_expected.html diff --git a/test/fixtures/config_file/expected/withGruntFileOptions/test_expected.js b/test/fixtures/configFile/expected/withGruntFileOptions/test_expected.js similarity index 100% rename from test/fixtures/config_file/expected/withGruntFileOptions/test_expected.js rename to test/fixtures/configFile/expected/withGruntFileOptions/test_expected.js diff --git a/test/fixtures/config_file/jsbeautifyrc.json b/test/fixtures/configFile/jsbeautifyrc.json similarity index 100% rename from test/fixtures/config_file/jsbeautifyrc.json rename to test/fixtures/configFile/jsbeautifyrc.json diff --git a/test/fixtures/config_file/jsbeautifyrc_flat.json b/test/fixtures/configFile/jsbeautifyrc_flat.json similarity index 100% rename from test/fixtures/config_file/jsbeautifyrc_flat.json rename to test/fixtures/configFile/jsbeautifyrc_flat.json diff --git a/test/fixtures/config_file/test.css b/test/fixtures/configFile/test.css similarity index 100% rename from test/fixtures/config_file/test.css rename to test/fixtures/configFile/test.css diff --git a/test/fixtures/config_file/test.html b/test/fixtures/configFile/test.html similarity index 100% rename from test/fixtures/config_file/test.html rename to test/fixtures/configFile/test.html diff --git a/test/fixtures/config_file/test.js b/test/fixtures/configFile/test.js similarity index 100% rename from test/fixtures/config_file/test.js rename to test/fixtures/configFile/test.js diff --git a/test/fixtures/config_file/expected/test_expected.css b/test/fixtures/config_file/expected/test_expected.css deleted file mode 100644 index 8b5ce9d..0000000 --- a/test/fixtures/config_file/expected/test_expected.css +++ /dev/null @@ -1,3 +0,0 @@ -body { - font-size:14px; -} \ No newline at end of file diff --git a/test/fixtures/config_file/expected/withGruntFileOptions/test_expected.css b/test/fixtures/config_file/expected/withGruntFileOptions/test_expected.css deleted file mode 100644 index b54cc22..0000000 --- a/test/fixtures/config_file/expected/withGruntFileOptions/test_expected.css +++ /dev/null @@ -1,3 +0,0 @@ -body { - font-size:14px; -} \ No newline at end of file diff --git a/test/fixtures/fileMapping/expected/beautified.css.erb b/test/fixtures/fileMapping/expected/beautified.css.erb new file mode 100644 index 0000000..da4f62e --- /dev/null +++ b/test/fixtures/fileMapping/expected/beautified.css.erb @@ -0,0 +1,3 @@ +body { + font-size: 14px; +} \ No newline at end of file diff --git a/test/fixtures/fileMapping/expected/beautified.html.erb b/test/fixtures/fileMapping/expected/beautified.html.erb new file mode 100644 index 0000000..38c10fc --- /dev/null +++ b/test/fixtures/fileMapping/expected/beautified.html.erb @@ -0,0 +1,12 @@ + + + + + Page Title + + + +
Body Content
+ + + \ No newline at end of file diff --git a/test/fixtures/fileMapping/expected/beautified.js.erb b/test/fixtures/fileMapping/expected/beautified.js.erb new file mode 100644 index 0000000..a3f8e76 --- /dev/null +++ b/test/fixtures/fileMapping/expected/beautified.js.erb @@ -0,0 +1,4 @@ +var temp = { + hello: 1, + world: 2 +}; diff --git a/test/fixtures/fileMapping/not-beautified.css.erb b/test/fixtures/fileMapping/not-beautified.css.erb new file mode 100644 index 0000000..b47aae9 --- /dev/null +++ b/test/fixtures/fileMapping/not-beautified.css.erb @@ -0,0 +1 @@ +body{font-size:14px;} \ No newline at end of file diff --git a/test/fixtures/fileMapping/not-beautified.html.erb b/test/fixtures/fileMapping/not-beautified.html.erb new file mode 100644 index 0000000..0a34c3b --- /dev/null +++ b/test/fixtures/fileMapping/not-beautified.html.erb @@ -0,0 +1,7 @@ + + + Page Title + + +
Body Content
+ \ No newline at end of file diff --git a/test/fixtures/fileMapping/not-beautified.js.erb b/test/fixtures/fileMapping/not-beautified.js.erb new file mode 100644 index 0000000..154f589 --- /dev/null +++ b/test/fixtures/fileMapping/not-beautified.js.erb @@ -0,0 +1,4 @@ +var temp = { + hello: 1, +world: 2 +};