-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
355 lines (287 loc) · 11.2 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* @author [Tristan Valcke]{@link https://github.com/Itee}
* @license [BSD-3-Clause]{@link https://opensource.org/licenses/BSD-3-Clause}
*
* @module Building
*
* @description The gulp tasks file. It allow to run some tasks from command line interface.<br>
* The available tasks are:
* <ul>
* <li>help</li>
* <li>clean</li>
* <li>lint</li>
* <li>doc</li>
* <li>unit</li>
* <li>bench</li>
* <li>test</li>
* <li>build-test</li>
* <li>build</li>
* <li>release</li>
* </ul>
* You could find a complet explanation about these tasks using: <b>npm run help</b>.
*
* @requires {@link module: [gulp]{@link https://github.com/gulpjs/gulp}}
* @requires {@link module: [gulp-jsdoc3]{@link https://github.com/mlucool/gulp-jsdoc3}}
* @requires {@link module: [gulp-eslint]{@link https://github.com/adametry/gulp-eslint}}
* @requires {@link module: [del]{@link https://github.com/sindresorhus/del}}
* @requires {@link module: [minimist]{@link https://github.com/substack/minimist}}
* @requires {@link module: [rollup]{@link https://github.com/rollup/rollup}}
* @requires {@link module: [path]{@link https://nodejs.org/api/path.html}}
* @requires {@link module: [karma]{@link https://github.com/karma-runner/karma}}
* @requires {@link module: [fancy-log]{@link https://github.com/js-cli/fancy-log}}
* @requires {@link module: [ansi-colors]{@link https://github.com/doowb/ansi-colors}}
*
*
*/
/* eslint-env node */
const packageInfos = require( './package.json' )
const gulp = require( 'gulp' )
const jsdoc = require( 'gulp-jsdoc3' )
const eslint = require( 'gulp-eslint' )
const del = require( 'del' )
const parseArgs = require( 'minimist' )
const rollup = require( 'rollup' )
const path = require( 'path' )
const karma = require( 'karma' )
const log = require( 'fancy-log' )
const colors = require( 'ansi-colors' )
const red = colors.red
const green = colors.green
const blue = colors.blue
const cyan = colors.cyan
const yellow = colors.yellow
const magenta = colors.magenta
/**
* @method npm run help ( default )
* @global
* @description Will display the help in console
*/
gulp.task( 'help', ( done ) => {
log( '' )
log( '====================================================' )
log( '| HELP |' )
log( '| Itee Server |' )
log( `| v${packageInfos.version} |` )
log( '====================================================' )
log( '' )
log( 'Available commands are:' )
log( '\t', blue( 'npm run' ), cyan( 'help' ), ' - Display this help.' )
log( '\t', blue( 'npm run' ), cyan( 'patch' ), ' - Will apply some patch/replacements in dependencies.', red( '(Apply only once after run "npm install")' ) )
log( '\t', blue( 'npm run' ), cyan( 'clean' ), ' - Will delete builds and temporary folders.' )
log( '\t', blue( 'npm run' ), cyan( 'lint' ), ' - Will run the eslint in pedantic mode with auto fix when possible.' )
log( '\t', blue( 'npm run' ), cyan( 'doc' ), ' - Will run jsdoc, and create documentation under `documentation` folder, using the docdash theme' )
log( '\t', blue( 'npm run' ), cyan( 'test' ), ' - Will run the test framworks (unit and bench), and create reports under `documentation/report` folder, using the mochawesome theme' )
log( '\t', blue( 'npm run' ), cyan( 'unit' ), ' - Will run the karma server for unit tests.' )
log( '\t', blue( 'npm run' ), cyan( 'bench' ), ' - Will run the karma server for benchmarks.' )
log( '\t', blue( 'npm run' ), cyan( 'build' ), yellow( '--' ), green( '<options>' ), ' - Will build the application for development and/or production environments.', yellow( 'Note: The two dash are only required if you provide options !' ) )
log( '\t\t The available', green( '<options>' ), 'are:' )
log( '\t\t\t', green( '-n' ), 'or', green( '--name' ), ' - The export name of the builded application', red( '(required for UMD module)' ), cyan( '[Default: ""]' ), '.' )
log( '\t\t\t', green( '-i' ), 'or', green( '--input' ), ' - The main file path to build', cyan( '[Default: "sources/main.js"]' ), '.' )
log( '\t\t\t', green( '-o' ), 'or', green( '--output' ), ' - The folder where output the build', cyan( '[Default: "builds"]' ), '.' )
log( '\t\t\t', green( '-f:' ), magenta( '<format>' ), 'or', green( '--format:' ), magenta( '<format>' ), ' - to specify the output build type. Where format could be any of:', magenta( 'amd' ), magenta( 'cjs' ), magenta( 'es' ), magenta( 'iife' ), magenta( 'umd' ), cyan( '[Default: "amd,cjs,es,iife,umd"]' ), '.' )
log( '\t\t\t', green( '-e:' ), magenta( '<env>' ), 'or', green( '--env:' ), magenta( '<env>' ), ' - to specify the build environment. Where env could be any of:', magenta( 'dev' ), magenta( 'prod' ), cyan( '[Default: "dev"]' ), '.' )
log( '\t\t\t', green( '-s' ), 'or', green( '--sourcemap' ), ' - to build with related source map', cyan( '[Default: true]' ), '.' )
log( '\t\t\t', green( '-t' ), 'or', green( '--treeshake' ), ' - allow to perform treeshaking when building', cyan( '[Default: true]' ), '.' )
log( '\t', blue( 'npm run' ), cyan( 'release' ), ' - Will run all the lint, test stuff, and if succeed will build the application.' )
log( '' )
log( 'In case you have', blue( 'gulp' ), 'installed globally, you could use also:' )
log( '\t', blue( 'gulp' ), cyan( 'command' ), ' - It will perform the command like using "npm run" but with less characters to type... Because you\'re a developer, right ?' )
log( '' )
done()
} )
/**
* @method npm run patch
* @global
* @description Will apply some patch/replacements in dependencies
*/
gulp.task( 'patch', ( done ) => {
done()
} )
/**
* @method npm run clean
* @global
* @description Will delete builds and temporary folders
*/
gulp.task( 'clean', () => {
const filesToClean = [
'./builds',
'./tests/builds',
'./docs'
]
return del( filesToClean )
} )
/**
* @method npm run lint
* @global
* @description Will lint the sources files and try to fix the style when possible
*/
gulp.task( 'lint', () => {
const filesToLint = [
'gulpfile.js',
'configs/**/*.js',
'sources/**/*.js',
'tests/**/*.js',
'!tests/builds/*.js'
]
return gulp.src( filesToLint, { base: './' } )
.pipe( eslint( {
allowInlineConfig: true,
globals: [],
fix: true,
quiet: false,
envs: [],
configFile: './configs/eslint.conf.js',
parserOptions: {},
plugins: [],
rules: {},
useEslintrc: false
} ) )
.pipe( eslint.format( 'stylish' ) )
.pipe( gulp.dest( '.' ) )
.pipe( eslint.failAfterError() )
} )
/**
* @method npm run doc
* @global
* @description Will generate this documentation
*/
gulp.task( 'doc', ( done ) => {
const config = require( './configs/jsdoc.conf' )
const filesToDoc = [
'README.md',
'gulpfile.js',
'./configs/*.js',
'./sources/**/*.js',
'./tests/**/*.js'
]
gulp.src( filesToDoc, { read: false } )
.pipe( jsdoc( config, done ) )
} )
/**
* @method npm run unit
* @global
* @description Will run unit tests using karma
*/
gulp.task( 'unit', ( done ) => {
const karmaServer = new karma.Server( {
configFile: `${__dirname}/configs/karma.units.conf.js`,
singleRun: true
}, ( exitCode ) => {
if ( exitCode !== 0 ) {
done( `Karma server exit with code ${exitCode}` )
} else {
log( `Karma server exit with code ${exitCode}` )
done()
}
} )
karmaServer.on( 'browser_error', ( browser, error ) => {
log( red( error.message ) )
} )
karmaServer.start()
} )
/**
* @method npm run bench
* @global
* @description Will run benchmarks using karma
*/
gulp.task( 'bench', ( done ) => {
const karmaServer = new karma.Server( {
configFile: `${__dirname}/configs/karma.benchs.conf.js`,
singleRun: true
}, ( exitCode ) => {
if ( exitCode !== 0 ) {
done( `Karma server exit with code ${exitCode}` )
} else {
log( `Karma server exit with code ${exitCode}` )
done()
}
} )
karmaServer.on( 'browser_error', ( browser, error ) => {
log( red( error.message ) )
} )
karmaServer.start()
} )
/**
* @method npm run test
* @global
* @description Will run unit tests and benchmarks using karma
*/
gulp.task( 'test', gulp.series( 'unit', 'bench' ) )
/**
* @method npm run build-test
* @global
* @description Will build itee client tests.
*/
gulp.task( 'build-test', ( done ) => {
const configs = require( './configs/rollup.test.conf' )()
nextBuild()
function nextBuild ( error ) {
'use strict'
if ( error ) {
done( error )
} else if ( configs.length === 0 ) {
done()
} else {
const config = configs.pop()
log( `Building ${config.output.file}` )
rollup.rollup( config )
.then( ( bundle ) => { return bundle.write( config.output ) } )
.then( () => { nextBuild() } )
.catch( nextBuild )
}
}
} )
/**
* @method npm run build
* @global
* @description Will build itee client module using optional arguments. See help to further informations.
*/
gulp.task( 'build', ( done ) => {
const options = parseArgs( process.argv, {
string: [ 'n', 'i', 'f', 'e' ],
boolean: [ 's', 't' ],
default: {
n: 'Itee.Server',
i: path.join( __dirname, 'sources', `${packageInfos.name}.js` ),
o: path.join( __dirname, 'builds' ),
f: 'esm,cjs',
e: 'dev,prod',
s: true,
t: true
},
alias: {
n: 'name',
i: 'input',
o: 'output',
f: 'format',
e: 'env',
s: 'sourcemap',
t: 'treeshake'
}
} )
const configs = require( './configs/rollup.conf' )( options )
nextBuild()
function nextBuild ( error ) {
'use strict'
if ( error ) {
done( error )
} else if ( configs.length === 0 ) {
done()
} else {
const config = configs.pop()
log( `Building ${config.output.file}` )
rollup.rollup( config )
.then( ( bundle ) => { return bundle.write( config.output ) } )
.then( () => { nextBuild() } )
.catch( nextBuild )
}
}
} )
/**
* @method npm run release
* @global
* @description Will perform a complet release of the library including 'clean', 'lint', 'doc', 'build-test', 'test' and finally 'build'.
*/
gulp.task( 'release', gulp.series( 'clean', 'lint', 'doc', 'build-test', 'test', 'build' ) )
//---------
gulp.task( 'default', gulp.series( 'help' ) )