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

Commit 189fdda

Browse files
Merge pull request #13 from tomasz-oponowicz/develop
Release 1.1.0
2 parents 194c0c9 + 33ba698 commit 189fdda

File tree

5 files changed

+105
-32
lines changed

5 files changed

+105
-32
lines changed

README.md

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@ Conceal your logic and hide any data contained in the code. Please read document
88

99
Original code:
1010

11-
(function(){
12-
var variable = 'abc';
13-
console.log(variable);
14-
})();
11+
```js
12+
(function(){
13+
var variable = 'abc';
14+
console.log(variable);
15+
})();
16+
```
1517

1618
Protected code:
1719

18-
var _0xabf1 = [
19-
'\x61\x62\x63',
20-
'\x6c\x6f\x67'
21-
];
22-
(function() {
23-
var _0xe6fab6 = _0xabf1[0x0];
24-
console[_0xabf1[0x1]](_0xe6fab6);
25-
}());
26-
27-
20+
```js
21+
var _0xabf1 = [
22+
'\x61\x62\x63',
23+
'\x6c\x6f\x67'
24+
];
25+
(function() {
26+
var _0xe6fab6 = _0xabf1[0x0];
27+
console[_0xabf1[0x1]](_0xe6fab6);
28+
}());
29+
```
2830

2931
Special thanks for [@sanex3339](https://github.com/sanex3339) for his outstanding [javascript-obfuscator](https://github.com/javascript-obfuscator/javascript-obfuscator) library.
3032

@@ -108,11 +110,29 @@ grunt.initConfig({
108110
});
109111
```
110112

113+
#### Obfuscate and overwrite
114+
115+
In this example, source files are obfuscated and overwritten:
116+
117+
```js
118+
grunt.initConfig({
119+
javascript_obfuscator: {
120+
options: {
121+
/* Default options */
122+
},
123+
main: {
124+
src: ['src/module1.js', 'src/module2.js']
125+
}
126+
},
127+
});
128+
```
129+
111130
## Contributing
112131
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/).
113132

114133
## Release History
115134

135+
* 2016-12-23 / v1.1.0 / Obfuscate and overwrite files without destination.
116136
* 2016-11-11 / v1.0.4 / Updated README.
117137
* 2016-11-09 / v1.0.3 / Updated README.
118138
* 2016-11-09 / v1.0.2 / Fixed examples.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "grunt-javascript-obfuscator",
33
"description": "Obfuscates JavaScript files using amazing javascript-obfuscator.",
4-
"version": "1.0.4",
4+
"version": "1.1.0",
55
"main": "tasks/javascript_obfuscator.js",
66
"files": ["tasks"],
77
"homepage": "https://github.com/tomasz-oponowicz/grunt-javascript-obfuscator",

tasks/javascript_obfuscator.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,35 @@ function concat(grunt, paths) {
1616
}
1717

1818
return paths.map(function(path) {
19-
20-
// Read file source.
2119
return grunt.file.read(path);
2220
}).join(grunt.util.normalizelf(';'));
2321
}
2422

23+
function obfuscate(grunt, originalSource, destFile, options) {
24+
var obfuscatedSource = JavaScriptObfuscator.obfuscate(originalSource, options).getObfuscatedCode();
25+
grunt.file.write(destFile, obfuscatedSource);
26+
grunt.log.writeln('File "' + destFile + '" obfuscated.');
27+
}
28+
29+
function obfuscateSingleTarget(grunt, srcFiles, destFile, options) {
30+
var source;
31+
32+
try {
33+
source = concat(grunt, srcFiles);
34+
} catch (error) {
35+
return grunt.fail.warn(error);
36+
}
37+
38+
obfuscate(grunt, source, destFile, options);
39+
}
40+
41+
function obfuscateMultiplesTargets(grunt, destFiles, options) {
42+
destFiles.forEach(function (destFile) {
43+
var source = grunt.file.read(destFile);
44+
obfuscate(grunt, source, destFile, options);
45+
});
46+
}
47+
2548
module.exports = function(grunt) {
2649
grunt.registerMultiTask('javascript_obfuscator', 'Obfuscates JavaScript files.', function() {
2750

@@ -33,22 +56,12 @@ module.exports = function(grunt) {
3356
throw new Error('Target files not found.');
3457
}
3558

36-
files.forEach(function(f) {
37-
var src;
38-
39-
try {
40-
src = concat(grunt, f.src);
41-
} catch (e) {
42-
return grunt.fail.warn(e);
59+
files.forEach(function(file) {
60+
if (file.dest) {
61+
obfuscateSingleTarget(grunt, file.src, file.dest, options);
62+
} else {
63+
obfuscateMultiplesTargets(grunt, file.src, options);
4364
}
44-
45-
src = JavaScriptObfuscator.obfuscate(src, options).getObfuscatedCode();
46-
47-
// Write the destination file.
48-
grunt.file.write(f.dest, src);
49-
50-
// Print a success message.
51-
grunt.log.writeln('File "' + f.dest + '" created.');
5265
});
5366
});
5467
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = require('./gruntfile_generic').setup({
2+
options: {},
3+
overwrite_without_dest: {
4+
src: '../../tmp/overwrite_without_dest_*'
5+
}
6+
});

test/javascript_obfuscator_test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,40 @@ exports.javascript_obfuscator = {
131131
// should be obfuscated
132132
test.ok(obfuscated.indexOf('FIRST_STRING_LITERAL') === -1, '`FIRST_STRING_LITERAL` should be obfuscated');
133133

134+
test.done();
135+
});
136+
},
137+
138+
overwrite_without_dest: function(test) {
139+
test.expect(8);
140+
141+
grunt.file.copy('test/fixtures/first_sample.js', 'tmp/overwrite_without_dest_first.js');
142+
grunt.file.copy('test/fixtures/second_sample.js', 'tmp/overwrite_without_dest_second.js');
143+
144+
helper.callGruntfile('fixtures/gruntfile_overwrite_without_dest.js', function (error, stdout, stderr) {
145+
test.equal(error, null, "Command should not fail.");
146+
test.equal(stderr, '', "Standard error stream should be empty.");
147+
148+
var obfuscated;
149+
150+
obfuscated = grunt.file.read('tmp/overwrite_without_dest_first.js');
151+
152+
// should NOT be obfuscated
153+
test.ok(obfuscated.indexOf('console') > -1, '`console` shouldn\'t be obfuscated');
154+
test.ok(obfuscated.indexOf('FIRST_LOCAL_VARIABLE') > -1, '`FIRST_LOCAL_VARIABLE` shouldn\'t be obfuscated');
155+
156+
// should be obfuscated
157+
test.ok(obfuscated.indexOf('FIRST_STRING_LITERAL') === -1, '`FIRST_STRING_LITERAL` should be obfuscated');
158+
159+
obfuscated = grunt.file.read('tmp/overwrite_without_dest_second.js');
160+
161+
// should NOT be obfuscated
162+
test.ok(obfuscated.indexOf('console') > -1, '`console` shouldn\'t be obfuscated');
163+
test.ok(obfuscated.indexOf('SECOND_LOCAL_VARIABLE') > -1, '`SECOND_LOCAL_VARIABLE` shouldn\'t be obfuscated');
164+
165+
// should be obfuscated
166+
test.ok(obfuscated.indexOf('SECOND_STRING_LITERAL') === -1, '`SECOND_STRING_LITERAL` should be obfuscated');
167+
134168
test.done();
135169
});
136170
}

0 commit comments

Comments
 (0)