Last active
February 23, 2016 14:25
-
-
Save brunomrpx/d4985b46036f7483949d to your computer and use it in GitHub Desktop.
Script to remove dependecy injection declaration of AngularJs files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/nodejs | |
var fs = require('fs'); | |
process.stdin.resume(); | |
process.stdin.setEncoding('utf8'); | |
process.stdin.on('data', function(data) { | |
var fileList = data.split('\n'), | |
file; | |
console.log('Starting...'); | |
for (var index in fileList) { | |
file = fileList[index]; | |
if (!fs.existsSync(file)) { | |
return; | |
} | |
console.log('Removing dependeny injection declaration of ', file); | |
removeDependcyInjectionDeclaration(file); | |
} | |
console.log('Done!') | |
process.exit(0); | |
}); | |
function removeDependcyInjectionDeclaration(file) { | |
/** | |
* Search occurrences in following pattern: | |
* | |
* DeclarationName.$inject = ['dependency1', 'dependency2', 'dependency3']; | |
* | |
* Too works with multiple lines: | |
* | |
* DeclarationName.$inject = [ | |
* 'dependency1', | |
* 'dependency2', | |
* 'dependency3' | |
* ]; | |
*/ | |
var regex = /(\n)(.*)\.\$inject(.*)\[(\s*?.*?)*?\];(\n)/g; | |
fs.readFile(file, 'utf-8', function(error, data) { | |
if (error) return console.log(error); | |
var result = data.replace(regex, ''); | |
fs.writeFile(file, result, 'utf8', function(error) { | |
if (error) return console.log(error); | |
}); | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank for your suggestion Luis. I have added a comment explaining the pattern that the regex matches.