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

Commit 1050288

Browse files
Basic implementation and test
1 parent 31d5fb9 commit 1050288

9 files changed

Lines changed: 44 additions & 47 deletions

File tree

Gruntfile.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,7 @@ module.exports = function(grunt) {
3434
options: {
3535
},
3636
files: {
37-
'tmp/default_options': ['test/fixtures/testing', 'test/fixtures/123']
38-
}
39-
},
40-
custom_options: {
41-
options: {
42-
separator: ': ',
43-
punctuation: ' !!!'
44-
},
45-
files: {
46-
'tmp/custom_options': ['test/fixtures/testing', 'test/fixtures/123']
37+
'tmp/default_options': ['test/fixtures/sample1.js']
4738
}
4839
}
4940
},

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
"grunt-cli": "^1.2.0",
3232
"grunt-contrib-clean": "^0.5.0",
3333
"grunt-contrib-jshint": "^0.9.2",
34-
"grunt-contrib-nodeunit": "^0.3.3"
34+
"grunt-contrib-nodeunit": "^0.3.3",
35+
"javascript-obfuscator": "0.7.2"
36+
},
37+
"peerDependencies": {
38+
"javascript-obfuscator": ">=0.7.2"
3539
},
3640
"keywords": [
3741
"gruntplugin"

tasks/javascript_obfuscator.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,44 @@
88

99
'use strict';
1010

11+
var JavaScriptObfuscator = require('javascript-obfuscator');
12+
13+
function concat(grunt, paths) {
14+
if (paths.length === 0) {
15+
grunt.log.warn('Source files don\'t exist');
16+
return '';
17+
}
18+
19+
return paths.filter(function(path) {
20+
// Warn on and remove invalid source files (if nonull was set).
21+
if (!grunt.file.exists(path)) {
22+
grunt.log.warn('Source file "' + path + '" not found.');
23+
return false;
24+
} else {
25+
return true;
26+
}
27+
}).map(function(path) {
28+
// Read file source.
29+
return grunt.file.read(path);
30+
}).join(grunt.util.normalizelf(';'));
31+
}
32+
1133
module.exports = function(grunt) {
1234

1335
// Please see the Grunt documentation for more information regarding task
1436
// creation: http://gruntjs.com/creating-tasks
1537

1638
grunt.registerMultiTask('javascript_obfuscator', 'Obfuscates JavaScript files.', function() {
1739
// Merge task-specific and/or target-specific options with these defaults.
18-
var options = this.options({
19-
punctuation: '.',
20-
separator: ', '
21-
});
40+
var options = this.options({});
2241

2342
// Iterate over all specified file groups.
2443
this.files.forEach(function(f) {
2544
// Concat specified files.
26-
var src = f.src.filter(function(filepath) {
27-
// Warn on and remove invalid source files (if nonull was set).
28-
if (!grunt.file.exists(filepath)) {
29-
grunt.log.warn('Source file "' + filepath + '" not found.');
30-
return false;
31-
} else {
32-
return true;
33-
}
34-
}).map(function(filepath) {
35-
// Read file source.
36-
return grunt.file.read(filepath);
37-
}).join(grunt.util.normalizelf(options.separator));
38-
39-
// Handle options.
40-
src += options.punctuation;
45+
46+
var src = concat(grunt, f.src);
47+
48+
src = JavaScriptObfuscator.obfuscate(src, options).getObfuscatedCode();
4149

4250
// Write the destination file.
4351
grunt.file.write(f.dest, src);

test/expected/custom_options

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/default_options

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/123

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/sample1.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var TEST_LOCAL_VARIABLE = 1;
2+
console.log(TEST_LOCAL_VARIABLE + 'TEST_STRING_LITERAL');

test/fixtures/testing

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/javascript_obfuscator_test.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,21 @@ var grunt = require('grunt');
2424

2525
exports.javascript_obfuscator = {
2626
setUp: function(done) {
27-
// setup here if necessary
2827
done();
2928
},
3029
default_options: function(test) {
31-
test.expect(1);
30+
test.expect(4);
3231

33-
var actual = grunt.file.read('tmp/default_options');
34-
var expected = grunt.file.read('test/expected/default_options');
35-
test.equal(actual, expected, 'should describe what the default behavior is.');
32+
var obfuscated = grunt.file.read('tmp/default_options');
3633

37-
test.done();
38-
},
39-
custom_options: function(test) {
40-
test.expect(1);
34+
// should NOT be obfuscated
35+
test.ok(obfuscated.indexOf('console') > -1, '`console` shouldn\'t be obfuscated');
36+
test.ok(obfuscated.indexOf('TEST_LOCAL_VARIABLE') > -1, '`TEST_LOCAL_VARIABLE` shouldn\'t be obfuscated');
4137

42-
var actual = grunt.file.read('tmp/custom_options');
43-
var expected = grunt.file.read('test/expected/custom_options');
44-
test.equal(actual, expected, 'should describe what the custom option(s) behavior is.');
38+
// should be obfuscated
39+
test.ok(obfuscated.indexOf('.log') > -1, '`.log` shouldn\'t be obfuscated');
40+
test.ok(obfuscated.indexOf('TEST_STRING_LITERAL') === -1, '`TEST_STRING_LITERAL` should be obfuscated');
4541

4642
test.done();
47-
},
43+
}
4844
};

0 commit comments

Comments
 (0)