forked from mohsen1/angular-json-schema-form
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
190 lines (171 loc) · 4.78 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
'use strict';
/* jshint node:true */
/* jshint browser:true */
var fs = require('fs');
var del = require('del');
var gulp = require('gulp');
var connect = require('gulp-connect');
var concat = require('gulp-concat');
var jshint = require('gulp-jshint');
var header = require('gulp-header');
var rename = require('gulp-rename');
var uglify = require('gulp-uglify');
var minifyHtml = require('gulp-minify-html');
var minifyCSS = require('gulp-minify-css');
var templateCache = require('gulp-angular-templatecache');
var plumber = require('gulp-plumber');
var openBrowser = require('gulp-open');
var less = require('gulp-less');
var order = require('gulp-order');
var jscs = require('gulp-jscs');
var runSequence = require('run-sequence');
var es = require('event-stream');
var karma = require('karma').server;
/*
* Configuration for distribution files including header banner and npm package
* information
*/
var config = {
pkg : JSON.parse(fs.readFileSync('./package.json')),
banner:
'/*!\n' +
' * <%= pkg.name %>\n' +
' * <%= pkg.homepage %>\n' +
' * Version: <%= pkg.version %> - <%= timestamp %>\n' +
' * License: <%= pkg.license %>\n' +
' */\n\n\n'
};
/*
* Connect server task
*/
gulp.task('connect', function() {
return connect.server({
// root: '.',
livereload: true
});
});
/*
* `html` task triggers connect reload on html file changes.
* This task does NOT compile html files to JavaScipt
*/
gulp.task('html', function() {
return gulp.src(['./demo/*.html', '.src/*.html'])
.pipe(connect.reload());
});
/*
* Watch each file category for changes and trigger tasks accordingly
*/
gulp.task('watch', function() {
gulp.watch(['./demo/**/*.html'], ['build']);
gulp.watch(['./src/*.less'], ['build']);
gulp.watch(['./src/*.js', './**/*.html'], ['build']);
});
/*
* CLean up `dist` folder before copying a new set of files to it
*/
gulp.task('clean', function(cb) {
del(['./dist'], cb);
});
/*
* Compiles scripts from across the project into distribution JavaScript files
*/
gulp.task('scripts', ['clean'], function() {
// Compile templates into AngularJS modules
function buildTemplates() {
return gulp.src('src/**/*.html')
.pipe(minifyHtml({
empty: true,
spare: true,
quotes: true
}))
.pipe(templateCache({module: 'mohsen1.schema-form'}));
}
// Test source JavaScript files with JSCS and JSHint
function buildDistJS() {
return gulp.src('src/schema-form.js')
.pipe(plumber({errorHandler: handleError}))
.pipe(jshint())
.pipe(jscs())
.pipe(jshint.reporter('jshint-stylish'))
.pipe(jshint.reporter('fail'));
}
// Compile all JavaScript files into one file for distribution
return es.merge(buildDistJS(), buildTemplates())
.pipe(plumber({errorHandler: handleError}))
.pipe(order([
'schema-form.js',
'template.js'
]))
.pipe(concat('schema-form.js'))
.pipe(header(config.banner, {
timestamp: (new Date()).toISOString(), pkg: config.pkg
}))
.pipe(gulp.dest('dist'))
.pipe(rename({suffix: '.min.js'}))
.pipe(uglify({preserveComments: 'some'}))
.pipe(gulp.dest('./dist'))
.pipe(connect.reload());
});
/*
* Compile and minify less files
*/
gulp.task('styles', ['clean'], function() {
return gulp.src('src/schema-form.less')
.pipe(less())
.pipe(header(config.banner, {
timestamp: (new Date()).toISOString(), pkg: config.pkg
}))
.pipe(gulp.dest('dist'))
.pipe(minifyCSS())
.pipe(rename({suffix: '.min.css'}))
.pipe(gulp.dest('dist'))
.pipe(connect.reload());
});
/*
* Open browser after connect server started
*/
gulp.task('open', function() {
return gulp.src('./demo/demo.html')
.pipe(openBrowser('', {url: 'http://localhost:8080/demo/demo.html'}));
});
/*
* Enforce JSCS and JSHint to test files
*/
gulp.task('jshint-test', function() {
return gulp.src('./test/**/*.js')
.pipe(jshint())
.pipe(jscs());
});
/*
* Run unit tests
*/
gulp.task('karma', function(done) {
karma.start({
configFile: __dirname + '/karma.conf.js',
singleRun: true
}, done);
});
/*
* Run unit test and keep tests open
*/
gulp.task('karma-serve', function(done) {
karma.start({
configFile: __dirname + '/karma.conf.js'
}, done);
});
/*
* Error handler
*/
function handleError(err) {
/*jshint validthis:true */
console.log(err);
this.emit('end');
}
// Exposed tasks
gulp.task('build', ['scripts', 'styles']);
gulp.task('serve', function(cb) {
runSequence('build', 'connect', 'watch', 'open', cb);
});
gulp.task('default', ['build', 'test']);
gulp.task('test', ['build', 'jshint-test', 'karma']);
gulp.task('serve-test', ['build', 'watch', 'jshint-test', 'karma-serve']);