Skip to content
This repository has been archived by the owner on Oct 31, 2022. It is now read-only.

Commit

Permalink
Merge pull request #6 from tomasz-oponowicz/develop
Browse files Browse the repository at this point in the history
Release 1.0.0
  • Loading branch information
tomasz-oponowicz authored Nov 8, 2016
2 parents 16a5e56 + b4be46d commit 207472d
Show file tree
Hide file tree
Showing 21 changed files with 280 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sudo: false
language: node_js
node_js:
- '0.10'
- '4'
30 changes: 4 additions & 26 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ module.exports = function(grunt) {
jshint: {
all: [
'Gruntfile.js',
'tasks/*.js',
'<%= nodeunit.tests %>'
'tasks/**/*.js',
'test/**/*.js'
],
options: {
reporterOutput: '',
jshintrc: '.jshintrc'
}
},
Expand All @@ -28,44 +29,21 @@ module.exports = function(grunt) {
tests: ['tmp']
},

// Configuration to be run (and then tested).
javascript_obfuscator: {
default_options: {
options: {
},
files: {
'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123']
}
},
custom_options: {
options: {
separator: ': ',
punctuation: ' !!!'
},
files: {
'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123']
}
}
},

// Unit tests.
nodeunit: {
tests: ['test/*_test.js']
}

});

// Actually load this plugin's task(s).
grunt.loadTasks('tasks');

// These plugins provide necessary tasks.
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-nodeunit');

// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['clean', 'javascript_obfuscator', 'nodeunit']);
grunt.registerTask('test', ['clean', 'nodeunit']);

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);
Expand Down
43 changes: 19 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

