-
Notifications
You must be signed in to change notification settings - Fork 1
/
mega-base.js
156 lines (131 loc) · 5.11 KB
/
mega-base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'use strict';
var util = require('util');
var path = require('path');
var fs = require('fs');
var chalk = require('chalk');
var yeoman = require('yeoman-generator');
var angularUtils = require('./util.js');
var Generator = module.exports = function Generator() {
yeoman.generators.NamedBase.apply(this, arguments);
var bower;
try {
bower = require(path.join(process.cwd(), 'bower.json'));
} catch (e) {
}
bower = bower || {};
this.appname = bower.name || path.basename(process.cwd());
this.appname = this._.slugify(this._.humanize(this.appname));
this.appName = bower.appName || this._.camelize(this.appname) + 'App';
this.name = this._.dasherize(this.name);
if (this.name[0] === '-')
this.name = this.name.substring(1);
this.cameledName = this._.camelize(this.name);
this.classedName = this._.classify(this.name);
this.dasherizedName = this._.dasherize(this.name);
this.titledName = this._.titleize(this.name);
this.titledSpacedName = this.titledName.replace(/-/g, ' ');
if (typeof this.env.options.appPath === 'undefined') {
this.env.options.appPath = bower.appPath || 'app';
this.options.appPath = this.env.options.appPath;
}
if (typeof this.env.options.scriptPath === 'undefined') {
this.env.options.scriptPath = bower.scriptPath || 'scripts';
this.options.scriptPath = this.env.options.scriptPath;
}
if (typeof this.env.options.commonPath === 'undefined') {
this.env.options.commonPath = bower.commonPath || 'common';
this.options.commonPath = this.env.options.commonPath;
}
this.env.options.modulePath = path.join(this.env.options.appPath, this.env.options.scriptPath);
this.env.options.commonPath = path.join(this.env.options.appPath, this.env.options.commonPath);
if (typeof this.env.options.testPath === 'undefined') {
this.env.options.testPath = bower.testPath || 'test/spec';
}
if (!this.options.all) {
this.options.all = this.options.a || false;
}
if (!this.options.bare) {
this.options.bare = this.options.b || false;
}
if (!this.options.common) {
this.options.common = this.options.c || false;
}
var sourceRoot = '/templates';
this.scriptSuffix = '.js';
this.sourceRoot(path.join(__dirname, sourceRoot));
this.checkForModule = function () {
if (!this.options.common) {
if (!fs.existsSync(path.join(this.env.options.modulePath, this.scriptModuleName, '_module.js'))) {
this.log(chalk.bold.yellow("Warning: A module does not exist for the generated file(s). Please run 'yo ngmega:module " + this.scriptModuleName + "'"));
}
}
}
};
util.inherits(Generator, yeoman.generators.NamedBase);
Generator.prototype.addScriptToIndex = function (script) {
if (!this.moduleClassedName) {
this.moduleClassedName = this._.classify(this.scriptModuleName);
}
var isModule = script.substring(script.lastIndexOf('/') + 1) === '_module.js' ? true : false;
var spliceValue = [];
var beginModule = '<!-- module:' + this.moduleClassedName + ' -->';
var endModule = '<!-- endmodule:' + this.moduleClassedName + ' -->';
var needleValue = isModule ? '<!-- endbuild -->' : endModule;
if (isModule == true) {
spliceValue.push(beginModule);
spliceValue.push('<script src="' + script.toLowerCase().replace(/\\/g, '/') + '"></script>');
spliceValue.push(endModule);
}
else {
spliceValue.push('<script src="' + script.toLowerCase().replace(/\\/g, '/') + '"></script>');
}
try {
var appPath = this.env.options.appPath;
var fullPath = path.join(appPath, 'index.html');
angularUtils.rewriteFile({
file: fullPath,
needle: needleValue,
needleAfter: isModule ? (script.substring(0, script.indexOf('/')) + '.js') : '',
splicable: spliceValue
});
} catch (e) {
this.log.error(chalk.yellow(
'\nUnable to find ' + fullPath + '. Reference to ' + script + ' not added.\n'
));
}
};
Generator.prototype.addScriptToAppJS = function (script) {
var isModule = script.substring(script.lastIndexOf('/') + 1) === '_module.js' ? true : false;
if (isModule) {
var spliceValue = [];
spliceValue.push('\t, \'' + this.moduleCameledName + '\'');
try {
var fullPath = path.join(this.env.options.appPath, this.options.scriptPath, 'app.js');
console.log(fullPath);
angularUtils.rewriteFile({
file: fullPath,
needle: ']);',
needleAfter: '\'' + this.appName + '\', [',
splicable: spliceValue
});
} catch (e) {
this.log.error(chalk.yellow(
'\nUnable to find ' + fullPath + '. Reference to ' + this.moduleCameledName + ' not added.\n'
));
}
}
}
;
Generator.prototype.templateAndReference = function (template, script) {
this.template(template, script);
this.addScriptToIndex(script.substring(script.indexOf('/') + 1));
this.addScriptToAppJS(script.substring(script.indexOf('/') + 1));
};
Generator.prototype.setModuleName = function (name) {
if (name[0] === '-')
name = name.substring(1);
this.moduleCameledName = this._.camelize(name);
this.moduleClassedName = this._.classify(name);
this.moduleDasherizedName = this._.dasherize(name);
this.moduleTitledName = this._.titleize(name);
};