UNPKG

basecamp-guide

Version:
94 lines (76 loc) 3.19 kB
// Dependecies var fs = require('fs'); var Colors = require('colors'); var FileSys = require('./FileSystem.js'); /* * Adds new Service Providers to Laravel */ var LaravelProvider = { configFile : 'config/app.php', add : function(provider) { // first search comment // yes-comment // -> add service provider // no-comment // -> search end '],' and add comment // -> add service provider // var that = this; FileSys.modifyFile('config/app.php', function(fileContent) { if (that.providerExist(provider, fileContent)) { console.log('Provider already exist.'.yellow); return undefined; // skip writing a new file } var commentPosition = that.searchComment(fileContent); if (! that.commentExist(commentPosition)) { fileContent = that.addCommentTo(fileContent); commentPosition = that.searchComment(fileContent); } var writePosition = that.getEndOfComment(fileContent, commentPosition); var providerText = '\n '+ provider +'::class,'; console.log('AuthServiceProvider ' + 'added.'.green); return that.writeContentAtPosition(writePosition, providerText, fileContent); }); }, commentExist : function(position) { if (position >= 0 ) return true; return false; }, searchComment : function(fileContent) { var rexpression = /\* Basecamp Framework Service Providers.../; return fileContent.search(rexpression); }, getEndOfComment : function(fileContent, commentPosition) { var position = this.searchNextPositionOf('/', fileContent, commentPosition); return position + 1; }, writeContentAtPosition : function(position, content, fileContent) { var part1 = fileContent.slice(0, position); var part2 = fileContent.slice(position); return part1 + content + part2; }, addCommentTo : function(fileContent) { var rexpression = /\'providers\' => \[/; var position = fileContent.search(rexpression); position = this.searchNextPositionOf(']', fileContent, position); var comment = ' /*\n * Basecamp Framework Service Providers...\n */ \n\n '; return this.writeContentAtPosition(position, comment, fileContent); }, searchNextPositionOf : function(searchChar, content, startPosition) { var position = -1; for (var i = startPosition; i <= content.length -1; i++) { if (content[i] == searchChar) { position = i; break; } }; return position; }, providerExist : function(provider, fileContent) { var rexpression = new RegExp(provider.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")); var position = fileContent.search(rexpression); if (position >= 0) return true; return false; } }; module.exports = LaravelProvider;