-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.js
156 lines (129 loc) · 4.63 KB
/
utils.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 paths = require('./paths');
var _ = require('lodash');
module.exports = {
getRootTemplatePath: getRootTemplatePath,
getAppTemplatePath: getAppTemplatePath,
getComponentsTemplatePath: getComponentsTemplatePath,
getComponentsTestTemplatePath: getComponentsTestTemplatePath,
getComponentFilePath: getComponentFilePath,
getComponentTestFilePath: getComponentTestFilePath,
getGruntTasksTemplatePath: getGruntTasksTemplatePath,
setModuleComponentNames: setModuleComponentNames,
addScriptTagToIndex: addScriptTagToIndex,
addModuleNameToAppModule: addModuleNameToAppModule,
doesModuleExist: doesModuleExist
};
var filePaths = {
index: paths.destination.app + 'index.html',
appModule: paths.destination.src + 'app.module.js'
};
var replacementTags = {
index:{
appModule: '<script src="src/app.module.js"></script>',
endbuild: '<!-- endbuild -->'
},
appModule:{
endingBrackets: ']);'
}
};
function getFilePath(folderPath, fileName){
return folderPath + '_' + fileName;
}
function getAppTemplatePath(fileName){
return getFilePath(paths.template.app, fileName);
}
function getRootTemplatePath(fileName){
return getFilePath(paths.template.root, fileName);
}
function getComponentsTemplatePath(fileName){
return getFilePath(paths.template.components, fileName);
}
function getComponentsTestTemplatePath(fileName){
return getFilePath(paths.template.componentTests, fileName);
}
function getGruntTasksTemplatePath(fileName){
return getFilePath(paths.template.gruntTasks, fileName);
}
function getComponentFilePath(module, component, isHTML){
var componentPath = paths.destination.src + module + '/' + component;
componentPath += isHTML? '.html' : '.js';
return componentPath;
}
function getComponentTestFilePath(module, component){
return paths.destination.tests + module + '/' + component + '.js';
}
function setModuleComponentNames(retValObject, dottedName){
var names = dottedName.split('.');
if(names.length < 2){
throw 'Component must specify a module';
}
retValObject.module = names[0];
retValObject.component = names[1];
}
function addScriptTagToIndex(self, scriptPath){
var pathToIndexFile = filePaths.index;
var indexReplacementTag = _.contains(scriptPath, 'module')? replacementTags.index.appModule : replacementTags.index.endbuild;
var indexFile = self.readFileAsString(pathToIndexFile);
var splitIndexFile = indexFile.split('\n');
var indexOfReplacementTag = indexOfTag(splitIndexFile, indexReplacementTag);
var replacingScriptTag = insertSpaces(getScriptTag(scriptPath), countSpaces(splitIndexFile[indexOfReplacementTag - 1]));
splitIndexFile.splice(indexOfReplacementTag, 0, replacingScriptTag);
indexFile = splitIndexFile.join('\n');
self.writeFileFromString(indexFile, pathToIndexFile);
}
function getScriptTag(scriptPath){
var completePathToScript = scriptPath.replace(paths.destination.src,'');
return '<script src="src/' + completePathToScript + '"></script>';
}
function addModuleNameToAppModule(self, moduleName){
var pathToAppModuleFile = paths.destination.src + 'app.module.js';
var moduleReplacementTag = ']);';
var moduleFile = self.readFileAsString(pathToAppModuleFile);
var splitModuleFile = moduleFile.split('\n');
var indexOfModuleTag = indexOfTag(splitModuleFile, moduleReplacementTag);
var placeACommaIndex = indexOfModuleTag - 1;
var moduleNameToBeInserted = insertSpaces("'app." + moduleName + "'", countSpaces(splitModuleFile[placeACommaIndex]));
splitModuleFile[placeACommaIndex] = splitModuleFile[placeACommaIndex] + ',';
splitModuleFile.splice(indexOfModuleTag, 0, moduleNameToBeInserted);
moduleFile = splitModuleFile.join('\n');
self.writeFileFromString(moduleFile, pathToAppModuleFile);
}
function indexOfTag(fileArr, tag){
var tagIndex = _.findLastIndex(fileArr, function(value) {
return _.contains(value, tag) ;
});
return tagIndex;
}
function countSpaces(valueArr){
var spacesCount = 0;
_.forEach(valueArr, function(char){
if(char === ' '){
spacesCount++;
}
else{
return false; //break on first char
}
});
return spacesCount;
}
function insertSpaces(value, count){
for(var i = 0, len = count; i < len; i++){
value = ' ' + value;
}
return value;
}
function doesModuleExist(self, moduleName){
var pathToModuleFile = paths.destination.src + moduleName +'/' +moduleName+ '.module.js';
var shouldCreateModuleFile = true;
try{
var moduleFile = self.readFileAsString(pathToModuleFile);
if(moduleFile.length > 0){
shouldCreateModuleFile = false;
}
}
catch(e){
console.log(moduleName + ' module not found');
}
return shouldCreateModuleFile;
}