Skip to content

Commit

Permalink
Added quick commands for angular app
Browse files Browse the repository at this point in the history
  • Loading branch information
gokulkrishh committed Apr 1, 2015
1 parent 26d14df commit 67581ac
Show file tree
Hide file tree
Showing 12 changed files with 435 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,44 @@ At this stage your project is setup and dependencies are installed, It's showtim
- Run `gulp` to run the server, and you are good to start your development.
**Angular application quick commands**
Use terminal to create controller, service, directive etc.
- Run following commands to create
1. Controller
```````
yo smacss:controller <name>
```````
creates a controller file in app/js/controllers
2. Service
```````
yo smacss:service <name>
```````
creates a service file in app/js/services
3. Directive
```````
yo smacss:directive <name>
```````
creates a directive file in app/js/directives
4. Filter
```````
yo smacss:filter <name>
```````
creates a filter file in app/js/filters
# Docs
To be updated soon... stay tuned
Expand Down
1 change: 1 addition & 0 deletions app/templates/angular-app/_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
<!-- JS files -->
<script src="js/bower.js" type="text/javascript" charset="utf-8"></script>
<script src="js/application.js" type="text/javascript" charset="utf-8"></script>
<!-- endbuild -->
</body>
</html>
20 changes: 20 additions & 0 deletions app/templates/js/_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

/*================================================================
=> Controller = <%= classedName %>Ctrl
==================================================================*/
/*global app*/

app.controller('<%= classedName %>Ctrl', ['$scope', function ($scope) {

'use strict';




console.log('Controller === <%= classedName %>Ctrl');
}]);


/*----- End of Controller = <%= classedName %> ------*/


21 changes: 21 additions & 0 deletions app/templates/js/_directive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*================================================================
=> Directive = <%= cameledName %>
==================================================================*/
/*global app*/

app.directive('<%= cameledName %>', ['$rootScope', function ($rootScope) {

'use strict';

return {
restrict: 'A',
link: function (scope, element, attrs) {

console.log('Directive === <%= cameledName %>');
}
};

}]);


/*----- End of Directive = <%= cameledName %> ------*/
19 changes: 19 additions & 0 deletions app/templates/js/_filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*================================================================
=> Filter = <%= cameledName %>
==================================================================*/
/*global app*/

app.filter('<%= cameledName %>', function () {

'use strict';

return function (input) {

console.log('Filter == <%= cameledName %>');

return;
};
});


/*----- End of Filter = <%= cameledName %> ------*/
51 changes: 51 additions & 0 deletions app/templates/js/_service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

/*================================================================
=> Service = <%= cameledName %>
==================================================================*/
/*global app*/

app.service('<%= cameledName %>', ['$rootScope', '$q', '$http', function ($rootScope, $q, $http) {

'use strict';

//GET method
this.getUserData = function (url) {
var defer = $q.defer();

$http.get(url)
.success(function (data) {
deferred.resolve(data);
})
.error(function (err) {
deferred.reject(err);
});

return deferred.promise;

};

//POST method
this.postUserData = function (userData) {
var defer = $q.defer();

var url = ''; //POST url

$http.post({
url : url,
data : userData
})
.success(function (data) {
deferred.resolve(data);
})
.error(function (err) {
deferred.reject(err);
});

return deferred.promise;

};

}]);


/*----- End of Service = <%= cameledName %> ------*/
31 changes: 31 additions & 0 deletions controller/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';
var util = require('util'),
chalk = require('chalk');

var ScriptBase = require('../script-base.js');


var Generator = module.exports = function Generator() {
ScriptBase.apply(this, arguments);

// if the controller name is suffixed with ctrl, remove the suffix
// if the controller name is just "ctrl," don't append/remove "ctrl"
if (this.name && this.name.toLowerCase() !== 'ctrl' && this.name.substr(-4).toLowerCase() === 'ctrl') {
this.name = this.name.slice(0, -4);
}
};

util.inherits(Generator, ScriptBase);

//Function to create controller
Generator.prototype.createControllerFiles = function createControllerFiles() {

this.log(chalk.yellow('Creating controller, please wait...... \n'));

this.generateSourceAndTest(
'_controller', // controller template name
'spec/controller', //for generating test file in test folder
'controllers', //target dir
this.options['skip-add'] || false
);
};
21 changes: 21 additions & 0 deletions directive/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
var util = require('util'),
ScriptBase = require('../script-base.js');

var Generator = module.exports = function Generator() {
ScriptBase.apply(this, arguments);
};

util.inherits(Generator, ScriptBase);

Generator.prototype.createDirectiveFiles = function createDirectiveFiles() {

this.log(chalk.yellow('Creating directive, please wait...... \n'));

this.generateSourceAndTest(
'_directive',
'spec/directive',
'directives',
this.options['add-index'] || false
);
};
21 changes: 21 additions & 0 deletions filter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
var util = require('util'),
ScriptBase = require('../script-base.js');

