This repository has been archived by the owner on Aug 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
186 lines (145 loc) · 4.27 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
'use strict';
/* ==========================
Styleguide Build Tasks
- Config
- Stylesheets
- Pattern lab
- Watch
- Tests
- Development
========================= */
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const gulp = require('gulp');
const shell = require('gulp-shell');
const gutil = require('gulp-util');
const sass = require('gulp-sass');
const postcss = require('gulp-postcss');
const sourcemaps = require('gulp-sourcemaps');
const stylelint = require('gulp-stylelint');
const reporter = require('postcss-reporter');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const backstopjs = require('backstopjs');
const runSequence = require('run-sequence');
// -------------------
// Config
const PATHS = {
src: `${__dirname}/source`,
dist: `${__dirname}/public`,
tmp: `${__dirname}/.tmp`
};
const options = {
env: process.env.NODE_ENV || 'development'
};
// -------------------
// Stylesheets
gulp.task('styles', ['lint:styles'], () => {
const AUTOPREFIXER_CONFIG = {
browsers: ['last 1 version'],
};
const POSTCSS_PLUGINS = [
autoprefixer(AUTOPREFIXER_CONFIG),
cssnano(),
reporter(),
];
return gulp.src(`${PATHS.src}/stylesheets/scss/**/*.scss`)
.pipe(sourcemaps.init())
.pipe(sass().on('error', sass.logError))
.pipe(postcss(POSTCSS_PLUGINS))
.pipe(sourcemaps.write())
.pipe(gulp.dest(`${PATHS.tmp}/stylesheets`));
});
gulp.task('lint:styles', () => {
return gulp.src(`${PATHS.src}/**/*.scss`)
.pipe(stylelint({
reporters: [{ formatter: 'string', console: true }],
}));
});
gulp.task('copy:styles', () => {
let stream;
const delay = options.env === 'production' ? 0 : 1000;
setTimeout(() => {
gutil.log(`Starting '${gutil.colors.cyan('copy:style')}' set delay: ${gutil.colors.magenta(delay)} ms`);
stream = gulp.src(`${PATHS.tmp}/stylesheets/*.css`)
.pipe(gulp.dest(`${PATHS.src}/stylesheets`));
gutil.log(`Finished '${gutil.colors.cyan('copy:style')}'`);
}, delay);
return stream;
});
gulp.task('styles:dev', (cb) => {
runSequence('styles', 'copy:styles', cb);
});
// -------------------
// Pattern Lab
gulp.task('serve:patternlab', function (cb) {
const server = exec('php core/console --server --with-watch', cb);
server.stdout.on('data', (data) => {
const url = /https?:\/\/[\w-]+:+\d{4}?/gi;
if (url.test(data.toString())) {
gutil.log(gutil.colors.cyan(data.toString()));
} else {
gutil.log(data.toString());
}
});
server.stderr.on('data', (data) => {
process.stdout.write(data.toString());
});
server.on('close', (code) => {
if (code === 0) {
cb();
}
else {
console.log(`close: ${code}`);
}
});
});
// -------------------
// Watch
gulp.task('watch', () => {
gulp.watch(`${PATHS.src}/**/*.scss`, ['styles:dev']);
});
// -------------------
// Tests
let BACKSTOP_CONFIG = require('./tests/visual-regression/backstop.config.js')();
// Getting **.tests.json files from component directories.
function setupBackstopjs(cb) {
gutil.log(`Starting '${gutil.colors.cyan('Setup Backstopjs:')}'...`);
if (!Array.isArray(BACKSTOP_CONFIG.scenarios)) {
BACKSTOP_CONFIG.scenarios = [];
}
gutil.log(`Starting ${gutil.colors.cyan('Setup Backstopjs:')}...`);
return gulp.src(`${PATHS.src}/**/*.tests.json`)
.on('data', (file) => {
gutil.log(`'${file.path}'`);
const unitConfig = JSON.parse(fs.readFileSync(file.path));
if(Array.isArray(unitConfig.scenarios) ) {
BACKSTOP_CONFIG.scenarios = BACKSTOP_CONFIG.scenarios.concat(unitConfig.scenarios);
}
})
.on('end', () => {
if (cb) cb();
});
}
gulp.task('visual-regrresion:ref', () => {
return setupBackstopjs(() => backstopjs('reference', { config: BACKSTOP_CONFIG }));
});
gulp.task('visual-regrresion:test', () => {
return setupBackstopjs(() => backstopjs('test', {
config: BACKSTOP_CONFIG,
filter: gutil.env.filter || null
}));
});
gulp.task('visual-regrresion:approve', () => {
return setupBackstopjs(() => backstopjs('approve', { config: BACKSTOP_CONFIG }));
});
// -------------------
// Development
gulp.task('default', ['styles'], () => {
runSequence (
'copy:styles',
'serve:patternlab',
'watch'
);
});