forked from kevbaker/generator-angular-famous-ionic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
158 lines (147 loc) · 5.4 KB
/
utils.js
File metadata and controls
158 lines (147 loc) · 5.4 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
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
157
158
'use strict';
var gulp = require('gulp');
var ginject = require('gulp-inject');
var Q = require('q');
var path = require('path');
var _ = require('lodash');
var fs = require('fs');
var subcomponents = require('./class/subcomponents.js');
/**
* Inject the list of angular modules of the main.js file
* @param {String} directory - The folder containing the modules
* @param {String} suffix - The suffix of the target application
* @param {String[]} modules - The list of modules
*
* @returns {Promise} - An empty promise after the injection is done
*/
exports.injectModules = function(directory, suffix, modules) {
var deferred = Q.defer();
var mainFile = path.join(directory, 'main' + suffix + '.js');
gulp.src(mainFile)
.pipe(ginject(gulp.src(mainFile, {
read: false
}), {
starttag: '// inject:modules start',
endtag: '// inject:modules end',
transform: function() {
return _.map(_.sortBy(_.uniq(modules)), function(module) {
return 'require(\'./' + module + '\')(namespace).name';
}).join(',' + '\n ');
}
}))
.pipe(gulp.dest(directory))
.on('error', function(err) {
deferred.reject(err);
})
.on('end', function() {
deferred.resolve();
});
return deferred.promise;
};
/**
* Inject the list of angular sub components of the module file
* @param {Generator} generator - The generator
* @param {String} directory - The module folder
*
* @returns {Promise} - An empty promise after the injection is done
*/
exports.injectSubComponent = function(generator, directory) {
var deferred = Q.defer();
var mainFile = path.join(directory, 'index.js');
generator.getDirectories(directory)
.then(function(components) {
// excluding the 'views' folder as it only contains html partials, as well as unexpected folders (styles, images, etc...) that should not be required
components = _.intersection(components, subcomponents);
gulp.src(mainFile)
.pipe(ginject(gulp.src(mainFile, {
read: false
}), {
starttag: '// inject:folders start',
endtag: '// inject:folders end',
transform: function() {
return _.map(_.sortBy(_.uniq(components)), function(component) {
return 'require(\'./' + component + '\')(app);';
}).join('\n');
}
}))
.pipe(gulp.dest(directory))
.on('error', function(err) {
deferred.reject(err);
})
.on('end', function() {
deferred.resolve();
});
});
return deferred.promise;
};
/**
* Inject the list of angular components of the same kind in their index.js file
* @param {String} directory - The folder containing the components
*
* @returns {Promise} - An empty promise after the injection is done
*/
exports.injectComponent = function(directory) {
var deferred = Q.defer();
var mainFile = path.join(directory, 'index.js');
var searchFiles = [
path.join(directory, '**/*.js'),
'!' + path.join(directory, 'index.js'),
'!' + path.join(directory, '**/*.test.js'),
'!' + path.join(directory, '**/*.tests.js'),
'!' + path.join(directory, '**/*.spec.js'),
'!' + path.join(directory, '**/*.specs.js')
];
gulp.src(mainFile)
.pipe(ginject(gulp.src(searchFiles, {
read: false
}), {
starttag: '// inject:start',
endtag: '// inject:end',
transform: function(filepath) {
var paths = filepath.split('/');
var filename = paths[paths.length - 1];
filename = filename.replace('.js', '');
return 'require(\'./' + filename + '\')(app);';
}
}))
.pipe(gulp.dest(directory))
.on('error', function(err) {
deferred.reject(err);
})
.on('end', function() {
deferred.resolve();
});
return deferred.promise;
};
/**
* Create an index.js file for an angular component (service, directive, etc..)
* for referencing all the components of same kind in the folder
* @param {Generator} generator - The generator
* @param {String} sourceDir - The folder were the template file is located (for example '../module/services' or '../component')
* @param {String} targetDir - The folder were the file should be created
*
* @returns {void}
*/
exports.createIndexFile = function(generator, sourceDir, targetDir) {
if(!fs.existsSync(path.join(targetDir, 'index.js'))) {
generator.template(sourceDir + '/index.js', path.join(targetDir, 'index.js'));
}
};
/**
* Get the angular modules list based on the choice of frameworks (ionic, famous, etc...) for the generator
* @param {Generator} generator - The generator
* @returns {String} - The list of ng modules as a comma separated string
*/
exports.getNgModules = function(generator) {
var retVal = [];
if(generator.ionic) {
retVal.push('\'ionic\'');
}
if(generator.famous) {
retVal.push('\'famous.angular\'');
}
if(generator.ngCordova) {
retVal.push('\'ngCordova\'');
}
return retVal.join(', ');
};