Skip to content

Commit c57a46b

Browse files
committed
Initial commit.
0 parents  commit c57a46b

12 files changed

Lines changed: 2391 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules/
2+
/tmp/

.jshintrc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"boss": true,
3+
"curly": true,
4+
"eqeqeq": true,
5+
"eqnull": true,
6+
"evil": true,
7+
"immed": true,
8+
"latedef": true,
9+
"newcap": true,
10+
"noarg": true,
11+
"node": true,
12+
"sub": true,
13+
"undef": true,
14+
"unused": true
15+
}

Gruntfile.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict';
2+
3+
module.exports = function (grunt) {
4+
grunt.initConfig({
5+
6+
jshint: {
7+
all: [
8+
'Gruntfile.js',
9+
'tasks/*.js',
10+
'<%= nodeunit.tests %>'
11+
],
12+
options: {
13+
jshintrc: '.jshintrc'
14+
}
15+
},
16+
17+
clean: {
18+
test: ['tmp']
19+
},
20+
21+
obfuscator: {
22+
options: {
23+
},
24+
single: {
25+
options: {
26+
reservedNames: ['upper'],
27+
},
28+
files: {
29+
'tmp/single-out.js': [
30+
'test/fixtures/single.js'
31+
]
32+
}
33+
},
34+
35+
multiple: {
36+
options: {
37+
},
38+
files: {
39+
'tmp/multiple-out.js': [
40+
'test/fixtures/file1.js',
41+
'test/fixtures/file2.js'
42+
]
43+
}
44+
}
45+
},
46+
47+
nodeunit: {
48+
tests: ['test/test.js']
49+
}
50+
});
51+
52+
grunt.loadTasks('tasks');
53+
grunt.loadNpmTasks('grunt-contrib-clean');
54+
grunt.loadNpmTasks('grunt-contrib-jshint');
55+
grunt.loadNpmTasks('grunt-contrib-nodeunit');
56+
grunt.loadNpmTasks('grunt-contrib-internal');
57+
58+
grunt.registerTask('test', [
59+
'jshint',
60+
'clean',
61+
'obfuscator',
62+
'nodeunit'
63+
]);
64+
65+
grunt.registerTask('default', [
66+
'test'
67+
]);
68+
};

LICENSE.MIT

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2016 Tiago Serafim, contributors
2+
3+
Permission is hereby granted, free of charge, to any person
4+
obtaining a copy of this software and associated documentation
5+
files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use,
7+
copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the
9+
Software is furnished to do so, subject to the following
10+
conditions:
11+
12+
The above copyright notice and this permission notice shall be
13+
included in all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
grunt-contrib-obfuscator
2+
========================
3+
4+
Grunt plugin for [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator).
5+
6+
**Consider this code alpha.**
7+
8+
## Installation
9+
10+
**It's not published on NPM yet.**
11+
12+
Install the package with NPM:
13+
14+
`npm install --save grunt-contrib-obfuscator`
15+
16+
## Usage
17+
18+
```javascript
19+
obfuscator: {
20+
options: {
21+
// global options for the obfuscator
22+
},
23+
task1: {
24+
options: {
25+
// options for each sub task
26+
},
27+
files: {
28+
'dest/output.js': [
29+
'src/js/file1.js',
30+
'src/js/file2.js'
31+
]
32+
}
33+
}
34+
}
35+
```
36+
37+
## Options
38+
39+
[See the options on the obfuscator repo](https://github.com/javascript-obfuscator/javascript-obfuscator#javascript-obfuscator-options).
40+
41+
Note that at this time the `sourceMap` isn't implemented in this plugin.

package.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "grunt-contrib-obfuscate",
3+
"description": "Grunt plugin that obfuscates your JavaScript using the javascript-obfuscator library.",
4+
"version": "0.0.1",
5+
"author": {
6+
"name": "Tiago Serafim"
7+
},
8+
"repository": "javascript-obfuscator/grunt-contrib-obfuscator",
9+
"license": "MIT",
10+
"engines": {
11+
"node": ">=0.10.0"
12+
},
13+
"main": "tasks/obfuscator.js",
14+
"scripts": {
15+
"test": "grunt test"
16+
},
17+
"dependencies": {
18+
"chalk": "^1.0.0",
19+
"javascript-obfuscator": "^0.7.3"
20+
},
21+
"devDependencies": {
22+
"grunt": "^1.0.0",
23+
"grunt-contrib-clean": "^1.0.0",
24+
"grunt-contrib-internal": "^1.1.0",
25+
"grunt-contrib-jshint": "^1.0.0",
26+
"grunt-contrib-nodeunit": "^1.0.0"
27+
},
28+
"keywords": [],
29+
"files": [
30+
"tasks"
31+
]
32+
}

tasks/obfuscator.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
var chalk = require('chalk');
4+
5+
var JavaScriptObfuscator = require('javascript-obfuscator');
6+
7+
function obfuscate(source, options) {
8+
var obfuscationResult = JavaScriptObfuscator.obfuscate(source, options);
9+
return obfuscationResult.getObfuscatedCode();
10+
}
11+
12+
module.exports = function (grunt) {
13+
var getAvailableFiles = function (filesArray) {
14+
return filesArray.filter(function (filepath) {
15+
if (!grunt.file.exists(filepath)) {
16+
grunt.log.warn('Source file ' + chalk.cyan(filepath) + ' not found');
17+
return false;
18+
}
19+
return true;
20+
});
21+
};
22+
23+
grunt.registerMultiTask('obfuscator', 'Obfuscate JavaScript', function () {
24+
var created = {
25+
maps: 0,
26+
files: 0
27+
};
28+
29+
this.files.forEach(function (file) {
30+
var options = this.options({});
31+
32+
var availableFiles = getAvailableFiles(file.src);
33+
34+
if (options.sourceMap) {
35+
grunt.log.error('Source Maps are not available yet.');
36+
return;
37+
}
38+
39+
var obfuscated = '';
40+
41+
try {
42+
var totalCode = availableFiles.map(function (file) {
43+
return grunt.file.read(file);
44+
}).join('');
45+
46+
obfuscated = obfuscate(totalCode, options);
47+
48+
} catch (err) {
49+
grunt.log.error(err);
50+
grunt.warn('JavaScript Obfuscation failed at ' + availableFiles + '.');
51+
}
52+
53+
grunt.file.write(file.dest, obfuscated);
54+
created.files++;
55+
56+
}, this);
57+
58+
if (created.files > 0) {
59+
grunt.log.ok(created.files + ' ' + grunt.util.pluralize(this.files.length, 'file/files') + ' created');
60+
} else {
61+
grunt.log.warn('No files created.');
62+
}
63+
});
64+
};

test/fixtures/file1.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function fortytwo() {
2+
return {
3+
number: 42,
4+
str: 'Forty Two'
5+
}
6+
}

test/fixtures/file2.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function answer() {
2+
return fortytwo();
3+
}
4+
module.exports = answer;

test/fixtures/single.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
function shout(what) {
2+
var upper = what.toUpperCase();
3+
return upper + '!';
4+
}
5+
module.exports = shout;

0 commit comments

Comments
 (0)