[![Build Status](https://travis-ci.org/tomasz-oponowicz/grunt-javascript-obfuscator.svg?branch=master)](https://travis-ci.org/tomasz-oponowicz/grunt-javascript-obfuscator)

> _Note:_ Work in progress. More information coming soon.
> Obfuscates JavaScript files.
> Obfuscates JavaScript files using amazing [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator).
## Getting Started
This plugin requires Grunt `~0.4.5`

If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:
If you haven't used [Grunt](http://gruntjs.com/) before, be sure to check out the [Getting Started](http://gruntjs.com/getting-started) guide, as it explains how to create a [Gruntfile](http://gruntjs.com/sample-gruntfile) as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin and _javascript-obfuscator_ with this command:

```shell
npm install grunt-javascript-obfuscator --save-dev
npm install grunt-javascript-obfuscator javascript-obfuscator --save-dev
```

..._javascript-obfuscator_ is defined as a peer dependency. In other words you can experiment with every version above `0.7.2`.

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

```js
Expand All @@ -41,46 +41,40 @@ grunt.initConfig({

### Options

#### options.separator
Type: `String`
Default value: `', '`

A string value that is used to do something with whatever.

#### options.punctuation
Type: `String`
Default value: `'.'`

A string value that is used to do something else with whatever else.
Options are passed directly to _javascript-obfuscator_. Please visit [documentation of the project](https://github.com/javascript-obfuscator/javascript-obfuscator) for a complete list of options.

### Usage Examples

#### Default Options
In this example, the default options are used to do something with whatever. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result would be `Testing, 1 2 3.`

In this example, the default options are used to obfuscate scripts:

```js
grunt.initConfig({
javascript_obfuscator: {
options: {},
options: {
/* Default options */
},
files: {
'dest/default_options': ['src/testing', 'src/123'],
'dist/obfuscated.js': ['src/module1.js', 'src/module2.js']
},
},
});
```

#### Custom Options
In this example, custom options are used to do something else with whatever else. So if the `testing` file has the content `Testing` and the `123` file had the content `1 2 3`, the generated result in this case would be `Testing: 1 2 3 !!!`

In this example, custom options are used to obfuscate scripts. `debugProtection` makes it almost impossible to use the console tab of the Developer Tools:

```js
grunt.initConfig({
javascript_obfuscator: {
options: {
separator: ': ',
punctuation: ' !!!',
debugProtection: true,
debugProtectionInterval: true
},
files: {
'dest/default_options': ['src/testing', 'src/123'],
'dist/obfuscated.js': ['src/module1.js', 'src/module2.js']
},
},
});
Expand All @@ -90,4 +84,5 @@ grunt.initConfig({
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using [Grunt](http://gruntjs.com/).

## Release History
_(Nothing yet)_

* 2016-11-08   v1.0.0   First release.
26 changes: 16 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"name": "grunt-javascript-obfuscator",
"description": "Obfuscates JavaScript files.",
"version": "0.1.0",
"version": "1.0.0",
"main": "tasks/javascript_obfuscator.js",
"files": ["tasks"],
"homepage": "https://github.com/tomasz-oponowicz/grunt-javascript-obfuscator",
"author": {
"name": "Tomasz Oponowicz",
Expand All @@ -14,14 +16,9 @@
"bugs": {
"url": "https://github.com/tomasz-oponowicz/grunt-javascript-obfuscator/issues"
},
"licenses": [
{
"type": "MIT",
"url": "https://github.com/tomasz-oponowicz/grunt-javascript-obfuscator/blob/master/LICENSE-MIT"
}
],
"license": "MIT",
"engines": {
"node": ">= 0.10.0"
"node": ">=4.5.0"
},
"scripts": {
"test": "grunt"
Expand All @@ -31,9 +28,18 @@
"grunt-cli": "^1.2.0",
"grunt-contrib-clean": "^0.5.0",
"grunt-contrib-jshint": "^0.9.2",
"grunt-contrib-nodeunit": "^0.3.3"
"grunt-contrib-nodeunit": "^0.3.3",
"javascript-obfuscator": "0.7.2"
},
"peerDependencies": {
"grunt": "~0.4.5",
"javascript-obfuscator": ">=0.7.2"
},
"keywords": [
"gruntplugin"
"gruntplugin",
"javascript-obfuscator",
"obfuscate",
"obfuscator"

]
}
56 changes: 30 additions & 26 deletions tasks/javascript_obfuscator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,41 @@

'use strict';

module.exports = function(grunt) {
var JavaScriptObfuscator = require('javascript-obfuscator');

function concat(grunt, paths) {
if (paths.length === 0) {
throw new Error('Source files not found.');
}

return paths.map(function(path) {

// Please see the Grunt documentation for more information regarding task
// creation: http://gruntjs.com/creating-tasks
// Read file source.
return grunt.file.read(path);
}).join(grunt.util.normalizelf(';'));
}

module.exports = function(grunt) {
grunt.registerMultiTask('javascript_obfuscator', 'Obfuscates JavaScript files.', function() {

// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
punctuation: '.',
separator: ', '
});
var options = this.options({});
var files = this.files;

if (files.length === 0) {
throw new Error('Target files not found.');
}

files.forEach(function(f) {
var src;

// Iterate over all specified file groups.
this.files.forEach(function(f) {
// Concat specified files.
var src = f.src.filter(function(filepath) {
// Warn on and remove invalid source files (if nonull was set).
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
} else {
return true;
}
}).map(function(filepath) {
// Read file source.
return grunt.file.read(filepath);
}).join(grunt.util.normalizelf(options.separator));

// Handle options.
src += options.punctuation;
try {
src = concat(grunt, f.src);
} catch (e) {
return grunt.fail.warn(e);
}

src = JavaScriptObfuscator.obfuscate(src, options).getObfuscatedCode();

// Write the destination file.
grunt.file.write(f.dest, src);
Expand All @@ -46,5 +51,4 @@ module.exports = function(grunt) {
grunt.log.writeln('File "' + f.dest + '" created.');
});
});

};
1 change: 0 additions & 1 deletion test/expected/custom_options

This file was deleted.

1 change: 0 additions & 1 deletion test/expected/default_options

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/123

This file was deleted.

2 changes: 2 additions & 0 deletions test/fixtures/first_sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var FIRST_LOCAL_VARIABLE = 1;
console.log(FIRST_LOCAL_VARIABLE + 'FIRST_STRING_LITERAL');
12 changes: 12 additions & 0 deletions test/fixtures/gruntfile_generic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports.setup = function(configuration) {
return function (grunt) {
'use strict';

grunt.initConfig({
javascript_obfuscator: configuration
});

grunt.loadTasks('../../tasks');
grunt.registerTask('default', 'javascript_obfuscator');
};
};
8 changes: 8 additions & 0 deletions test/fixtures/gruntfile_multiple_files1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = require('./gruntfile_generic').setup({
options: {},
multiple_files1: {
files: {
'../../tmp/multiple_files1.js': ['first_sample.js', 'second_sample.js']
}
}
});
7 changes: 7 additions & 0 deletions test/fixtures/gruntfile_multiple_files2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = require('./gruntfile_generic').setup({
options: {},
multiple_files2: {
src: ['first_sample.js', 'second_sample.js'],
dest: '../../tmp/multiple_files2.js'
}
});
5 changes: 5 additions & 0 deletions test/fixtures/gruntfile_no_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = require('./gruntfile_generic').setup({
options: {},
no_file: {
}
});
7 changes: 7 additions & 0 deletions test/fixtures/gruntfile_overwrite1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = require('./gruntfile_generic').setup({
options: {},
overwrite: {
src: '../../tmp/overwrite1.js',
dest: '../../tmp/overwrite1.js'
}
});
8 changes: 8 additions & 0 deletions test/fixtures/gruntfile_overwrite2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = require('./gruntfile_generic').setup({
options: {},
overwrite: {
files: {
'../../tmp/overwrite2.js': '../../tmp/overwrite2.js'
}
}
});
8 changes: 8 additions & 0 deletions test/fixtures/gruntfile_single_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = require('./gruntfile_generic').setup({
options: {},
single_file: {
files: {
'../../tmp/single_file.js': 'first_sample.js'
}
}
});
8 changes: 8 additions & 0 deletions test/fixtures/gruntfile_unknown_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = require('./gruntfile_generic').setup({
options: {},
unknown_file: {
files: {
'../../tmp/unknown_file': ['unknown.js']
}
}
});
2 changes: 2 additions & 0 deletions test/fixtures/second_sample.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var SECOND_LOCAL_VARIABLE = 2;
console.log(SECOND_LOCAL_VARIABLE + 'SECOND_STRING_LITERAL');
1 change: 0 additions & 1 deletion test/fixtures/testing

This file was deleted.

8 changes: 8 additions & 0 deletions test/helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var cp = require("child_process");

exports.callGruntfile = function (filename, whenDoneCallback) {
var command, options;
command = "grunt --gruntfile " + filename + " --no-color";
options = {cwd: 'test/'};
cp.exec(command, options, whenDoneCallback);
};
Loading

0 comments on commit 207472d

Please sign in to comment.