Test Setup Failed
Push — master ( 90fdbc...f9629d )
by Jonathan
05:41 queued 02:26
created

Gulpfile.js   A

Complexity

Total Complexity 16
Complexity/F 1

Size

Lines of Code 346
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 0
c 0
b 0
f 0
nc 1
dl 0
loc 346
rs 10
wmc 16
mnd 0
bc 4
fnc 16
bpm 0.25
cpm 1
noi 2
1
// Require our dependencies
2
const autoprefixer = require( 'autoprefixer' );
3
const babel = require( 'gulp-babel' );
4
const browserSync = require( 'browser-sync' );
5
const cheerio = require( 'gulp-cheerio' );
6
const concat = require( 'gulp-concat' );
7
const csslint = require('gulp-csslint' );
8
const cssnano = require( 'gulp-cssnano' );
9
const del = require( 'del' );
10
const eslint = require( 'gulp-eslint' );
11
const gulp = require( 'gulp' );
12
const gutil = require( 'gulp-util' );
13
const globbing = require( 'gulp-css-globbing' );
14
const imagemin = require( 'gulp-imagemin' );
15
const mqpacker = require( 'css-mqpacker' );
16
const notify = require( 'gulp-notify' );
17
const plumber = require( 'gulp-plumber' );
18
const postcss = require( 'gulp-postcss' );
19
const reload = browserSync.reload;
20
const rename = require( 'gulp-rename' );
21
const sass = require( 'gulp-sass' );
22
const sassLint = require( 'gulp-sass-lint' );
23
const sort = require( 'gulp-sort' );
24
const sourcemaps = require( 'gulp-sourcemaps' );
25
const svgmin = require( 'gulp-svgmin' );
26
const svgstore = require( 'gulp-svgstore' );
27
const uglify = require( 'gulp-uglify' );
28
const wpPot = require( 'gulp-wp-pot' );
29
30
// Set assets paths.
31
const paths = {
32
	'css': [ 'assets/css/*.css', '!*.min.css' ],
33
	'icons': 'assets/img/svg-icons/*.svg',
34
	'images': [ 'assets/img/*', '!assets/img/*.svg', 'docs/assets/img/**/*' ],
35
	'php': [ './*.php', './**/*.php' ],
36
	'sass': 'assets/sass/**/*.scss',
37
	'concat_scripts': 'assets/js/src/*.js',
38
	'scripts': [ 'assets/js/*.js', '!assets/js/*.min.js' ],
39
	'changelog': 'changelog.md'
40
};
41
42
/**
43
 * Handle errors and alert the user.
44
 */
45
function handleErrors () {
46
	const args = Array.prototype.slice.call( arguments );
47
48
	notify.onError( {
49
		'title': 'Task Failed [<%= error.message %>',
50
		'message': 'See console.',
51
		'sound': 'Sosumi' // See: https://github.com/mikaelbr/node-notifier#all-notification-options-with-their-defaults
52
	} ).apply( this, args );
53
54
	gutil.beep(); // Beep 'sosumi' again.
55
56
	// Prevent the 'watch' task from stopping.
57
	this.emit( 'end' );
58
}
59
60
/**
61
 * Delete object-sync-for-salesforce-admin.css and object-sync-for-salesforce-admin.min.css before we minify and optimize
62
 */
63
gulp.task( 'clean:styles', () =>
64
	del( [ 'assets/css/object-sync-for-salesforce-admin.css', 'assets/css/object-sync-for-salesforce-admin.min.css' ] )
65
);
66
67
/**
68
 * Compile Sass and run stylesheet through PostCSS.
69
 *
70
 * https://www.npmjs.com/package/gulp-sass
71
 * https://www.npmjs.com/package/gulp-postcss
72
 * https://www.npmjs.com/package/gulp-autoprefixer
73
 * https://www.npmjs.com/package/css-mqpacker
74
 */
75
gulp.task( 'postcss', [ 'clean:styles' ], () =>
76
	gulp.src( 'assets/sass/*.scss', paths.css )
77
78
		// Deal with errors.
79
		.pipe( plumber( {'errorHandler': handleErrors} ) )
80
81
		// Wrap tasks in a sourcemap.
82
		.pipe( sourcemaps.init() )
83
84
			// glob files together
85
			.pipe(globbing({
86
		        // Configure it to use SCSS files
87
		        extensions: ['.scss']
88
		    }))
89
90
			// Compile Sass using LibSass.
91
			.pipe( sass( {
92
				'errLogToConsole': true,
93
				'outputStyle': 'expanded' // Options: nested, expanded, compact, compressed
94
			} ) )
95
96
			// Parse with PostCSS plugins.
97
			.pipe( postcss( [
98
				autoprefixer( {
99
					'browsers': [ 'last 2 version' ]
100
				} ),
101
				mqpacker( {
102
					'sort': true
103
				} )
104
			] ) )
105
106
		// Create sourcemap.
107
		.pipe( sourcemaps.write() )
108
109
		// Create object-sync-for-salesforce-admin.css.
110
		.pipe( gulp.dest( 'assets/css/' ) )
111
		.pipe( browserSync.stream() )
112
);
113
114
/**
115
 * Minify and optimize object-sync-for-salesforce-admin.css.
116
 *
117
 * https://www.npmjs.com/package/gulp-cssnano
118
 */
119
gulp.task( 'cssnano', [ 'postcss' ], () =>
120
	gulp.src( 'assets/css/object-sync-for-salesforce-admin.css' )
121
		.pipe( plumber( {'errorHandler': handleErrors} ) )
122
		.pipe( cssnano( {
123
			'safe': true // Use safe optimizations.
124
		} ) )
125
		.pipe( rename( 'object-sync-for-salesforce-admin.min.css' ) )
126
		.pipe( gulp.dest( 'assets/css' ) )
127
		.pipe( browserSync.stream() )
128
);
129
130
/**
131
 * Delete the svg-icons.svg before we minify, concat.
132
 */
133
gulp.task( 'clean:icons', () =>
134
	del( [ 'assets/img/svg-icons.svg' ] )
135
);
136
137
/**
138
 * Minify, concatenate, and clean SVG icons.
139
 *
140
 * https://www.npmjs.com/package/gulp-svgmin
141
 * https://www.npmjs.com/package/gulp-svgstore
142
 * https://www.npmjs.com/package/gulp-cheerio
143
 */
144
gulp.task( 'svg', [ 'clean:icons' ], () =>
145
	gulp.src( paths.icons )
146
147
		// Deal with errors.
148
		.pipe( plumber( {'errorHandler': handleErrors} ) )
149
150
		// Minify SVGs.
151
		.pipe( svgmin() )
152
153
		// Add a prefix to SVG IDs.
154
		.pipe( rename( {'prefix': 'icon-'} ) )
155
156
		// Combine all SVGs into a single <symbol>
157
		.pipe( svgstore( {'inlineSvg': true} ) )
158
159
		// Clean up the <symbol> by removing the following cruft...
160
		.pipe( cheerio( {
161
			'run': function ( $, file ) {
162
				$( 'svg' ).attr( 'style', 'display:none' );
163
				$( '[fill]' ).removeAttr( 'fill' );
164
				$( 'path' ).removeAttr( 'class' );
165
			},
166
			'parserOptions': {'xmlMode': true}
167
		} ) )
168
169
		// Save svg-icons.svg.
170
		.pipe( gulp.dest( 'assets/img/' ) )
171
		.pipe( browserSync.stream() )
172
);
173
174
/**
175
 * Optimize images.
176
 *
177
 * https://www.npmjs.com/package/gulp-imagemin
178
 */
179
gulp.task( 'imagemin', () =>
180
	gulp.src( paths.images, {base: "./"} )
181
		.pipe( plumber( {'errorHandler': handleErrors} ) )
182
		.pipe( imagemin( {
183
			'optimizationLevel': 5,
184
			'progressive': true,
185
			'interlaced': true
186
		} ) )
187
		.pipe( gulp.dest("./") )
188
);
189
190
/**
191
 * Concatenate and transform JavaScript.
192
 *
193
 * https://www.npmjs.com/package/gulp-concat
194
 * https://github.com/babel/gulp-babel
195
 * https://www.npmjs.com/package/gulp-sourcemaps
196
 */
197
gulp.task( 'concat', () =>
198
	gulp.src( paths.concat_scripts )
199
200
		// Deal with errors.
201
		.pipe( plumber(
202
			{'errorHandler': handleErrors}
203
		) )
204
205
		// Start a sourcemap.
206
		.pipe( sourcemaps.init() )
207
208
		// Convert ES6+ to ES2015.
209
		.pipe( babel( {
210
			presets: [ 'es2015' ]
211
		} ) )
212
213
		// Concatenate partials into a single script.
214
		.pipe( concat( 'object-sync-for-salesforce-admin.js' ) )
215
216
		// Append the sourcemap to object-sync-for-salesforce-admin.js.
217
		.pipe( sourcemaps.write() )
218
219
		// Save object-sync-for-salesforce-admin.js
220
		.pipe( gulp.dest( 'assets/js' ) )
221
		.pipe( browserSync.stream() )
222
);
223
224
/**
225
  * Minify compiled JavaScript.
226
  *
227
  * https://www.npmjs.com/package/gulp-uglify
228
  */
229
gulp.task( 'uglify', [ 'concat' ], () =>
230
	gulp.src( paths.scripts )
231
		.pipe( rename( {'suffix': '.min'} ) )
232
		.pipe( uglify( {
233
			'mangle': false
234
		} ) )
235
		.pipe( gulp.dest( 'assets/js' ) )
236
);
237
238
/**
239
 * Delete the plugin's .pot before we create a new one.
240
 */
