-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
241 lines (210 loc) · 7.45 KB
/
gulpfile.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
////////////////////////////////////////////////////////////
// Requirements //
////////////////////////////////////////////////////////////
var gulp = require('gulp-help')(require('gulp'));
var plumber = require('gulp-plumber'),
watch = require('./semantic/tasks/watch'),
build = require('./semantic/tasks/build'),
buildJS = require('./semantic/tasks/build/javascript'),
buildCSS = require('./semantic/tasks/build/css'),
buildAssets = require('./semantic/tasks/build/assets'),
clean = require('./semantic/tasks/clean'),
nodemon = require('gulp-nodemon'),
jspm = require('jspm'),
path = require('path'),
pluralize = require('pluralize'),
rename = require('gulp-rename'),
template = require('gulp-template'),
uglify = require('gulp-uglify'),
htmlreplace = require('gulp-html-replace'),
ngAnnotate = require('gulp-ng-annotate'),
yargs = require('yargs').argv,
rimraf = require('rimraf'),
jshint = require('gulp-jshint'),
stylish = require('jshint-stylish'),
exec = require('child_process').exec;
////////////////////////////////////////////////////////////
// Functions //
////////////////////////////////////////////////////////////
//error logging
//
var onError = function(err) {
console.log(err)
};
// helper method to resolveToApp paths
//
var resolveTo = function(resolvePath) {
return function(glob) {
glob = glob || '';
return path.resolve(path.join(__dirname, resolvePath, glob));
}
};
var resolveToApp = resolveTo('public/app'); // public/app/{glob}
var resolveToComponents = resolveTo('public/app/components'); // public/app/components/{glob}
var resolveToServices = resolveTo('public/app/services'); // public/app/services/{glob}
var resolveToModels = resolveTo('app/models'); // app/models/{glob}
var resolveToRoutes = resolveTo('app/routes'); // app/routes/{glob}
// map of paths
//
var paths = {
css: resolveToApp('**/*.css'),
html: [
resolveToApp('**/*.html'),
path.join('public', 'index.html')
],
blankComponent: path.join(__dirname, 'generator', 'component/**/*.**'),
blankService: path.join(__dirname, 'generator', 'service/**/*.**'),
blankModel: path.join(__dirname, 'generator', 'model/**/*.**'),
dist: path.join(__dirname, 'dist/')
};
////////////////////////////////////////////////////////////
// Tasks //
////////////////////////////////////////////////////////////
//Launch app and watch for changes
//
gulp.task('serve', function() {
nodemon({
script: 'server.js',
ext: 'html js',
watch: [
'app/**/*',
'public/app/**/*'
],
tasks: ['jshint']
})
.on('restart', function() {
console.log('Restarted for changes');
})
});
//Launch Mongo
//
gulp.task('mongo', exec('mongod'));
//jshint task
//
gulp.task('jshint', function() {
gulp.src(['./app/**/*.js', './public/app/**/*.js'])
.pipe(plumber({
errorHandler : onError
}))
.pipe(jshint())
.pipe(jshint.reporter(stylish));
});
//Build and minify app using jspm
//
gulp.task('build', function() {
var dist = path.join(paths.dist + 'app.js');
rimraf.sync(path.join(paths.dist, '*'));
// Use JSPM to bundle our app
return jspm.bundleSFX(resolveToApp('app'), dist, {})
.then(function() {
// Also create a fully annotated minified copy
return gulp.src(dist)
.pipe(ngAnnotate())
.pipe(uglify())
.pipe(rename('app.min.js'))
.pipe(gulp.dest(paths.dist))
})
.then(function() {
// Inject minified script into index
return gulp.src('public/index.html')
.pipe(htmlreplace({
'js': 'app.min.js'
}))
.pipe(gulp.dest(paths.dist));
});
});
////////////////////////////////////////////////////////////
// Generators //
////////////////////////////////////////////////////////////
//Create Angular components
//
gulp.task('component', function(){
var cap = function(val){
return val.charAt(0).toUpperCase() + val.slice(1);
};
var name = yargs.name;
var parentPath = yargs.parent || '';
var destPath = path.join(resolveToComponents(), parentPath, name);
return gulp.src(paths.blankComponent)
.pipe(template({
name: name,
upCaseName: cap(name)
}))
.pipe(rename(function(path){
path.basename = path.basename.replace('temp', name);
}))
.pipe(gulp.dest(destPath));
});
//Create Angular services
//
gulp.task('service', function(){
var cap = function(val){
return val.charAt(0).toUpperCase() + val.slice(1);
};
var name = yargs.name;
var destPath = path.join(resolveToServices());
return gulp.src(paths.blankService)
.pipe(template({
name: name,
upCaseName: cap(name)
}))
.pipe(rename(function(path){
path.basename = path.basename.replace('temp', name);
}))
.pipe(gulp.dest(destPath));
});
//Create cascade style mongoose models
//
gulp.task('model', function(){
var cap = function(val){
return val.charAt(0).toUpperCase() + val.slice(1);
};
var name = yargs.name;
var parent = yargs.parent || '';
var child = yargs.child || '';
var destPath = path.join(resolveToModels());
var pOpen = `/*`;
var pClose = `*/`;
var cOpen = `/*`;
var cClose = `*/`;
if (parent) {
pOpen = '';
pClose = '';
}
if (child) {
cOpen = '';
cClose ='';
}
return gulp.src(paths.blankModel)
.pipe(template({
name: name,
upCaseName: cap(name),
names: pluralize(name),
parent: parent,
upCaseParent: cap(parent),
child: child,
upCaseChild: cap(child),
children: pluralize(child),
pOpen: pOpen,
pClose: pClose,
cOpen: cOpen,
cClose: cClose
}))
.pipe(rename(function(path){
path.basename = path.basename.replace('temp', name);
}))
.pipe(gulp.dest(destPath));
});
////////////////////////////////////////////////////////////
// Semantic Ui Tasks //
////////////////////////////////////////////////////////////
gulp.task('watch', 'Watch for site/theme changes', watch);
gulp.task('build-semantic', 'Builds all files from source', build);
gulp.task('build-javascript', 'Builds all javascript from source', buildJS);
gulp.task('build-css', 'Builds all css from source', buildCSS);
gulp.task('build-assets', 'Copies all assets from source', buildAssets);
gulp.task('clean', 'Clean dist folder', clean);
///////////////////////////////////////////////////////////
// Default //
//////////////////////////////////////////////////////////
gulp.task('default', ['mongo', 'jshint', 'serve']);