This repository has been archived by the owner on Nov 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
oasp-base-generator.js
120 lines (111 loc) · 4.84 KB
/
oasp-base-generator.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
var paths = require('path');
var glob = require('glob');
var ngParseModule = require('ng-parse-module');
var yeoman = require('yeoman-generator');
var _ = require('underscore');
_.str = require('underscore.string');
var fs = require('fs-extra');
if (!String.prototype.format) {
String.prototype.format = function () {
'use strict';
var args = arguments;
return this.replace(/{(\d+)}/g, function (match, number) {
return typeof args[number] !== 'undefined' ? args[number] : match;
});
};
}
module.exports = yeoman.generators.Base.extend({
constructor: function () {
yeoman.generators.Base.apply(this, arguments);
var that = this;
this.hasBeenCalledFromRoot = function (rootPath, currentPath) {
return rootPath === currentPath;
};
this.pathBuilder = {
calculateTestBasePath: function (path, appPath, testPath) {
return path.replace(appPath, testPath);
}
};
this.nameBuilder = {
ngModuleName: function () {
return _.map(arguments, function (element) {
return _.str.camelize(element);
}
).join('.');
},
ngDirectiveName: function (directive) {
return _.str.camelize(directive);
},
ngControllerName: function (controller) {
return _.str(_.str.camelize(controller + 'Cntl')).capitalize().value();
},
trainItemName: function (item) {
return _.str.dasherize(_.str.underscored(item));
},
fileNgControllerName: function (module) {
return _.str.dasherize(_.str.underscored(module)) + '.controller.js';
},
fileNgModuleName: function (module) {
return _.str.dasherize(_.str.underscored(module)) + '.module.js';
}
};
this.moduleFinder = {
findClosestModule: function (rootPath, currentPath) {
var modules;
while (currentPath !== rootPath) {
modules = glob.sync('*.module.js', {cwd: currentPath, realpath: true});
if (modules.length > 0) {
return {
modulePath: modules[0],
moduleDir: paths.dirname(modules[0]),
relativeDirPath: paths.relative(rootPath, currentPath),
fileName: paths.basename(modules[0]),
parsedModule: ngParseModule.parse(modules[0])
};
}
currentPath = paths.dirname(currentPath);
}
return null;
},
findModuleByName: function (appPath, appDir, moduleName) {
var modules, currentPath = paths.join(appPath, appDir, that.nameBuilder.trainItemName(moduleName));
modules = glob.sync('*.module.js', {cwd: currentPath, realpath: true});
if (modules.length > 0) {
return {
modulePath: modules[0],
moduleDir: paths.dirname(modules[0]),
relativeDirPath: paths.relative(appPath, currentPath),
fileName: paths.basename(modules[0]),
parsedModule: ngParseModule.parse(modules[0])
};
} else {
return null;
}
},
findAppModule: function (appPath, appDir) {
var modules, currentPath = paths.join(appPath, appDir);
modules = glob.sync('*.module.js', {cwd: currentPath, realpath: true});
if (modules.length > 0) {
return {
modulePath: modules[0],
moduleDir: paths.dirname(modules[0]),
relativeDirPath: paths.relative(appPath, currentPath),
fileName: paths.basename(modules[0]),
parsedModule: ngParseModule.parse(modules[0])
};
} else {
return null;
}
}
};
//////////////////////////////////////////////////////////////////////////////////
this.paths = fs.readJsonSync(this.destinationPath('config.json')).paths;
this.isCalledFromRoot = this.hasBeenCalledFromRoot(this.destinationPath(), this.env.cwd);
//calculate target module
if (this.isCalledFromRoot) {
this.targetParentModule = this.moduleFinder.findAppModule(this.destinationPath(), this.paths.src);
} else {
this.targetParentModule = this.moduleFinder.findClosestModule(this.destinationPath(), this.env.cwd);
}
}
});