Skip to content

Commit e044228

Browse files
authored
Merge pull request #2 from nancel/directory-as-destination
A directory now can be used as a task destination
2 parents 463fa6f + 673b817 commit e044228

File tree

6 files changed

+98
-12
lines changed

6 files changed

+98
-12
lines changed

Gruntfile.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ module.exports = function (grunt) {
4242
'test/fixtures/file2.js'
4343
]
4444
}
45+
},
46+
47+
folderDest: {
48+
options: {
49+
},
50+
files: {
51+
'tmp/': [
52+
'test/fixtures/file3.js',
53+
'test/fixtures/folder/file4.js'
54+
]
55+
}
4556
}
4657
},
4758

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,27 @@ obfuscator: {
6262
}
6363
```
6464

65+
This configuration will obfuscate the input files in a destination folder by keeping the original names and directories
66+
67+
```javascript
68+
obfuscator: {
69+
options: {
70+
// global options for the obfuscator
71+
},
72+
task1: {
73+
options: {
74+
// options for each sub task
75+
},
76+
files: {
77+
'dest/': [ // the files and their directories will be created in this folder
78+
'src/js/file1.js',
79+
'src/js/folder/file2.js'
80+
]
81+
}
82+
}
83+
}
84+
```
85+
6586
#### Debug protection and banner
6687

6788
Here you code will be protected against debugging and locked to the domain `www.example.com`.

tasks/obfuscator.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ function normalizeLf(string) {
1414
return string.replace(/\r\n/g, '\n');
1515
}
1616

17+
function getFilename(path) {
18+
return path.replace(/^.*[\\\/]/, '');
19+
}
20+
1721
module.exports = function (grunt) {
1822
var getAvailableFiles = function (filesArray) {
1923
return filesArray.filter(function (filepath) {
@@ -47,23 +51,45 @@ module.exports = function (grunt) {
4751

4852
var obfuscated = '';
4953

50-
try {
51-
var totalCode = availableFiles.map(function (file) {
52-
return grunt.file.read(file);
53-
}).join('');
54+
var filenameDest = getFilename(file.dest);
5455

55-
obfuscated = obfuscate(totalCode, options);
56+
if (filenameDest) {
57+
try {
58+
var totalCode = availableFiles.map(function (file) {
59+
return grunt.file.read(file);
60+
}).join('');
5661

57-
} catch (err) {
58-
grunt.log.error(err);
59-
grunt.warn('JavaScript Obfuscation failed at ' + availableFiles + '.');
60-
}
62+
obfuscated = obfuscate(totalCode, options);
63+
64+
} catch (err) {
65+
grunt.log.error(err);
66+
grunt.warn('JavaScript Obfuscation failed at ' + availableFiles + '.');
67+
}
68+
69+
var output = banner + obfuscated;
6170

62-
var output = banner + obfuscated;
71+
grunt.file.write(file.dest, output);
6372

64-
grunt.file.write(file.dest, output);
65-
created.files++;
73+
created.files++;
74+
} else {
75+
availableFiles.forEach(function (fileSrc) {
76+
try {
77+
var code = grunt.file.read(fileSrc);
6678

79+
obfuscated = obfuscate(code, options);
80+
81+
} catch (err) {
82+
grunt.log.error(err);
83+
grunt.warn('JavaScript Obfuscation failed at ' + fileSrc + '.');
84+
}
85+
86+
var output = banner + obfuscated;
87+
88+
grunt.file.write(file.dest + fileSrc, output);
89+
90+
created.files++;
91+
});
92+
}
6793
}, this);
6894

6995
if (created.files > 0) {

test/fixtures/file3.js

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

test/fixtures/folder/file4.js

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

test/test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ exports.obfuscator = {
4141
// tests whether the obfuscation worked
4242
test.ok(result.indexOf('Forty Two') === -1);
4343

44+
test.done();
45+
},
46+
folderDest: function(test) {
47+
test.expect(4);
48+
49+
var result1 = readFile('tmp/test/fixtures/file3.js');
50+
var result2 = readFile('tmp/test/fixtures/folder/file4.js');
51+
52+
var ask = eval(result1);
53+
test.equal(ask('Hello World'), 'HELLO WORLD?');
54+
55+
var doubt = eval(result2);
56+
test.equal(doubt('Hello World'), 'hello world...');
57+
58+
// tests whether the obfuscation worked
59+
test.ok(result1.indexOf('toUpperCase') === -1);
60+
test.ok(result2.indexOf('toLowerCase') === -1);
61+
4462
test.done();
4563
}
4664
};

0 commit comments

Comments
 (0)