Skip to content

Commit

Permalink
Merge pull request #2 from GiorgiB97/gulp-module-generator
Browse files Browse the repository at this point in the history
Basic version of module generator added
  • Loading branch information
thecodeholic authored Apr 19, 2017
2 parents b1ebce1 + 462947e commit 9feda75
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 30 deletions.
132 changes: 132 additions & 0 deletions gulp/generators/module-generator/generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* Created by george on 4/18/17.
*/

var gulp = require('gulp');
var replace = require('gulp-replace');
var rename = require('gulp-rename');
var fs = require('fs');


function processArguments(args) {
//Creating empty hash map
var argsMap = {};

//Loop which converts string input into javascript object
for (var i = 0; i < args.length; i++) {
//If index isn't for last element of array
if (i + 1 != args.length) {
//If element at index is argument NAME and next element is argument VALUE
if (args[i].substr(0, 2) == "--" && args[i + 1].substr(0, 2) != "--") {
//assigning VALUE to the NAME
argsMap[args[i]] = args[i + 1];
}
//If element at index is argument NAME with no VALUE next to it
else if (args[i].substr(0, 2) == "--" && args[i + 1].substr(0, 2) == "--") {
//assigning FALSE to the NAME with no VALUE
argsMap[args[i]] = false;
}
}
//If index is for last element of array and that element is argument NAME
else if (i + 1 == args.length && args[i].substr(0, 2) == "--") {
//assigning FALSE to the NAME with no VALUE
argsMap[args[i]] = false;
}
//If index is for last element of array and that element is argument VALUE
else if (i + 1 == args.length && args[i].substr(0, 2) != "--") {
//Empty condition not to get inside 'else' block
}
//If there is some kind of error
else {
console.log("=======ERROR=======");
}
}

return argsMap;
}

function validateArgs(argsMap) {
//Defining path value
var path = argsMap["--path"];

//Checking if there is '--path' argument in hashmap
if (argsMap.hasOwnProperty("--path") != false) {
//Checking if value of it isn't false
if (path != false) {
//Checking whether it start with "/" or not
if (path.indexOf("/") == 0) {
//Cutting "/" from path not to get double slashes
argsMap["--path"] = path.toString().substr(1);
return true;
} else {
return true;
}
}
//If no value is defined for path
else {
console.log("NO PATH VALUE DEFINED");
return false;
}
}
//If no path argument is defined
else {
console.log("NO PATH ARGUMENT DEFINED");
return false;
}
}

function generateModule(params) {
var streams = {
'module.txt': ".module.js",
'controller.txt': ".controller.js",
'html.txt': ".html",
'less.txt': ".less"
};

for (var s in streams) {

//Creating stream
var stream = gulp.src('gulp/generators/module-generator/template/'+s);

//Replacing all matching strings with custom strings
for (var i in params) {
stream.pipe(replace("{{" + i + "}}", params[i]));
}

//Saving
stream.pipe(rename(params.ModulePath.split("/").pop() + streams[s]))
.pipe(gulp.dest('src/app/' + params.ModulePath + '/'));

console.log("src/app/"+params['ModulePath']+"/"+params['FolderName']+streams[s]+" Generated");
}
}

gulp.task('generate-module', function () {
//Removing unnecessary elements from 'argv' array and copying others to 'args' array
var args = process.argv.splice(3);

var argsMap = processArguments(args);

//If validation succeeds assign values to variables.
if (validateArgs(argsMap) == true) {
var params = {};
params['ModulePath'] = argsMap["--path"];
params['ModuleDisplayName'] = params.ModulePath.split("/").pop().ucFirst().replace(/[-_.](\w)?/g, function ($0, $1) {
return " " + $1.toUpperCase();
});
params['ModuleName'] = params['ModuleDisplayName'].replace(/\s+/g, "").lcFirst();
params['ControllerName'] = params['ModuleName'].ucFirst() + "Controller";
params['FolderName'] = params.ModulePath.split("/").pop();

// console.log(params);

generateModule(params);
}
});

String.prototype.ucFirst = function () {
return this.valueOf()[0].toUpperCase() + this.valueOf().substring(1);
};
String.prototype.lcFirst = function () {
return this.valueOf()[0].toLowerCase() + this.valueOf().substring(1);
};
26 changes: 26 additions & 0 deletions gulp/generators/module-generator/template/controller.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
(function () {
'use strict';

angular
.module('app.{{ModuleName}}')
.controller('{{ControllerName}}', {{ControllerName}}Fn);

/** @ngInject */
function {{ControllerName}}Fn(){
// var vm = this;

// Data


// Methods


init();

///////////

function init(){

}
}
})();
3 changes: 3 additions & 0 deletions gulp/generators/module-generator/template/html.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div id="{{ModuleName}}">
{{ModuleContent}}
</div>
3 changes: 3 additions & 0 deletions gulp/generators/module-generator/template/less.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#{{ModuleName}}{

}
31 changes: 31 additions & 0 deletions gulp/generators/module-generator/template/module.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(function () {
'use strict';

angular
.module('app.{{ModuleName}}', [])
.config(Config);

/** @ngInject */
function Config($stateProvider, lobiNavigationServiceProvider) {

$stateProvider
.state('app.{{ModuleName}}', {
url: '/{{FolderName}}',
views: {
'content@app': {
templateUrl: 'app/{{ModulePath}}/{{FolderName}}.html',
controller: '{{ControllerName}} as vm'
}
},
bodyClass: 'app-{{FolderName}}'
})
;

lobiNavigationServiceProvider.saveItem('app.{{ModuleName}}', {
text: '{{ModuleDisplayName}}',
state: 'app.{{ModuleName}}',
weight: 1
// icon: 'fa fa-table'
});
}
})();
4 changes: 2 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