var Generator = module.exports = function Generator() {
ScriptBase.apply(this, arguments);
};

util.inherits(Generator, ScriptBase);

Generator.prototype.createFilterFiles = function createFilterFiles() {

this.log(chalk.yellow('Creating filter, please wait...... \n'));

this.generateSourceAndTest(
'_filter',
'spec/filter',
'filters',
this.options['add-index'] || false
);
};
112 changes: 112 additions & 0 deletions script-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
'use strict';
var util = require('util');
var path = require('path');
var yeoman = require('yeoman-generator');
var angularUtils = require('./util.js');
var chalk = require('chalk');

var Generator = module.exports = function Generator() {
yeoman.generators.NamedBase.apply(this, arguments);

var bowerJson = {};

try {
bowerJson = require(path.join(process.cwd(), 'bower.json'));
} catch (e) {}

if (bowerJson.name) {
this.appname = bowerJson.name;
} else {
this.appname = path.basename(process.cwd());
}

this.appname = this._.slugify(this._.humanize(this.appname));

this.scriptAppName = bowerJson.moduleName || this._.camelize(this.appname) + angularUtils.appName(this);

this.cameledName = this._.camelize(this.name);
this.classedName = this._.classify(this.name);

if (typeof this.env.options.appPath === 'undefined') {
this.env.options.appPath = this.options.appPath || bowerJson.appPath || 'app';
this.options.appPath = this.env.options.appPath;
}

this.env.options.testPath = this.env.options.testPath || bowerJson.testPath || 'test/spec';

this.env.options.coffee = this.options.coffee;
if (typeof this.env.options.coffee === 'undefined') {
this.option('coffee');

// attempt to detect if user is using CS or not
// if cml arg provided, use that; else look for the existence of cs
if (!this.options.coffee &&
this.expandFiles(path.join(this.env.options.appPath, '/scripts/**/*.coffee'), {}).length > 0) {
this.options.coffee = true;
}

this.env.options.coffee = this.options.coffee;
}

var sourceRoot = '/app/templates/js';
this.scriptSuffix = '.js';

if (this.env.options.coffee) {
sourceRoot = '/templates/coffeescript';
this.scriptSuffix = '.coffee';
}

this.sourceRoot(path.join(__dirname, sourceRoot));
};

util.inherits(Generator, yeoman.generators.NamedBase);

Generator.prototype.appTemplate = function (src, dest) {
yeoman.generators.Base.prototype.template.apply(this, [
src + this.scriptSuffix,
path.join(this.env.options.appPath, dest.toLowerCase()) + this.scriptSuffix
]);
};

Generator.prototype.testTemplate = function (src, dest) {
yeoman.generators.Base.prototype.template.apply(this, [
src + this.scriptSuffix,
path.join(this.env.options.testPath, dest.toLowerCase()) + this.scriptSuffix
]);
};

Generator.prototype.htmlTemplate = function (src, dest) {
yeoman.generators.Base.prototype.template.apply(this, [
src,
path.join(this.env.options.appPath, dest.toLowerCase())
]);
};

Generator.prototype.addScriptToIndex = function (script) {
try {
var appPath = this.env.options.appPath;
var fullPath = path.join(appPath, 'index.html');
angularUtils.rewriteFile({
file: fullPath,
needle: '<!-- endbuild -->',
splicable: [
'<script src="js/' + script.toLowerCase().replace(/\\/g, '/') + '.js"></script>'
]
});
} catch (e) {
this.log.error(chalk.yellow(
'\nUnable to find ' + fullPath + '. Reference to ' + script + '.js ' + 'not added.\n'
));
}
};

Generator.prototype.generateSourceAndTest = function (appTemplate, testTemplate, targetDirectory, skipAdd) {

skipAdd = true;

this.appTemplate(appTemplate, path.join('js', targetDirectory, this.name));
// this.testTemplate(testTemplate, path.join(targetDirectory, this.name));
if (!skipAdd) {
this.addScriptToIndex(path.join(targetDirectory, this.name));
}
};
24 changes: 24 additions & 0 deletions service/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use strict';
var util = require('util'),
chalk = require('chalk');

var ScriptBase = require('../script-base.js');


var Generator = module.exports = function Generator() {
ScriptBase.apply(this, arguments);
};

util.inherits(Generator, ScriptBase);

Generator.prototype.createServiceFiles = function createServiceFiles() {

this.log(chalk.yellow('Creating service, please wait...... \n'));

this.generateSourceAndTest(
'_service',
'spec/service',
'services',
this.options['skip-add'] || false
);
};
Loading

0 comments on commit 67581ac

Please sign in to comment.