Permalink
Browse files

39: Run beautifier on single file

  • Loading branch information...
1 parent 5ac91fd commit 381b7aab2f3b5fecd714c44221f5a0e9943651a1 @vkadam committed Apr 9, 2016
Showing with 120 additions and 6 deletions.
  1. +55 −0 README.md
  2. +18 −5 lib/jsbeautifier.js
  3. +41 −0 test/commandLineArgs_spec.js
  4. +6 −1 test/mockTask.js
View
@@ -66,6 +66,61 @@ grunt.loadNpmTasks("grunt-jsbeautifier");
}
```
+### Run single file
+
+You can run jsbeautifier on a single file by passing runtime command line argument.
+Its possible to combine this command with existing jsbeautifier task, so config from specified task can be used for beautifying mentioned file.
+
+#### Run with multi task
+If you have multi task configured like this,
+
+```javascript
+"jsbeautifier" : {
+ "default": {
+ src : ["path/to/some/files/*.js"],
+ options:{
+ js: {
+ indentSize: 5
+ }
+ }
+ }
+}
+```
+
+and want to beautify a file which is lets say under
+```
+/tmp/some/non/configured/file.js
+```
+
+then run command like
+```
+grunt jsbeautifier:default:file:/tmp/some/non/configured/file.js
+```
+
+#### Run with single task
+If you have single task configured like this,
+
+```javascript
+"jsbeautifier" : {
+ src : ["path/to/some/files/*.js"],
+ options:{
+ js: {
+ indentSize: 5
+ }
+ }
+}
+```
+
+and want to beautify a file which is lets say under
+```
+/tmp/some/non/configured/file.js
+```
+
+then run command like
+```
+grunt jsbeautifier::file:/tmp/some/non/configured/file.js
+```
+
## Config
#### files
View
@@ -8,6 +8,14 @@ var path = require("path"),
JsBeautifierTask = function(task) {
// Store reference to original task
this.task = task;
+ var args = {};
+
+ task.args.forEach(function(item, index) {
+ if (index % 2 === 0) {
+ args[item] = task.args[index + 1];
+ }
+ });
+ this.args = args;
// Merge task options with defaults
this.options = task.options(JsBeautifierTask.DEFAULT_OPTIONS);
@@ -99,7 +107,8 @@ function beautify(file, config, actionHandler) {
}
JsBeautifierTask.prototype.run = function() {
- var options = this.options;
+ var options = this.options,
+ args = this.args;
var fileCount = 0,
changedFileCount = 0,
@@ -167,7 +176,7 @@ JsBeautifierTask.prototype.run = function() {
var sourceFiles = this.task.files,
done = this.task.async();
- if (sourceFiles && sourceFiles.length > 0) {
+ if ((sourceFiles && sourceFiles.length > 0) || !_.isEmpty(args.file)) {
if (!_.isEmpty(options.dest)) {
grunt.verbose.writeln("All beautified files will be stored under \"" + options.dest + "\" folder");
if (!stringUtils.endsWith(options.dest, "/")) {
@@ -207,9 +216,13 @@ JsBeautifierTask.prototype.run = function() {
done();
};
- sourceFiles.forEach(function(fileset) {
- q.push(fileset.src);
- });
+ if (!_.isEmpty(args.file)) {
+ q.push(args.file);
+ } else {
+ sourceFiles.forEach(function(fileset) {
+ q.push(fileset.src);
+ });
+ }
});
}
};
@@ -0,0 +1,41 @@
+"use strict";
+/*jshint -W079*/
+var chai = require("chai"),
+ expect = chai.expect,
+ ncp = require('ncp').ncp,
+ grunt = require("grunt"),
+ JsBeautifierTask = require("../lib/jsbeautifier"),
+ createMockTask = require("./mockTask");
+
+chai.use(require('chai-fs'));
+/*jshint -W030*/
+describe("JsBeautifier: Command Line Parameter test", function() {
+ var mockTask;
+
+ beforeEach(function(done) {
+ grunt.file.mkdir("tmp/commandLine");
+ ncp("test/fixtures/common", "tmp/commandLine", done);
+ });
+
+ afterEach(function() {
+ mockTask = null;
+ grunt.file.delete("tmp");
+ });
+
+ function assertBeautifiedFile() {
+ var actual = "tmp/commandLine/not-been-beautified.js",
+ expected = grunt.file.read("tmp/commandLine/been-beautified.js");
+ expect(actual).to.have.content(expected, "should beautify js file passed via command line");
+ }
+
+ it("Verify beautification of file passed via command line", function(done) {
+ var task;
+ mockTask = createMockTask({}, [], ["file", "tmp/commandLine/not-been-beautified.js"], function() {
+ assertBeautifiedFile();
+ done();
+ });
+
+ task = new JsBeautifierTask(mockTask);
+ task.run();
+ });
+});
View
@@ -3,7 +3,11 @@
var grunt = require("grunt"),
_ = grunt.util._;
-module.exports = function(taskOptions, files, done) {
+module.exports = function mockTask(taskOptions, files, args, done) {
+ if (_.isFunction(args)) {
+ done = args;
+ args = [];
+ }
return {
_taskOptions: taskOptions,
files: [{
@@ -12,6 +16,7 @@ module.exports = function(taskOptions, files, done) {
options: function(defs) {
return _.defaults(this._taskOptions, defs);
},
+ args: args || [],
async: function() {
return done || function() {};
}

0 comments on commit 381b7aa

Please sign in to comment.