Skip to content

Commit

Permalink
0.2.4: Support custom file types. Always use latest for dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
vkadam committed Dec 8, 2013
1 parent de42163 commit 437d0fc
Show file tree
Hide file tree
Showing 25 changed files with 178 additions and 56 deletions.
29 changes: 22 additions & 7 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -50,7 +66,7 @@ module.exports = function(grunt) {
},
copy: {
tmp: {
src: "config_file/**",
src: ["configFile/**", "fileMapping/**"],
dest: "tmp",
cwd: "test/fixtures",
expand: true
Expand All @@ -74,7 +90,6 @@ module.exports = function(grunt) {
boss: true,
eqnull: true,
node: true,
es5: true,
camelcase: true
}
}
Expand Down
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",

This comment has been minimized.

Copy link
@bmoeskau

bmoeskau Feb 27, 2014

Been trying to figure out why I am now getting the following warning when npm updating my project, and it seems like this commit caused it:

npm WARN unmet dependency /Users/Brian/Dropbox/Projects/L7/retriever/node_modules/grunt-jsbeautifier requires underscore.string@'latest' but will load
npm WARN unmet dependency /Users/Brian/Dropbox/Projects/L7/retriever/node_modules/underscore.string,
npm WARN unmet dependency which is version 2.3.3

What's the rationale for forcing users of your package to use the latest version of all your dependencies? For me to run things in a predictable (read: production) environment I can't just grab the latest of everything, but now I can't do anything with npm without beautifier yelling at me.

"grunt-contrib-nodeunit": "latest",
"grunt-contrib-copy": "latest",
"grunt-contrib-clean": "latest"
},
"scripts": {
"test": "grunt test -v"
Expand Down
78 changes: 50 additions & 28 deletions tasks/jsbeautifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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];
}
Expand All @@ -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));

Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
}
Expand Down
10 changes: 5 additions & 5 deletions test/configFile_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand Down
30 changes: 30 additions & 0 deletions test/fileMapping_test.js
Original file line number Diff line number Diff line change
@@ -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");
}
};
})();
3 changes: 3 additions & 0 deletions test/fixtures/configFile/expected/test_expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
font-size: 14px;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
font-size: 14px;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 0 additions & 3 deletions test/fixtures/config_file/expected/test_expected.css

This file was deleted.

This file was deleted.

3 changes: 3 additions & 0 deletions test/fixtures/fileMapping/expected/beautified.css.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
font-size: 14px;
}
12 changes: 12 additions & 0 deletions test/fixtures/fileMapping/expected/beautified.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>Page Title</title>
</head>

<body>
<div>Body Content</div>
</body>

</html>
4 changes: 4 additions & 0 deletions test/fixtures/fileMapping/expected/beautified.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
var temp = {
hello: 1,
world: 2
};
Loading

0 comments on commit 437d0fc

Please sign in to comment.