forked from cgross/generator-cg-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
89 lines (72 loc) · 2.75 KB
/
utils.js
File metadata and controls
89 lines (72 loc) · 2.75 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var path = require('path');
var fs = require('fs');
var _ = require('underscore');
var chalk = require('chalk');
_.str = require('underscore.string');
_.mixin(_.str.exports());
exports.addToFile = function(filename,lineToAdd,beforeMarker,spacing){
try {
var fullPath = path.join(process.cwd(),filename);
var fileSrc = fs.readFileSync(fullPath,'utf8');
var indexOf = fileSrc.indexOf(beforeMarker);
var lineStart = fileSrc.substring(0,indexOf).lastIndexOf('\n') + 1;
var indent = fileSrc.substring(lineStart,indexOf);
fileSrc = fileSrc.substring(0,indexOf) + lineToAdd + "\n" + indent + fileSrc.substring(indexOf);
fs.writeFileSync(fullPath,fileSrc);
} catch(e) {
throw e;
}
};
exports.JS_MARKER = "<!-- Add New Component JS Above -->";
exports.LESS_MARKER = "/* Add Component LESS Above */";
exports.ROUTE_MARKER = "/* Add New Routes Above */";
exports.STATE_MARKER = "/* Add New States Above */";
exports.cleanDirectory = function(directoryName) {
if (_(directoryName).startsWith('/') || _(directoryName).startsWith('\\')) {
directoryName = directoryName.substring(1);
}
if (_(directoryName).endsWith('/') || _(directoryName).endsWith('\\')) {
directoryName = directoryName.substring(0,directoryName.length - 1);
}
return directoryName + '/';
};
exports.processTemplates = function(name,dir,type,that,defaultDir,configName){
if (!defaultDir) {
defaultDir = 'templates'
}
if (!configName) {
configName = type + 'Templates';
}
var templateDirectory = path.join(path.dirname(that.resolved),defaultDir);
if(that.config.get(configName)){
templateDirectory = path.join(process.cwd(),that.config.get(configName));
}
_.chain(fs.readdirSync(templateDirectory))
.filter(function(template){
return template[0] !== '.';
})
.each(function(template){
var customTemplateName = template.replace(type,name);
var templateFile = path.join(templateDirectory,template);
//create the file
that.template(templateFile,dir + customTemplateName);
//inject the file reference into index.html/app.less/etc as appropriate
exports.inject(dir + customTemplateName,that);
});
};
exports.inject = function(filename,that) {
//special case to skip unit tests
if (_(filename).endsWith('-spec.js')) {
return;
}
var ext = path.extname(filename);
if (ext[0] === '.') {
ext = ext.substring(1);
}
var config = that.config.get('inject')[ext];
if (config) {
var lineTemplate = _.template(config.template)({filename:filename});
exports.addToFile(config.file,lineTemplate,config.marker);
that.log.writeln(chalk.green(' updating') + ' %s',config.file);
}
};