'use strict';

var fs = require('fs');
var wrench = require('wrench');
var gulp = require('gulp');

/**
* This will load all js or coffee files in the gulp directory
* in order to load all gulp tasks
*/
fs.readdirSync('./gulp').filter(function(file) {
wrench.readdirSyncRecursive('./gulp').filter(function(file) {
return (/\.(js|coffee)$/i).test(file);
}).map(function(file) {
require('./gulp/' + file);
Expand Down
55 changes: 28 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,49 @@
"test": "gulp test"
},
"devDependencies": {
"browser-sync": "~2.9.11",
"browser-sync-spa": "~1.0.3",
"chalk": "~1.1.1",
"del": "~2.0.2",
"eslint-plugin-angular": "~0.12.0",
"estraverse": "~4.1.0",
"gulp": "~3.9.0",
"gulp-autoprefixer": "~3.0.2",
"gulp-angular-filesort": "~1.1.1",
"gulp-angular-templatecache": "~1.8.0",
"del": "~2.0.2",
"lodash": "~3.10.1",
"gulp-autoprefixer": "~3.0.2",
"gulp-cssnano": "~2.1.1",
"gulp-eslint": "~1.0.0",
"gulp-filter": "~3.0.1",
"gulp-flatten": "~0.2.0",
"gulp-eslint": "~1.0.0",
"eslint-plugin-angular": "~0.12.0",
"gulp-htmlmin": "~1.3.0",
"gulp-inject": "~3.0.0",
"gulp-less": "~3.0.3",
"gulp-load-plugins": "~0.10.0",
"gulp-size": "~2.0.0",
"gulp-uglify": "~1.4.1",
"gulp-useref": "~3.0.3",
"gulp-util": "~3.0.6",
"gulp-ng-annotate": "~1.1.0",
"gulp-replace": "~0.5.4",
"gulp-rename": "~1.2.2",
"gulp-protractor": "~2.1.0",
"gulp-rename": "^1.2.2",
"gulp-replace": "^0.5.4",
"gulp-rev": "~6.0.1",
"gulp-rev-replace": "~0.4.2",
"gulp-htmlmin": "~1.3.0",
"gulp-inject": "~3.0.0",
"gulp-protractor": "~2.1.0",
"gulp-size": "~2.0.0",
"gulp-sourcemaps": "~1.6.0",
"gulp-less": "~3.0.3",
"gulp-angular-filesort": "~1.1.1",
"main-bower-files": "~2.9.0",
"wiredep": "~2.2.2",
"gulp-uglify": "~1.4.1",
"gulp-useref": "~3.0.3",
"gulp-util": "~3.0.6",
"http-proxy-middleware": "~0.9.0",
"karma": "~0.13.10",
"karma-jasmine": "~0.3.6",
"karma-phantomjs-launcher": "~0.2.1",
"phantomjs": "~1.9.18",
"karma-angular-filesort": "~1.0.0",
"karma-phantomjs-shim": "~1.2.0",
"karma-coverage": "~0.5.2",
"karma-jasmine": "~0.3.6",
"karma-ng-html2js-preprocessor": "~0.2.0",
"browser-sync": "~2.9.11",
"browser-sync-spa": "~1.0.3",
"http-proxy-middleware": "~0.9.0",
"chalk": "~1.1.1",
"uglify-save-license": "~0.4.1"
"karma-phantomjs-launcher": "~0.2.1",
"karma-phantomjs-shim": "~1.2.0",
"lodash": "~3.10.1",
"main-bower-files": "~2.9.0",
"phantomjs": "~1.9.18",
"uglify-save-license": "~0.4.1",
"wiredep": "~2.2.2",
"wrench": "^1.5.9"
},
"engines": {
"node": ">=0.10.0"
Expand Down
3 changes: 2 additions & 1 deletion src/app/index.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

'app.pages',

'app.dashboard'
'app.dashboard',
'app.testModule'
]);

})();
31 changes: 31 additions & 0 deletions test.txt/hello
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(aaa () {
'use strict';

angular
.module('app.{{ModuleName}}', [])
.config(Config);

/** @ngInject */
aaa Config($stateProvider, lobiNavigationServiceProvider) {

$stateProvider
.state('app.{{ModuleName}}', {
url: '/{{ModuleName}}',
views: {
'content@app': {
templateUrl: 'app/{{ModulePath}}/{{ModuleName}}.html',
controller: '{{ControllerName}} as vm'
}
},
bodyClass: 'app-{{ModuleName}}'
})
;

lobiNavigationServiceProvider.saveItem('app.{{ModuleName}}', {
text: '{{ModuleDisplayName}}',
state: 'app.{{ModuleName}}',
weight: 1
// icon: 'fa fa-table'
});
}
})();

0 comments on commit 9feda75

Please sign in to comment.