Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed primary module location to be configured #94

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,39 @@ Notice that there is now no beginning "$routeProvider." above the comment marker
/* Add New Routes Above */
$routeProvider.otherwise({redirectTo:'/home'});
```

Upgrading from v3.2 to v3.3
-------------
1. Modify your `.yo-rc.json` file to look like the following. The only changes are in the `modules` section and the `primaryModule`. If you already have a `modules` section, simply add the content below.

```js
{
"generator-cg-angular": {
"uirouter": false,
"partialDirectory": "partial/",
"directiveDirectory": "directive/",
"serviceDirectory": "service/",
"filterDirectory": "filter/",
"inject": {
"js": {
"file": "index.html",
"marker": "<!-- Add New Component JS Above -->",
"template": "<script src=\"<%= filename %>\"></script>"
},
"less": {
"relativeToModule": true,
"file": "<%= module %>.less",
"marker": "/* Add Component LESS Above */",
"template": "@import \"<%= filename %>\";"
}
},
"primaryModule": "app", //<------------- Put your primary angular module name here
"modules": [
{
"name": "app", //<------------- Put your primary angular module name here
"file": "app.js" //<------------- Put the path to your primary JS file here
}
]
}
}
```
59 changes: 52 additions & 7 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ var util = require('util');
var path = require('path');
var yeoman = require('yeoman-generator');
var cgUtils = require('../utils.js');
var _ = require('underscore');
var fs = require('fs');

_.str = require('underscore.string');
_.mixin(_.str.exports());

var CgangularGenerator = module.exports = function CgangularGenerator(args, options, config) {
yeoman.generators.Base.apply(this, arguments);
Expand All @@ -27,6 +32,14 @@ var CgangularGenerator = module.exports = function CgangularGenerator(args, opti
}
};
this.config.set('inject',inject);
this.config.set('primaryModule', _.camelize(this.appname));
var modules = [
{
name: _.camelize(this.appname),
file: path.join(this.dir, this.appname + '.js')
}
];
this.config.set('modules', modules);
this.config.save();
this.installDependencies({ skipInstall: options['skip-install'] });
});
Expand All @@ -39,14 +52,32 @@ util.inherits(CgangularGenerator, yeoman.generators.Base);
CgangularGenerator.prototype.askFor = function askFor() {
var cb = this.async();

var prompts = [{
name: 'appname',
message: 'What would you like the angular app/module name to be?',
default: path.basename(process.cwd())
}];
var prompts = [
{
name: 'appname',
message: 'What would you like the angular app/module name to be?',
default: path.basename(process.cwd())
},
{
name: 'dir',
message: 'Where would you like to create the main module?',
default: '.',
validate: function (value) {
value = _.str.trim(value);
if (_.isEmpty(value)) {
return 'Please enter a value.';
}
return true;
}
}
];

this.prompt(prompts, function (props) {
this.appname = props.appname;
this.dir = path.join(props.dir, path.sep);
this.appJs = path.join(this.dir, this.appname + '.js');
this.appLess = path.join(this.dir, this.appname + '.less');
this.bowerDir = path.relative(path.join('.', this.dir), './bower_components');
cb();
}.bind(this));
};
Expand All @@ -65,12 +96,12 @@ CgangularGenerator.prototype.askForUiRouter = function askFor() {
this.prompt(prompts, function (props) {
if (props.router === 'Angular UI Router') {
this.uirouter = true;
this.routerJs = 'bower_components/angular-ui-router/release/angular-ui-router.js';
this.routerJs = 'angular-ui-router/release/angular-ui-router.js';
this.routerModuleName = 'ui.router';
this.routerViewDirective = 'ui-view';
} else {
this.uirouter = false;
this.routerJs = 'bower_components/angular-route/angular-route.js';
this.routerJs = 'angular-route/angular-route.js';
this.routerModuleName = 'ngRoute';
this.routerViewDirective = 'ng-view';
}
Expand All @@ -80,5 +111,19 @@ CgangularGenerator.prototype.askForUiRouter = function askFor() {
};

CgangularGenerator.prototype.app = function app() {
var that = this;
this.directory('skeleton/','./');

var templateDirectory = path.join(__dirname, 'templates', 'application');
_.chain(fs.readdirSync(templateDirectory))
.filter(function (template) {
return template[0] !== '.';
})
.each(function (template) {
var customTemplateName = template.replace('app', that.appname);
var templateFile = path.join(templateDirectory, template);
var outputFile = path.join(that.dir, customTemplateName);
//create the file
that.template(templateFile, outputFile);
});
};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
@import "bower_components/bootstrap/less/bootstrap.less";
//@import "bower_components/bootstrap/less/theme.less";
@import "bower_components/font-awesome/less/font-awesome.less";
@fa-font-path: "bower_components/font-awesome/fonts";
@bower: ~"<%= bowerDir %>";

@import "@{bower}/bootstrap/less/bootstrap.less";
//@import "@{bower}/bootstrap/less/theme.less";
@import "@{bower}/font-awesome/less/font-awesome.less";
@fa-font-path: "@{bower}/font-awesome/fonts";

/* Component LESS */
/* Add Component LESS Above */
Expand Down
2 changes: 1 addition & 1 deletion app/templates/skeleton/Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ module.exports = function (grunt) {
options: {
},
files: {
'temp/app.css': 'app.less'
'temp/app.css': '<%= appLess %>'
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion app/templates/skeleton/gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ gulp.task('clean', function() {
});

gulp.task('css', ['clean'], function() {
return gulp.src('app.less')
return gulp.src('<%= appLess =>')
.pipe(less())
.pipe(cssmin({keepSpecialComments: 0}))
.pipe(rename('app.full.min.css'))
Expand Down
6 changes: 3 additions & 3 deletions app/templates/skeleton/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<link href="app.less" type="text/css" rel="stylesheet/less">
<link href="<%= appLess %>" type="text/css" rel="stylesheet/less">
</head>
<body>

Expand All @@ -18,7 +18,7 @@
<script src="bower_components/underscore/underscore.js"></script>
<script src="bower_components/moment/moment.js"></script>
<script src="bower_components/angular/angular.js"></script>
<script src="<%= routerJs %>"></script>
<script src="bower_components/<%= routerJs %>"></script>
<script src="bower_components/angular-animate/angular-animate.js"></script>
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
Expand All @@ -27,7 +27,7 @@


<!-- Main App JS -->
<script src="app.js"></script>
<script src="<%= appJs %>"></script>
<!-- Add New Component JS Above -->

<div>
Expand Down
6 changes: 4 additions & 2 deletions module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ ModuleGenerator.prototype.askFor = function askFor() {
name:'dir',
message:'Where would you like to create the module (must specify a subdirectory)?',
default: function(data){
return path.join(that.name || data.name,'/');
var module = cgUtils.getPrimaryModule(that);
var basedir = module ? path.dirname(module.file) : '.';
return path.join(basedir, that.name || data.name, '/');
},
validate: function(value) {
value = _.str.trim(value);
Expand All @@ -56,7 +58,7 @@ ModuleGenerator.prototype.askFor = function askFor() {

ModuleGenerator.prototype.files = function files() {

var module = cgUtils.getParentModule(path.join(this.dir,'..'));
var module = cgUtils.getParentModule(this, path.join(this.dir,'..'));
module.dependencies.modules.push(_.camelize(this.name));
module.save();
this.log.writeln(chalk.green(' updating') + ' %s',path.basename(module.file));
Expand Down
59 changes: 33 additions & 26 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ exports.injectRoute = function(moduleFile,uirouter,name,route,routeUrl,that){

};

exports.getParentModule = function(dir){
exports.getParentModule = function(that, dir){
//starting this dir, find the first module and return parsed results
if (fs.existsSync(dir)) {
var files = fs.readdirSync(dir);
Expand All @@ -115,26 +115,36 @@ exports.getParentModule = function(dir){
}

if (fs.existsSync(path.join(dir,'.yo-rc.json'))) {
//if we're in the root of the project then bail
return;
//if we're in the root of the project then get the primary module
var module = exports.getPrimaryModule(that);

//if the primary module does not exist in the config file then bail
if (!module) {
return;
}

//return whatever comes back
return ngParseModule.parse(module.file);
}

return exports.getParentModule(path.join(dir,'..'));
};

exports.askForModule = function(type,that,cb){

var primary = that.config.get('primaryModule');
var modules = that.config.get('modules');
var mainModule = ngParseModule.parse('app.js');
mainModule.primary = true;

if (!modules || modules.length === 0) {
cb.bind(that)(mainModule);
return;
}

if (modules.length === 1) {
cb.bind(that)(ngParseModule.parse(modules[0].file));
return;
}

var choices = _.pluck(modules,'name');
choices.unshift(mainModule.name + ' (Primary Application Module)');
choices[0] += ' (Primary Application Module)';

var prompts = [
{
Expand All @@ -147,20 +157,9 @@ exports.askForModule = function(type,that,cb){
];

that.prompt(prompts, function (props) {

var i = choices.indexOf(props.module);

var module;

if (i === 0) {
module = mainModule;
} else {
module = ngParseModule.parse(modules[i-1].file);
}

cb.bind(that)(module);
cb.bind(that)(ngParseModule.parse(modules[i].file));
}.bind(that));

};

exports.askForDir = function(type,that,module,ownDir,cb){
Expand Down Expand Up @@ -188,12 +187,10 @@ exports.askForDir = function(type,that,module,ownDir,cb){
message:'Where would you like to create the '+type+' files?',
default: defaultDir,
validate: function(dir){
if (!module.primary) {
//ensure dir is in module dir or subdir of it
dir = path.resolve(dir);
if (path.relative(that.dir,dir).substring(0,2) === '..') {
return 'Files must be placed inside the module directory or a subdirectory of the module.'
}
//ensure dir is in module dir or subdir of it
dir = path.resolve(dir);
if (path.relative(that.dir,dir).substring(0,2) === '..') {
return 'Files must be placed inside the module directory or a subdirectory of the module.'
}
return true;
}
Expand Down Expand Up @@ -266,3 +263,13 @@ exports.addNamePrompt = function(that,prompts,type){
});
}
}

exports.getPrimaryModule = function(that) {
var primary = that.config.get('primaryModule');
var modules = that.config.get('modules');
for (var i = 0; i < modules.length; i++) {
if (modules[i].name === primary) {
return modules[i];
}
}
};