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

Commit 28502a6

Browse files
Obfuscate and overwrite source files when dest not specified
1 parent 0e9f123 commit 28502a6

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

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)