Skip to content

Instantly share code, notes, and snippets.

@brunomrpx
Last active February 23, 2016 14:25
Show Gist options
  • Save brunomrpx/d4985b46036f7483949d to your computer and use it in GitHub Desktop.
Save brunomrpx/d4985b46036f7483949d to your computer and use it in GitHub Desktop.
Script to remove dependecy injection declaration of AngularJs files
#!/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);
});
});
}
@luiscoms
Copy link

Nice script!
To improve this, add a comment before function removeDependcyInjectionDeclaration or in the top of script explaining what the regex matches.

@brunomrpx
Copy link
Author

Thank for your suggestion Luis. I have added a comment explaining the pattern that the regex matches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment