-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGruntfile.js
More file actions
384 lines (367 loc) · 9.89 KB
/
Gruntfile.js
File metadata and controls
384 lines (367 loc) · 9.89 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
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
module.exports = function( grunt ) {
const sass = require( 'sass' );
const pkg = grunt.file.readJSON( 'package.json' );
const settings = grunt.file.readJSON( 'grunt-options.json' );
const branch = require( 'child_process' )
.execSync( 'git branch --show-current', { encoding: 'utf8' } )
.trim()
.split( '/' )
.pop();
// calculate which files to copy in
let copyFiles = [
// common
{
expand: true,
cwd: 'node_modules/squarecandy-common/common',
src: '**/*',
dest: '',
dot: true,
rename( dest, matchedSrcPath ) {
// the exact file name .gitignore is reserved by npm
// so we track it as /common/gitignore (no dot) and rename on copy
if ( matchedSrcPath === 'gitignore' ) {
return dest + '.gitignore';
}
// default for all other files
return dest + matchedSrcPath;
},
// We are removing .eslintignore & .eslintrc, copy over these files if they exist
filter: function( filepath ) {
if ( filepath.includes( '.eslintignore' ) || filepath.includes( '.eslintrc' ) ) {
// get the dest path of the file
const basePath = filepath.replace( this.cwd + '/', '' );
// check whether the file exists, if it does, copy over it, but if not, don't re-add it
const fileExists = grunt.file.exists( basePath );
if ( fileExists ) {
grunt.log.writeln( basePath + ' should be deleted.' );
}
return fileExists;
} else if ( filepath.includes( 'grunt-options.json' ) ) {
// get the dest path of the file
const basePath = filepath.replace( this.cwd + '/', '' );
// check whether the file exists, if it does, don't copy over it, if it doesn't, add it
const fileExists = grunt.file.exists( basePath );
return ! fileExists;
} else if ( filepath.includes( 'Gruntfile.js' ) ) {
// unless the repo has a custom gruntfile, copy ours over
return ! settings.customGruntfile;
} else if ( filepath.includes( 'package.json' ) ) {
return ! settings.customPackageJson;
} else if ( filepath.includes( 'composer.json' ) ) {
return ! settings.customComposerJson;
} else {
return true;
}
},
},
];
// add theme or plugin specific files
if ( settings.copyType == 'plugin' || settings.copyType == 'theme' ) {
const themeOrPluginFiles = [
{
expand: true,
cwd: 'node_modules/squarecandy-common/' + settings.copyType,
src: '**/*',
dest: '',
dot: true,
},
];
copyFiles = copyFiles.concat( themeOrPluginFiles );
}
// optionally add cycle2
if ( settings.copyCycle2 ) {
const cycle2Files = [
{
"expand": true,
"cwd": "node_modules/jquery-cycle2/build",
"src": "jquery.cycle2.min.js.map",
"dest": "dist/js/vendor",
},
{
"expand": true,
"cwd": "node_modules/jquery-cycle2/build",
"src": "jquery.cycle2.min.js",
"dest": "dist/js/vendor",
},
{
"expand": true,
"cwd": "node_modules/jquery-cycle2/build/plugin",
"src": "jquery.cycle2.swipe.min.js",
"dest": "dist/js/vendor",
},
{
"expand": true,
"cwd": "node_modules/jquery-cycle2/build/plugin",
"src": "jquery.cycle2.center.min.js",
"dest": "dist/js/vendor",
},
];
copyFiles = copyFiles.concat( cycle2Files );
}
// optionally add magnific
if ( settings.copyMagnific ) {
const magnificFiles = [
{
"expand": true,
"cwd": "node_modules/magnific-popup/dist",
"src": "jquery.magnific-popup.min.js",
"dest": "dist/js/vendor",
},
{
"expand": true,
"cwd": "node_modules/magnific-popup/dist",
"src": "magnific-popup.css",
"dest": "dist/css/vendor",
}
];
copyFiles = copyFiles.concat( magnificFiles );
}
// optionally add mixitup
if ( settings.copyMixitup ) {
const mixitupFiles = [
{
expand: true,
cwd: 'node_modules/mixitup/dist',
src: 'mixitup.min.js',
dest: 'dist/js/vendor',
},
];
copyFiles = copyFiles.concat( mixitupFiles );
}
copyFiles = ! settings.additionalCopyFiles ? copyFiles : copyFiles.concat( settings.additionalCopyFiles );
// define php files to be linted
const phpPaths = settings.phpFiles.join( ' ' );
grunt.initConfig( {
pkg: pkg,
sass: {
// sass tasks
dist: {
files: settings.sassFiles,
},
options: {
implementation: sass,
compass: true,
style: 'expanded',
sourceMap: true,
silenceDeprecations: [ 'import' ],
},
},
postcss: {
options: {
map: true, // inline sourcemaps
processors: [
// add vendor prefixes
require( 'autoprefixer' )( { grid: 'autoreplace' } ),
// minify the result
require( 'cssnano' )( {
preset: [ 'default', {
colormin: false, // Disable color optimization completely. Shortening is mostly handled by stylelint already.
reduceInitial: false, // stops background-color from changing transparent to initial
} ],
} ),
],
},
dist: {
src: 'dist/css/*.css',
},
},
copy: {
preflight: {
files: copyFiles,
options: {
process: function( content, srcpath ) {
if ( srcpath.includes( 'package.json' ) ) {
//if copying package json, replace the placeholder version with the current local version
return content.replace( '"version": "0.0.1"', '"version": "' + pkg.version + '"' );
}
return content;
},
},
},
},
modernizr: {
dist: {
crawl: false,
customTests: [],
dest: 'dist/js/vendor/modernizr.min.js',
// lookup test names or make a custom set here: https://modernizr.com/download?setclasses
tests: [
'hiddenscroll',
'input',
'inputtypes',
'svg',
'webp',
'touchevents',
[ 'cssgrid', 'cssgridlegacy' ],
'flexbox',
'flexboxlegacy',
'objectfit',
'cssvhunit',
'cssvwunit',
'flexgap',
],
options: [ 'setClasses' ],
uglify: true,
},
},
terser: {
options: {
sourceMap: true,
},
dist: {
files: [
{
expand: true,
src: '*.js',
dest: 'dist/js',
cwd: 'js',
ext: '.min.js',
},
],
},
},
stylelint: {
src: [ 'css/*.scss', 'css/**/*.scss', 'css/*.css' ],
},
eslint: {
gruntfile: {
src: [ 'Gruntfile.js' ],
},
src: {
src: [ 'js' ],
},
},
svgstore: {
options: {
prefix: 'icon-', // This will prefix each <g> ID
class: 'svgstore',
},
default: {
files: {
'icons/svg-defs.svg': [ 'icons/svg-originals/*.svg' ],
},
},
},
run: {
stylelintfix: {
cmd: 'npx',
args: [ 'stylelint', 'css/*.scss', 'css/**/*.scss', '--fix' ],
},
eslintfix: {
cmd: 'npx',
args: [ 'eslint', 'js/*.js', 'Gruntfile.js', '--fix' ],
},
phpcs: {
cmd: 'bash',
args: [
'-c',
'./vendor/squizlabs/php_codesniffer/bin/phpcs ' +
'--standard=phpcs.xml ' +
'--runtime-set ignore_warnings_on_exit 1 ' +
phpPaths,
],
},
phpcbf: {
cmd: 'bash',
args: [
'-c',
'./vendor/squizlabs/php_codesniffer/bin/phpcbf ' +
'--standard=phpcs.xml ' +
phpPaths + ' || true',
],
},
bump: {
cmd: 'npm',
args: [ 'run', 'release', '--', '--prerelease', branch, '--skip.tag', '--skip.changelog' ],
},
update: {
cmd: 'npm',
args: [ 'update' ]
},
ding: {
cmd: 'tput',
args: [ 'bel' ],
},
},
watch: {
css: {
files: [ 'css/*.scss', 'css/**/*.scss' ],
tasks: [ 'run:stylelintfix', 'sass', 'postcss', 'run:ding' ],
},
js: {
files: [ 'js/*.js' ],
tasks: [ 'run:eslintfix', 'terser', 'run:ding' ],
},
},
gitnewer: {
checkForNewFiles: {
options: {
override: function( details, include ) {
include( true );
},
},
},
},
checkForNewFiles: {
src: {
src: [ '**/*.*', '*.*' ],
},
},
} );
grunt.loadNpmTasks( 'grunt-sass' );
grunt.loadNpmTasks( 'grunt-contrib-watch' );
grunt.loadNpmTasks( 'grunt-terser' );
grunt.loadNpmTasks( 'grunt-phpcs' );
grunt.loadNpmTasks( 'grunt-stylelint' );
grunt.loadNpmTasks( 'grunt-eslint' );
grunt.loadNpmTasks( '@lodder/grunt-postcss' );
grunt.loadNpmTasks( 'grunt-contrib-copy' );
grunt.loadNpmTasks( 'grunt-run' );
grunt.loadNpmTasks( 'grunt-gitnewer' );
// optional npm tasks
if ( settings.doModernizr ) {
grunt.loadNpmTasks( 'grunt-modernizr' );
}
if ( settings.doSvgStore ) {
grunt.loadNpmTasks( 'grunt-svgstore' );
}
grunt.registerTask( 'default', [ 'run:stylelintfix', 'run:eslintfix', 'sass', 'postcss', 'terser', 'watch' ] );
grunt.registerTask( 'update', [ 'run:update', 'copy:preflight' ] );
grunt.registerTask( 'compile', 'compile task with optional modernizr', function() {
const beforeTasks = [ 'sass', 'postcss' ];
const afterTasks = [ 'terser' ];
if ( settings.doModernizr ) {
beforeTasks.push( 'modernizr' );
}
tasks = beforeTasks.concat( afterTasks );
if ( settings.doSvgStore ) {
beforeTasks.push( 'svgstore' );
}
grunt.task.run( tasks );
} );
grunt.registerTask( 'lint', [ 'stylelint', 'eslint', 'run:phpcs' ] );
grunt.registerTask( 'phpfix', [ 'run:phpcbf' ] );
grunt.registerTask( 'fix', [ 'run:stylelintfix', 'run:eslintfix', 'run:phpcbf' ] );
grunt.registerTask( 'bump', [ 'run:bump' ] );
grunt.registerTask( 'preflight', [ 'compile', 'lint', 'shouldBump', 'bump', 'run:ding' ] );
grunt.registerTask( 'shouldBump', [ 'gitnewer:checkForNewFiles' ] ); // send output of git-newer to checkForNewFiles
grunt.registerMultiTask( 'checkForNewFiles', 'Check if files changed that need to be committed before bumping.', function() {
// files that change with bump:
const versionFiles = [
'package-lock.json',
'package.json',
'functions.php',
'plugin.php',
'readme.txt',
];
// optionally add some other files to this list:
if ( typeof settings.additionalversionFiles == 'object' && settings.additionalversionFiles.length ) {
versionFiles = versionFiles.concat( settings.additionalversionFiles );
}
this.filesSrc.forEach( function( file ) {
if ( ! versionFiles.includes( file ) ) {
grunt.fail.warn( file + ' should be committed before bump. ' ); // abort mission
}
} );
} );
};