forked from yeoman/generator-angular
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathconfig.js
More file actions
83 lines (71 loc) · 2.51 KB
/
config.js
File metadata and controls
83 lines (71 loc) · 2.51 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
'use strict';
var fs = require('fs');
var util = require('util');
var path = require('path');
var slash = require('slash');
module.exports = {
getConfig: getConfig,
rewriteFile: rewriteFile
};
function getConfig(args) {
var fullPath = path.join(args.path, args.file);
var config = null;
if (fs.existsSync(fullPath))
config = JSON.parse(fs.readFileSync(fullPath, 'utf8'));
else
config = JSON.parse(fs.readFileSync(path.join(__dirname, 'config/templates/config-component.json'), 'utf8'));
calculateFullPath(config, config['structure'], undefined, config.structure.type);
calculateAppPath(config, config['structure'].app, undefined, config.structure.type);
return config;
}
function rewriteFile(args) {
args.path = args.path || process.cwd();
var fullPath = path.join(args.path, args.file);
var file = fs.readFileSync(fullPath, 'utf8');
for (var key in args.map)
file = file.replace(new RegExp(escapeRegExp(key), 'g'), args.map[key]);
fs.writeFileSync(fullPath, file);
};
function calculateFullPath(rootNode, node, nodePath, type) {
if (typeof(node) == 'object')
for (var key in node) {
if (typeof node[key].path !== 'undefined') {
if (!rootNode[key])
rootNode[key] = {};
rootNode[key].path = slash(substitutePath(node, key));
rootNode[key].fullPath = slash(path.join((nodePath ? nodePath : ''), rootNode[key].path));
if (type != 'feature') {
rootNode[key].gruntPath = rootNode[key].fullPath;
}
calculateFullPath(rootNode, node[key], rootNode[key].fullPath, type);
}
}
}
function calculateAppPath(rootNode, node, nodePath, type) {
if (typeof(node) == 'object')
for (var key in node) {
if (typeof node[key].path !== 'undefined') {
if (!rootNode[key])
rootNode[key] = {};
var _path = substitutePath(node, key);
rootNode[key].appPath = slash(path.join((nodePath ? nodePath : ''), _path));
if (type == 'feature') {
rootNode[key].gruntPath = rootNode[key].appPath
}
calculateAppPath(rootNode, node[key], rootNode[key].appPath, type);
}
}
}
function substitutePath(node, key) {
var subPath = node[key].path.match(/\[.+\]/);
if (subPath != null) {
var subKeyName = subPath[0].replace(/\[/, '').replace(/\]/, '');
return node[key].path.replace(subPath[0], substitutePath(node, subKeyName));
}
else {
return node[key].path;
}
}
function escapeRegExp (str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&');
}