-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgulpfile.js
More file actions
191 lines (169 loc) · 5.05 KB
/
gulpfile.js
File metadata and controls
191 lines (169 loc) · 5.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
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
'use strict';
/**
* Gulp file for compiling library.
*
* - gulp build
* Builds the library under the configured dist folder
* - gulp watch
* Watches for changes in the sources and triggers a build
* - gulp clean
* Cleans all auto generated files
*/
/*
* Gulp file configuration
*/
var
config = {
'dist': 'dist',
'src': [
'src/index.js',
'src/**/*.js'
],
'docConfig': {
'src': 'src',
'dist': 'dist/doc'
},
'moduleLicenseDir': 'MODULE_LICENSES',
'moduleConfig': {
'name' : 'ngperms',
'deps': [
'angular'
],
'args': [
'angular'
],
'exports': 'angular.module("ngPerms")',
'type': 'umd'
}
};
/*
* Gulp module imports
*/
var
// Gulp modules
gulp = require('gulp'),
gulpPlumber = require('gulp-plumber'),
gulpUtil = require('gulp-util'),
gulpUglify = require('gulp-uglify'),
gulpSequence = require('gulp-sequence'),
gulpClean = require('gulp-clean'),
gulpConcat = require('gulp-concat'),
gulpWatch = require('gulp-watch'),
gulpConvertNewline = require('gulp-convert-newline'),
gulpStripDebug = require('gulp-strip-debug'),
gulpNgAnnotate = require('gulp-ng-annotate'),
gulpModuleWrapper = require('gulp-module-wrapper'),
// Other modules
url = require('url'),
path = require('path'),
fs = require('fs'),
childProcess = require('child_process'),
licenseChecker = require('license-checker');
/*
* Environment variables
*/
var
production = !!gulpUtil.env.production;
/**
* Watches for changes and builds the library for distribution
*/
gulp.task('watch', function () {
gulp.start('build');
gulpWatch([config.src], function () {
gulp.start('build');
});
});
/**
* Builds the library for distribution
*/
gulp.task('build', function (cb) {
return gulpSequence(['module-licenses', 'scripts-js', 'docs'], cb);
});
/**
* Builds the javascript sources
*/
gulp.task('scripts-js', function () {
return gulp.src(config.src)
.pipe(gulpPlumber())
// ng annotate
.pipe(gulpNgAnnotate({ 'add': true, 'remove': false, 'single_quotes': true }))
// remove logging
.pipe(production ? gulpStripDebug() : gulpUtil.noop())
// merge to one file
.pipe(gulpConcat(production ? config.moduleConfig.name + '.min.js' : config.moduleConfig.name + '.js'))
// wrap in node module
.pipe(gulpModuleWrapper(config.moduleConfig))
// minify js
.pipe(production ? gulpUglify({ 'mangle': false }) : gulpUtil.noop())
// write compiled js
.pipe(gulp.dest(config.dist));
});
/**
* Builds the documentation
*/
gulp.task('docs', function (cb) {
childProcess.exec(
'node_modules/jsdoc/jsdoc.js '+
'--configure node_modules/angular-jsdoc/common/conf.json '+ // config file
'--template node_modules/angular-jsdoc/default '+ // template file
'--destination "' + config.docConfig.dist + '" '+ // output directory
'--readme ./README.md ' + // to include README.md as index contents
'--recurse "' + config.docConfig.src + '" ', // source code directory
function (err) {
cb(err);
}
);
});
/**
* Cleans dist files
*/
gulp.task('clean', function (cb) {
return gulpSequence(['clean-dist'], cb);
});
/**
* Builds the license file for 3rd party node modules
*/
gulp.task('module-licenses', function (cb) {
var
// collect license files in an array
licenseFiles = [];
// run license crawler to find all licenses
licenseChecker.init({
'production': true, // only use production environment, we do not need licencses of dev dependencies
'start': '.', // use current folder
'relativeLicensePath': '.'
}, function (err, json) {
if (err) {
console.error(err);
return;
}
// iterate over the returned license dictionary
for (var key in json)
{
// key = name of dependency
if (json.hasOwnProperty(key)) {
// see if license file is set
if (json[key].licenseFile !== undefined) {
licenseFiles.push(json[key].licenseFile)
} else {
// print that we did not find a license file
console.warn('dependency "' + key + '" does not have a license file.');
}
}
}
// copy all license files from node_modules
gulp.src(licenseFiles, {'base': 'node_modules'})
.pipe(gulpPlumber())
.pipe(gulpConvertNewline())
.pipe(gulp.dest(path.join(config.dist, config.moduleLicenseDir)))
.on('end', cb);
});
});
/**
* Cleans the dist folder
*/
gulp.task('clean-dist', function () {
return gulp.src(path.join(config.dist, '*'), { 'read': false })
.pipe(gulpPlumber())
.pipe(gulpClean({ 'force': true }));
});