Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
openhoat committed Feb 11, 2013
1 parent 6902c71 commit 68b8d51
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 19 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -19,9 +19,9 @@ Put this in your grunt config :
crypt:{ crypt:{
files:[ // files to process files:[ // files to process
{ {
dir:'demo/src', // root dir of files to encrypt, or dest dir of files to decrypt dir:'demo', // root dir of files to encrypt / decrypt
include:'**/*.js', // pattern to include files include:'**/*.js', // pattern to include files
encryptedDir:'demo/encrypted' // dest dir of files to encrypt, or root dir of files to decrypt encryptedExtension:'.encrypted' // extension used for encrypted files
} }
], ],
options:{ options:{
Expand Down
1 change: 0 additions & 1 deletion demo/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion demo/encrypted/hello.js

This file was deleted.

1 change: 1 addition & 0 deletions demo/hello.js
@@ -0,0 +1 @@
console.log('hello!');
4 changes: 2 additions & 2 deletions grunt.js
Expand Up @@ -16,9 +16,9 @@ module.exports = function (grunt) {
crypt:{ crypt:{
files:[ files:[
{ {
dir:'demo/src', dir:'demo',
include:'**/*.js', include:'**/*.js',
encryptedDir:'demo/encrypted' encryptedExtension:'.encrypted'
} }
], ],
options:{ options:{
Expand Down
25 changes: 12 additions & 13 deletions tasks/crypt.js
Expand Up @@ -9,17 +9,17 @@ module.exports = function (grunt) {
, cryptKey = options.key; , cryptKey = options.key;
files.forEach(function (file) { files.forEach(function (file) {
var srcDir = file.dir || 'src' var srcDir = file.dir || 'src'
, destDir = file.encryptedDir || path.join('encrypted', srcDir) , encryptedExtension = file.encryptedExtension || '.encrypted'
, include = file.include || '*.js' , include = file.include || '*.js'
, filePaths = grunt.file.expand({ cwd:srcDir }, include); , filePaths = grunt.file.expand({ cwd:srcDir }, include);
filePaths.forEach(function (filePath) { filePaths.forEach(function (filePath) {
var srcFilePath = path.join(srcDir, filePath) var srcFilePath = path.join(srcDir, filePath)
, destFilePath = path.join(destDir, filePath) , destFilePath = srcFilePath + encryptedExtension
, content, encryptedContent; , decryptedContent, encryptedContent;
try { try {
grunt.log.write('Encrypting "' + srcFilePath + '" to "' + destFilePath + '"...'); grunt.log.write('Encrypting "' + srcFilePath + '" to "' + destFilePath + '"...');
content = grunt.file.read(srcFilePath); decryptedContent = grunt.file.read(srcFilePath);
encryptedContent = kruptosUtilCrypt.encryptText(content, cryptKey); encryptedContent = kruptosUtilCrypt.encryptText(decryptedContent, cryptKey);
grunt.file.write(destFilePath, encryptedContent); grunt.file.write(destFilePath, encryptedContent);
grunt.log.ok(); grunt.log.ok();
} catch (e) { } catch (e) {
Expand All @@ -35,19 +35,18 @@ module.exports = function (grunt) {
, options = grunt.config('crypt').options , options = grunt.config('crypt').options
, cryptKey = options.key; , cryptKey = options.key;
files.forEach(function (file) { files.forEach(function (file) {
var destDir = file.dir || 'src' var srcDir = file.dir || 'src'
, srcDir = file.encryptedDir || path.join('encrypted', destDir) , encryptedExtension = file.encryptedExtension || '.encrypted'
, include = file.include || '*.js' , filePaths = grunt.file.expand({ cwd:srcDir }, '*' + encryptedExtension);
, filePaths = grunt.file.expand({ cwd:srcDir }, include);
filePaths.forEach(function (filePath) { filePaths.forEach(function (filePath) {
var srcFilePath = path.join(srcDir, filePath) var srcFilePath = path.join(srcDir, filePath)
, destFilePath = path.join(destDir, filePath) , destFilePath = srcFilePath.substring(0, srcFilePath.length - encryptedExtension.length)
, content, encryptedContent; , decryptedContent, encryptedContent;
try { try {
grunt.log.write('Decrypting "' + srcFilePath + '" to "' + destFilePath + '"...'); grunt.log.write('Decrypting "' + srcFilePath + '" to "' + destFilePath + '"...');
encryptedContent = grunt.file.read(srcFilePath); encryptedContent = grunt.file.read(srcFilePath);
content = kruptosUtilCrypt.decryptText(encryptedContent, cryptKey); decryptedContent = kruptosUtilCrypt.decryptText(encryptedContent, cryptKey);
grunt.file.write(destFilePath, content); grunt.file.write(destFilePath, decryptedContent);
grunt.log.ok(); grunt.log.ok();
} catch (e) { } catch (e) {
grunt.log.error(); grunt.log.error();
Expand Down

0 comments on commit 68b8d51

Please sign in to comment.