Skip to content

Commit

Permalink
added option for testing (read only property); failure robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Hauch committed Jun 27, 2016
1 parent ddddd2c commit 1a591c6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 16 deletions.
12 changes: 9 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

"use strict";

var path = require("path");

module.exports = function(grunt) {

// load local tasks
Expand All @@ -41,6 +39,14 @@ module.exports = function(grunt) {
build: {
src: "test/source.js",
dest: "test/result.js"
},

// set "readOnly" flag for testing
test: {
options: {
readOnly: true
},
src: "test/src/*.js"
}
}
});
Expand All @@ -49,5 +55,5 @@ module.exports = function(grunt) {
grunt.loadNpmTasks("grunt-contrib-nodeunit");

// default task will be used for 'frontend sync'
grunt.registerTask("default", ["clean", "es3ify", "nodeunit"]);
grunt.registerTask("default", ["clean", "es3ify:build", "nodeunit"]);
};
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ grunt.initConfig({
},

fooBar: {
// use "readOnly" flag for testing your configuration
options: {
readOnly: true
},
cwd: "js/app/",
src: "**/*.js",
dest: "target"
Expand All @@ -37,4 +41,8 @@ grunt.initConfig({
});
```

### Options

{{options.readOnly}} (Boolean, default: false)
Shows in the console what will be transformed and where is the destination without any write operations.

11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
"name": "pro!vision GmbH",
"url": "http://www.pro-vision.de"
},
"contributors": [{
"name": "Matthias Hauch",
"email": "[email protected]"
}],
"contributors": [
{
"name": "Matthias Hauch",
"email": "[email protected]"
}
],
"homepage": "https://github.com/wcm-io-frontend/grunt-es3ify",
"bugs": {
"url": "https://github.com/wcm-io-frontend/grunt-es3ify/issues"
Expand Down Expand Up @@ -39,6 +41,7 @@
"grunt-contrib-nodeunit": "^1.0.0"
},
"dependencies": {
"chalk": "^1.1.3",
"es3ify": "^0.2.2"
}
}
27 changes: 24 additions & 3 deletions tasks/es3ify.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,38 @@

"use strict";

var path = require("path");
var chalk = require("chalk");
var transform = require("es3ify").transform;

module.exports = function(grunt) {

grunt.registerMultiTask("es3ify", "Transforms ES5 to ES3", function() {

var options = this.options({
readOnly: false
});

this.files.forEach(function(filePair) {

filePair.src.forEach(function(src) {
var content = grunt.file.read(src);
grunt.file.write(filePair.dest, transform(content));

var dest = filePair.dest;
if (!dest) {
dest = src;
}
else if (grunt.util._.endsWith(dest, "/")) {
dest = path.join(dest, src);
}

if (options.readOnly) {
grunt.log.writeln("Will transform " + chalk.cyan(src) + " -> " + chalk.cyan(dest));
}
else {
grunt.verbose.writeln("Transforming " + chalk.cyan(src) + " -> " + chalk.cyan(dest));
var content = grunt.file.read(src);
grunt.file.write(dest, transform(content));
}
});
});
});
Expand Down
12 changes: 6 additions & 6 deletions test/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

var x = {
dynamic: 0,
static: 17,
throws: "forbidden",
class: "foo"
"static": 17,
"throws": "forbidden",
"class": "foo"
};

x.dynamic++;
x.static++;
x.throws = "test";
x.class += "bar";
x["static"]++;
x["throws"] = "test";
x["class"] += "bar";

0 comments on commit 1a591c6

Please sign in to comment.