forked from ionic-team/ionic-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.js
More file actions
75 lines (62 loc) · 2.3 KB
/
generator.js
File metadata and controls
75 lines (62 loc) · 2.3 KB
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
var _ = require('lodash'),
fs = require('fs'),
path = require('path'),
mkdirp = require('mkdirp-no-bin');
module.exports = Generator;
function Generator(options) {
this.name = options.name;
this.type = options.generator;
this.appDirectory = options.appDirectory;
this.isTS = options.isTS;
//templateVars
this.fileName = _.kebabCase(this.name);
this.jsClassName = _.capitalize(_.camelCase(this.name));
}
Generator.prototype.run = function(){
this.makeDirectories();
this.renderTemplates();
}
Generator.prototype.makeDirectories = function(){
if (!this.directory) {
throw new Error('Generators must define their directory in their constructor.\nEx: \'pages\', \'components\', etc.');
}
mkdirp.sync(path.join(this.appDirectory, 'app', this.directory, this.fileName));
}
Generator.prototype.renderTemplates = function renderTemplates() {
var templates = this.loadTemplates();
templates.forEach(function(template) {
var renderedTemplate = this.renderTemplate(template);
var renderedTemplateDest = path.join(this.appDirectory, 'app', this.directory, this.fileName, this.fileName + template.extension);
console.log('√ Create'.blue, path.relative(this.appDirectory, renderedTemplateDest));
fs.writeFileSync(renderedTemplateDest, renderedTemplate);
}, this);
}
Generator.prototype.loadTemplates = function() {
var generatorPath = path.join(__dirname, 'generators', this.type);
var templates = [];
fs.readdirSync(generatorPath)
.forEach(function(template) {
var type;
// Go through all the files in the folder, grab the templates, read in the file contents
// return as template type, contents
if (template.indexOf('.tmpl') == -1) {
return;
}
//If not using typescript, ignore .ts files
if(!this.isTS && template.indexOf('.ts') != -1){
return;
}
//If using typescript, ignore .js files
if(this.isTS && template.indexOf('.js') != -1){
return;
}
templates.push({path: path.join(generatorPath, template), extension: path.extname(template)});
}, this);
return templates;
};
Generator.prototype.renderTemplate = function(template) {
var templateContents = fs.readFileSync(template.path, 'utf8');
var templateCompiler = _.template(templateContents);
var result = templateCompiler(this);
return result;
};