Uh oh!
There was an error while loading. Please reload this page.
forked from aaronallport/generator-angular-require
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
Latest commit
123 lines (96 loc) · 3.05 KB
/
Copy pathutil.js
File metadata and controls
123 lines (96 loc) · 3.05 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
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
'use strict';
varpath=require('path');
varfs=require('fs');
module.exports={
rewrite: rewrite,
rewriteFile: rewriteFile,
appName: appName,
injectIntoFile: injectIntoFile
};
functionrewriteFile(args){
args.path=args.path||process.cwd();
varfullPath=path.join(args.path,args.file);
args.spliceWithinLine=args.spliceWithinLine||false;
args.haystack=fs.readFileSync(fullPath,'utf8');
varbody=rewrite(args);
fs.writeFileSync(fullPath,body);
}
functioninjectIntoFile(appPath,moduleName,attachedComponentName,injectedModuleName){
// Set up config object
varconfig={
file: path.join(
appPath,
'scripts/app.js'),
needle: "",
splicable: [],
spliceWithinLine: true
};
// Insert AMD module dependency
config.needle="]/*deps*/";
config.splicable=[", '"+moduleName+"'"];
rewriteFile(config);
// Ensure our module is invoked
config.needle=")/*invoke*/";
config.splicable=[", "+attachedComponentName];
rewriteFile(config);
// Check for the existence of a controllers module as an
// application dependency. If it doesn't exist, inject it
varapp_js=fs.readFileSync(path.join(appPath,'scripts/app.js'),'utf8');
varregex_app_module=newRegExp(injectedModuleName);
if(!regex_app_module.test(app_js)){
// Inject the controllers module as an AngularJS module dependency
config.needle="/*angJSDeps*/";
config.splicable=["'"+injectedModuleName+"',\n"];
rewriteFile(config);
}
}
functionescapeRegExp(str){
returnstr.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,'\\$&');
}
functionrewrite(args){
// check if splicable is already in the body text
varre=newRegExp(args.splicable.map(function(line){
return'\s*'+escapeRegExp(line);
}).join('\n'));
if(re.test(args.haystack)){
returnargs.haystack;
}
varlines=args.haystack.split('\n');
varotherwiseLineIndex=0;
lines.forEach(function(line,i){
if(line.indexOf(args.needle)!==-1){
otherwiseLineIndex=i;
}
});
if((otherwiseLineIndex>0)&&(args.spliceWithinLine)){
varline=lines[otherwiseLineIndex];
varindexToSpliceAt=line.indexOf(args.needle);
lines[otherwiseLineIndex]=line.substr(0,indexToSpliceAt)+args.splicable[0]+line.substr(indexToSpliceAt);
returnlines.join('\n');
}
varspaces=0;
while(lines[otherwiseLineIndex].charAt(spaces)===' '){
spaces+=1;
}
varspaceStr='';
while((spaces-=1)>=0){
spaceStr+=' ';
}
lines.splice(otherwiseLineIndex,0,args.splicable.map(function(line){
returnspaceStr+line;
}).join('\n'));
returnlines.join('\n');
}
functionappName(self){
varcounter=0,suffix=self.options['app-suffix'];
// Have to check this because of generator bug #386
process.argv.forEach(function(val){
if(val.indexOf('--app-suffix')>-1){
counter++;
}
});
if(counter===0||(typeofsuffix==='boolean'&&suffix)){
suffix='App';
}
returnsuffix ? self._.classify(suffix) : '';
}