241
gulp.task( 'clean:pot', () =>
242
	del( [ 'languages/object-sync-for-salesforce.pot' ] )
243
);
244
245
/**
246
 * Scan the plugin and create a POT file.
247
 *
248
 * https://www.npmjs.com/package/gulp-wp-pot
249
 */
250
gulp.task( 'wp-pot', [ 'clean:pot' ], () =>
251
	gulp.src( paths.php )
252
		.pipe( plumber( {'errorHandler': handleErrors} ) )
253
		.pipe( sort() )
254
		.pipe( wpPot( {
255
			'domain': 'object-sync-for-salesforce',
256
			'package': 'object-sync-for-salesforce',
257
		} ) )
258
		.pipe( gulp.dest( 'languages/object-sync-for-salesforce.pot' ) )
259
);
260
261
/**
262
 * Delete the plugin's changelog.txt before making another one
263
 */
264
gulp.task( 'clean:changelog', () =>
265
	del( [ 'changelog.txt' ] )
266
);
267
268
/**
269
 * Create a changelog.txt from the changelog.md
270
 */
271
gulp.task( 'wp-changelog', [ 'clean:changelog' ], () =>
272
    gulp.src( paths.changelog )
273
        .pipe( rename( 'changelog.txt') )
274
      	.pipe( gulp.dest( '.' ) )
275
);
276
277
/**
278
 * Sass linting.
279
 *
280
 * https://www.npmjs.com/package/sass-lint
281
 */
282
gulp.task( 'sass:lint', () =>
283
	gulp.src( [
284
		'assets/sass/**/*.scss',
285
		'!node_modules/**'
286
	] )
287
		.pipe( sassLint() )
288
		.pipe( sassLint.format() )
289
		.pipe( sassLint.failOnError() )
290
);
291
292
/**
293
 * CSS linting.
294
 *
295
 * https://www.npmjs.com/package/gulp-csslint
296
 */
297
gulp.task( 'css:lint', () =>
298
	gulp.src( [
299
		'assets/css/**/*.css',
300
		'!assets/css/**/*.min.css'
301
	] )
302
		.pipe( csslint() )
303
    	.pipe( csslint.formatter() )
304
);
305
306
/**
307
 * JavaScript linting.
308
 *
309
 * https://www.npmjs.com/package/gulp-eslint
310
 */
311
gulp.task('js:lint', () => {
312
    // ESLint ignores files with "node_modules" paths.
313
    // So, it's best to have gulp ignore the directory as well.
314
    // Also, Be sure to return the stream from the task;
315
    // Otherwise, the task may end before the stream has finished.
316
    return gulp.src(['**/*.js','!node_modules/**', '!**.min.js'])
317
        // eslint() attaches the lint output to the "eslint" property
318
        // of the file object so it can be used by other modules.
319
        .pipe(eslint({
320
        	fix: true
321
        }))
322
        // eslint.format() outputs the lint results to the console.
323
        // Alternatively use eslint.formatEach() (see Docs).
324
        .pipe(eslint.format())
325
        // To have the process exit with an error code (1) on
326
        // lint error, return the stream and pipe to failAfterError last.
327
        .pipe(eslint.failAfterError());
328
});
329
330
/**
331
 * Process tasks and reload browsers on file changes.
332
 *
333
 * https://www.npmjs.com/package/browser-sync
334
 */
335
gulp.task( 'watch', function () {
336
337
	// Kick off BrowserSync.
338
	browserSync( {
339
		'open': false,             // Open project in a new tab?
340
		'injectChanges': true,     // Auto inject changes instead of full reload.
341
		'proxy': 'testing.dev',    // Use http://largo.com:3000 to use BrowserSync.
342
		'watchOptions': {
343
			'debounceDelay': 1000  // Wait 1 second before injecting.
344
		}
345
	} );
346
347
	// Run tasks when files change.
348
	gulp.watch( paths.icons, [ 'icons' ] );
349
	gulp.watch( paths.sass, [ 'styles' ] );
350
	gulp.watch( paths.scripts, [ 'scripts' ] );
351
	gulp.watch( paths.concat_scripts, [ 'scripts' ] );
352
	gulp.watch( paths.php, [ 'markup' ] );
353
} );
354
355
/**
356
 * Create individual tasks.
357
 */
358
gulp.task( 'markup', browserSync.reload );
359
gulp.task( 'i18n', [ 'wp-pot' ] );
360
gulp.task( 'changelog', [ 'wp-changelog' ] );
361
gulp.task( 'icons', [ 'svg' ] );
362
gulp.task( 'scripts', [ 'uglify' ] );
363
gulp.task( 'styles', [ 'cssnano' ] );
364
gulp.task( 'lint', [ 'sass:lint', 'js:lint' ] );
365
gulp.task( 'default', [ 'i18n', 'changelog', 'icons', 'styles', 'scripts', 'imagemin'] );
366