Skip to content

Commit 84d2e4e

Browse files
committed
feat(gen): generate a more modular project stucture
1 parent 8e1d6fd commit 84d2e4e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+151
-130
lines changed

app/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ var Generator = module.exports = function Generator(args, options) {
6161
coffee: this.options.coffee,
6262
travis: true,
6363
'skip-install': this.options['skip-install']
64-
}
64+
}
6565
}
6666
});
6767

@@ -162,7 +162,7 @@ Generator.prototype.bootstrapFiles = function bootstrapFiles() {
162162
});
163163
};
164164

165-
Generator.prototype.bootstrapJS = function bootstrapJS() {
165+
Generator.prototype.bootstrapJs = function bootstrapJs() {
166166
if (!this.bootstrap) {
167167
return; // Skip if disabled.
168168
}

constant/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
var path = require('path');
33
var util = require('util');
44
var ScriptBase = require('../script-base.js');
5-
var angularUtils = require('../util.js');
65

76

87
module.exports = Generator;
@@ -14,7 +13,6 @@ function Generator() {
1413
util.inherits(Generator, ScriptBase);
1514

1615
Generator.prototype.createServiceFiles = function createServiceFiles() {
17-
this.appTemplate('service/constant', 'scripts/services/' + this.name);
18-
this.testTemplate('spec/service', 'services/' + this.name);
19-
this.addScriptToIndex('services/' + this.name);
16+
this.appTemplate('service/constant');
17+
this.testTemplate('spec/service');
2018
};

controller/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ function Generator() {
1919
util.inherits(Generator, ScriptBase);
2020

2121
Generator.prototype.createControllerFiles = function createControllerFiles() {
22-
this.appTemplate('controller', 'scripts/controllers/' + this.name);
23-
this.testTemplate('spec/controller', 'controllers/' + this.name);
24-
this.addScriptToIndex('controllers/' + this.name);
22+
this.appTemplate('controller');
23+
this.testTemplate('spec/controller');
2524
};

decorator/index.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Generator.prototype.askForOverwrite = function askForOverwrite() {
1414
var cb = this.async();
1515

1616
// TODO: Any yeoman.util function to handle this?
17-
var fileExists = fs.existsSync(this.env.cwd + '/app/scripts/' + buildRelativePath(this.fileName) + ".js");
17+
var fileExists = fs.existsSync(this.env.cwd + '/app/scripts/' + buildRelativePath(this.fileName) + '.js');
1818
if (fileExists) {
1919
var prompts = [{
2020
type: 'confirm',
@@ -28,10 +28,8 @@ Generator.prototype.askForOverwrite = function askForOverwrite() {
2828

2929
cb();
3030
}.bind(this));
31-
}
32-
else{
31+
} else {
3332
cb();
34-
return;
3533
}
3634
};
3735

@@ -42,19 +40,18 @@ Generator.prototype.askForNewName = function askForNewName() {
4240
cb();
4341
return;
4442
}
45-
else {
46-
var prompts = [];
47-
prompts.push({
48-
name: 'decoratorName',
49-
message: 'Alternative name for the decorator'
50-
});
5143

52-
this.prompt(prompts, function (props) {
53-
this.fileName = props.decoratorName;
44+
var prompts = [];
45+
prompts.push({
46+
name: 'decoratorName',
47+
message: 'Alternative name for the decorator'
48+
});
5449

55-
cb();
56-
}.bind(this));
57-
}
50+
this.prompt(prompts, function (props) {
51+
this.fileName = props.decoratorName;
52+
53+
cb();
54+
}.bind(this));
5855
};
5956

6057
Generator.prototype.createDecoratorFiles = function createDecoratorFiles() {
@@ -63,5 +60,5 @@ Generator.prototype.createDecoratorFiles = function createDecoratorFiles() {
6360
};
6461

6562
function buildRelativePath(fileName){
66-
return 'decorators/' + fileName + "Decorator";
63+
return 'decorators/' + fileName + 'Decorator';
6764
}

directive/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ function Generator() {
1414
util.inherits(Generator, ScriptBase);
1515

1616
Generator.prototype.createDirectiveFiles = function createDirectiveFiles() {
17-
this.appTemplate('directive', 'scripts/directives/' + this.name);
18-
this.testTemplate('spec/directive', 'directives/' + this.name);
19-
this.addScriptToIndex('directives/' + this.name);
17+
this.appTemplate('directive');
18+
this.testTemplate('spec/directive');
2019
};

factory/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ function Generator() {
1414
util.inherits(Generator, ScriptBase);
1515

1616
Generator.prototype.createServiceFiles = function createServiceFiles() {
17-
this.appTemplate('service/factory', 'scripts/services/' + this.name);
18-
this.testTemplate('spec/service', 'services/' + this.name);
19-
this.addScriptToIndex('services/' + this.name);
17+
this.appTemplate('service/factory');
18+
this.testTemplate('spec/service');
2019
};

provider/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ function Generator() {
1414
util.inherits(Generator, ScriptBase);
1515

1616
Generator.prototype.createServiceFiles = function createServiceFiles() {
17-
this.appTemplate('service/provider', 'scripts/services/' + this.name);
18-
this.testTemplate('spec/service', 'services/' + this.name);
19-
this.addScriptToIndex('services/' + this.name);
17+
this.appTemplate('service/provider');
18+
this.testTemplate('spec/service');
2019
};

route/index.js

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,34 @@ function Generator() {
1515
util.inherits(Generator, ScriptBase);
1616

1717
Generator.prototype.rewriteAppJs = function () {
18-
if (this.env.options.coffee) {
19-
angularUtils.rewriteFile({
20-
file: path.join(this.env.options.appPath, 'scripts/app.coffee'),
21-
needle: '.otherwise',
22-
splicable: [
23-
'.when \'/' + this.name + '\',',
24-
' templateUrl: \'views/' + this.name + '.html\',',
25-
' controller: \'' + this._.classify(this.name) + 'Ctrl\''
26-
]
27-
});
18+
var htmlTemplatePath = this.name + '.html',
19+
splicable,
20+
filePath = path.join(this.env.options.appPath, 'scripts/app.');
21+
22+
if (htmlTemplatePath.indexOf('/') === -1) {
23+
htmlTemplatePath = 'views/' + htmlTemplatePath;
2824
}
29-
else {
30-
angularUtils.rewriteFile({
31-
file: path.join(this.env.options.appPath, 'scripts/app.js'),
32-
needle: '.otherwise',
33-
splicable: [
34-
'.when(\'/' + this.name + '\', {',
35-
' templateUrl: \'views/' + this.name + '.html\',',
36-
' controller: \'' + this._.classify(this.name) + 'Ctrl\'',
37-
'})'
38-
]
39-
});
25+
26+
if (this.env.options.coffee) {
27+
splicable = [
28+
'.when \'/' + this.name + '\',',
29+
' templateUrl: \'' + htmlTemplatePath + '\',',
30+
' controller: \'' + this._.classify(this.name) + 'Ctrl\''
31+
];
32+
filePath += 'coffee';
33+
} else {
34+
splicable = [
35+
'.when(\'/' + this.name + '\', {',
36+
' templateUrl: \'' + htmlTemplatePath + '\',',
37+
' controller: \'' + this._.classify(this.name) + 'Ctrl\'',
38+
'})'
39+
];
40+
filePath += 'js';
4041
}
42+
43+
angularUtils.rewriteFile({
44+
file: filePath,
45+
needle: '.otherwise',
46+
splicable: splicable
47+
});
4148
};

script-base.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,43 +60,64 @@ function Generator() {
6060
}
6161

6262
this.sourceRoot(path.join(__dirname, sourceRoot));
63+
64+
this.moduleName = this._.camelize(this.appname) + 'App';
65+
66+
this.namespace = [];
67+
if (this.name.indexOf('/') !== -1) {
68+
this.namespace = this.name.split('/');
69+
this.name = this.namespace.pop();
70+
71+
this.moduleName += '.' + this.namespace.join('.'); // add to parent ?
72+
}
73+
6374
}
6475

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

67-
Generator.prototype.appTemplate = function (src, dest) {
78+
Generator.prototype._dest = function (src) {
79+
if (src.indexOf('spec/') === 0) {
80+
src = src.substr(5);
81+
} else if (src.indexOf('service/') === 0) {
82+
src = src.substr(8);
83+
}
84+
return path.join((this.namespace.join('/') || src), this.name);
85+
};
86+
87+
Generator.prototype.appTemplate = function (src) {
6888
yeoman.generators.Base.prototype.template.apply(this, [
6989
src + this.scriptSuffix,
70-
path.join(this.env.options.appPath, dest) + this.scriptSuffix
90+
path.join(this.env.options.appPath, this._dest(src)) + this.scriptSuffix
7191
]);
92+
this.addScriptToIndex(src);
7293
};
7394

74-
Generator.prototype.testTemplate = function (src, dest) {
95+
Generator.prototype.testTemplate = function (src) {
7596
yeoman.generators.Base.prototype.template.apply(this, [
7697
src + this.scriptSuffix,
77-
path.join(this.env.options.testPath, dest) + this.scriptSuffix
98+
path.join(this.env.options.testPath, this._dest(src)) + this.scriptSuffix
7899
]);
79100
};
80101

81-
Generator.prototype.htmlTemplate = function (src, dest) {
102+
Generator.prototype.htmlTemplate = function (src) {
82103
yeoman.generators.Base.prototype.template.apply(this, [
83104
src,
84-
path.join(this.env.options.appPath, dest)
105+
path.join(this.env.options.appPath, this._dest(src))
85106
]);
86107
};
87108

88-
Generator.prototype.addScriptToIndex = function (script) {
109+
Generator.prototype.addScriptToIndex = function (src) {
89110
try {
90111
var appPath = this.env.options.appPath;
91112
var fullPath = path.join(appPath, 'index.html');
92113
angularUtils.rewriteFile({
93114
file: fullPath,
94115
needle: '<!-- endbuild -->',
95116
splicable: [
96-
'<script src="scripts/' + script + '.js"></script>'
117+
'<script src="' + this._dest(src) + '.js"></script>'
97118
]
98119
});
99120
} catch (e) {
100-
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + script + '.js ' + 'not added.\n'.yellow);
121+
console.log('\nUnable to find '.yellow + fullPath + '. Reference to '.yellow + this._dest(src) + '.js ' + 'not added.\n'.yellow);
101122
}
102123
};

service/index.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ function Generator() {
1414
util.inherits(Generator, ScriptBase);
1515

1616
Generator.prototype.createServiceFiles = function createServiceFiles() {
17-
this.appTemplate('service/service', 'scripts/services/' + this.name);
18-
this.testTemplate('spec/service', 'services/' + this.name);
19-
this.addScriptToIndex('services/' + this.name);
17+
this.appTemplate('service/service');
18+
this.testTemplate('spec/service');
2019
};

0 commit comments

Comments
 (0)