-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
executable file
·286 lines (253 loc) · 8.31 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
const gulp = require('gulp');
/**
* Code block for sass tasks
*/
(function(){
const sass = require('gulp-sass');
const minifyCss = require('gulp-clean-css');
const sourcemaps = require('gulp-sourcemaps');
const autoprefixer = require('gulp-autoprefixer');
const postcss = require('gulp-postcss');
const postcssPrefix = require('postcss-prefix-selector');
const civicrmScssRoot = require('civicrm-scssroot')();
const bootstrapNamespace = '#bootstrap-theme';
/**
* Gulp sass task for compiling SASS to css
*/
gulp.task('sass', ['sass-sync'], () => {
gulp.src('sass/mosaico-bootstrap.scss')
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'compressed',
includePaths: civicrmScssRoot.getPath(),
precision: 10
}).on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(postcss([postcssPrefix({
prefix: bootstrapNamespace + ' ',
exclude: [/^html/, /^body/, /^.select2-drop-auto-width/, /^div\[ng-controller="PreviewMailingDialogCtrl"\]/]
})]))
.pipe(minifyCss())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./css/'));
gulp.src('sass/mosaico-crmstar.scss')
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'compressed',
includePaths: civicrmScssRoot.getPath(),
precision: 10
}).on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(minifyCss())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./css/'));
gulp.src('sass/legacy.scss')
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'compressed',
includePaths: civicrmScssRoot.getPath(),
precision: 10
}).on('error', sass.logError))
.pipe(autoprefixer({
browsers: ['last 2 versions'],
cascade: false
}))
.pipe(minifyCss())
.pipe(sourcemaps.write('./'))
.pipe(gulp.dest('./css/'));
});
/**
* Gulp sass task for getting scssRoot for civicrm related config
*/
gulp.task('sass-sync',() => {
civicrmScssRoot.updateSync();
});
/**
* Gulp watch task for getting continious watch of sass
*/
gulp.task('watch', () => {
gulp.watch('sass/**/*.scss', ['sass']);
});
}());
/**
* Code block for backstopJS tasks
*/
(function(){
const _ = require('lodash');
const argv = require('yargs').argv;
const backstopjs = require('backstopjs');
const clean = require('gulp-clean');
const colors = require('ansi-colors');
const execSync = require('child_process').execSync;
const file = require('gulp-file');
const fs = require('fs');
const notify = require('gulp-notify');
const path = require('path');
const puppeteer = require('puppeteer');
const PluginError = require('plugin-error');
const BACKSTOP_DIR = 'tests/backstop_data/';
const CONFIG_TPL = { 'url': 'http://%{site-host}', 'root': '%{path-to-site-root}' };
const FILES = {
siteConfig: path.join(BACKSTOP_DIR, 'site-config.json'),
temp: path.join(BACKSTOP_DIR, 'backstop.temp.json'),
tpl: path.join(BACKSTOP_DIR, 'backstop.tpl.json')
};
/**
* Gulp backstop tasks
* 'backstopjs:reference': For creating reference screenshots
* 'backstopjs:test': For creating test screenshots and matching them
* 'backstopjs:openReport': For opening reports in the browser
* 'backstopjs:approve': Approving reports
*/
['reference', 'test', 'openReport', 'approve'].map(action => {
gulp.task('backstopjs:' + action, () => runBackstopJS(action));
});
/**
* Removes the temp config file and sends a notification
* based on the given outcome from BackstopJS
*
* @param {Boolean} success
*/
function cleanUpAndNotify (success) {
gulp
.src(FILES.temp, { read: false })
.pipe(clean())
.pipe(notify({
message: success ? 'Success' : 'Error',
title: 'BackstopJS',
sound: 'Beep'
}));
}
/**
* Creates the content of the config temporary file that will be fed to BackstopJS
* The content is the mix of the config template and the list of scenarios
* under the scenarios/ folder
*
* @return {String}
*/
function createTempConfig () {
const config = siteConfig();
const content = JSON.parse(fs.readFileSync(FILES.tpl));
content.scenarios = _(content.scenarios).map((scenario, index, scenarios) => {
return _.assign(scenario, {
cookiePath: path.join(BACKSTOP_DIR, 'cookies', 'admin.json'),
count: '(' + (index + 1) + ' of ' + scenarios.length + ')',
url: scenario.url.replace('{url}', config.url)
});
})
.value();
['bitmaps_reference', 'bitmaps_test', 'html_report', 'ci_report', 'engine_scripts'].forEach(path => {
content.paths[path] = BACKSTOP_DIR + content.paths[path];
});
return JSON.stringify(content);
}
/**
* Runs backstopJS with the given command.
*
* It fills the template file with the list of scenarios, creates a temp
* file passed to backstopJS, then removes the temp file once the command is completed
*
* @param {String} command
* @return {Promise}
*/
function runBackstopJS (command) {
if (touchSiteConfigFile()) {
throwError(
'No site-config.json file detected!\n' +
`\tOne has been created for you under ${path.basename(BACKSTOP_DIR)}\n` +
'\tPlease insert the real value for each placeholder and try again'
);
}
return new Promise((resolve, reject) => {
let success = false;
gulp.src(FILES.tpl)
.pipe(file(path.basename(FILES.temp), createTempConfig()))
.pipe(gulp.dest(BACKSTOP_DIR))
.on('end', async () => {
try {
(typeof argv.skipCookies === 'undefined') && await writeCookies();
await backstopjs(command, { configPath: FILES.temp, filter: argv.filter });
success = true;
} finally {
cleanUpAndNotify(success);
success ? resolve() : reject(new Error('BackstopJS error'));
}
});
})
.catch(function (err) {
throwError(err.message);
});
}
/**
* Returns the content of site config file
*
* @return {Object}
*/
function siteConfig () {
return JSON.parse(fs.readFileSync(FILES.siteConfig));
}
/**
* Creates the site config file is in the backstopjs folder, if it doesn't exists yet
*
* @return {Boolean} Whether the file had to be created or not
*/
function touchSiteConfigFile () {
let created = false;
try {
fs.readFileSync(FILES.siteConfig);
} catch (err) {
fs.writeFileSync(FILES.siteConfig, JSON.stringify(CONFIG_TPL, null, 2));
created = true;
}
return created;
}
/**
* A simple wrapper for displaying errors
* It converts the tab character to the amount of spaces required to correctly
* align a multi-line block of text horizontally
*
* @param {String} msg
* @throws {Error}
*/
function throwError (msg) {
throw new PluginError('Error', {
message: colors.red(msg.replace(/\t/g, ' '))
});
}
/**
* Writes the session cookie files that will be used to log in as different users
*
* It uses the [`drush uli`](https://drushcommands.com/drush-7x/user/user-login/)
* command to generate a one-time login url, the browser then go to that url
* which then creates the session cookie
*
* The cookie is then stored in a json file which is used by the BackstopJS scenarios
* to log in
*
* @return {Promise}
*/
async function writeCookies () {
const cookiesDir = path.join(BACKSTOP_DIR, 'cookies');
const cookieFilePath = path.join(cookiesDir, 'admin.json');
const config = siteConfig();
const loginUrl = execSync(`drush uli --name=admin --uri=${config.url} --browser=0`, { encoding: 'utf8', cwd: config.root });
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(loginUrl);
const cookies = await page.cookies();
await browser.close();
!fs.existsSync(cookiesDir) && fs.mkdirSync(cookiesDir);
fs.existsSync(cookieFilePath) && fs.unlinkSync(cookieFilePath);
fs.writeFileSync(cookieFilePath, JSON.stringify(cookies));
}
}());
/**
* Gulp default task
*/
gulp.task('default', ['sass', 'watch']);