Skip to content

Commit e946198

Browse files
committed
feat: Add optional automatic file numbering
1 parent a031a76 commit e946198

File tree

4 files changed

+77
-29
lines changed

4 files changed

+77
-29
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ Default options support the most usual usage scenario:
7979
doctype: '<!DOCTYPE html>',
8080
snapshots: 'snapshots',
8181
screenshots: null,
82+
fileNumbering: false,
83+
fileNumberDigits: 3,
84+
fileNumberSeparator: '.',
8285
force: false
8386
},
8487
'google': {
@@ -163,6 +166,24 @@ Default value: null
163166

164167
Destination directory to write the viewport screenshots to. It will be created if it does not exist. Default value is `null`, which means, that no screenshots will be written out.
165168

169+
#### fileNumbering
170+
Type: `Booolean`
171+
Default value: `false`
172+
173+
Enables prefixing names of snapshot and screenshot files with their index number, for example: "002.google-input.html".
174+
175+
#### fileNumberDigits
176+
Type: `Number`
177+
Default value: `3`
178+
179+
Ensures, that file numbers will have always at least the specified count of digits. If the number is smaller, than its power of 10, the number will be padded by zeros (0) from the left.
180+
181+
#### fileNumberSeparator
182+
Type: `String`
183+
Default value: '.'
184+
185+
The character to put between the file number and the file name.
186+
166187
#### force
167188
Type: `Boolean`
168189
Default value: false
@@ -475,6 +496,8 @@ your code using Grunt.
475496
476497
## Release History
477498
499+
* 2018-03-01 [v0.7.0] Add optional automatic file numbering
500+
* 2018-02-28 [v0.6.0] Add the allRequired option to the hasClass instruction
478501
* 2018-02-26 [v0.5.0] Allow checking and setting various properties
479502
* 2018-02-22 [v0.4.0] Allow sending key strokes to the browser
480503
* 2018-01-30 [v0.3.0] Allow specifying test commands in separate modules

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
},
3636
"dependencies": {
3737
"mkdirp": "^0.5.1",
38+
"pad-left": "^2.1.0",
3839
"webdriverio": "^4.11.0"
3940
},
4041
"devDependencies": {

tasks/html-dom-snapshot.js

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'use strict';
1111

1212
const fs = require('fs'),
13+
pad = require('pad-left'),
1314
path = require('path'),
1415
mkdirp = require('mkdirp'),
1516
instructions = [
@@ -24,6 +25,7 @@ const fs = require('fs'),
2425
].map(function (instruction) {
2526
return require('./instructions/' + instruction);
2627
});
28+
var fileCount = 0;
2729

2830
module.exports = function (grunt) {
2931
grunt.registerMultiTask('html-dom-snapshot',
@@ -45,6 +47,9 @@ module.exports = function (grunt) {
4547
selectorTimeout: 10000,
4648
doctype: '<!DOCTYPE html>',
4749
snapshots: 'snapshots',
50+
fileNumbering: false,
51+
fileNumberDigits: 3,
52+
fileNumberSeparator: '.',
4853
force: false
4954
}),
5055
target = this.target,
@@ -159,6 +164,9 @@ module.exports = function (grunt) {
159164
lastViewport: lastViewport
160165
}, options, command.options || {}),
161166
file = command.file,
167+
fileNumbering = commandOptions.fileNumbering,
168+
fileNumberDigits = commandOptions.fileNumberDigits,
169+
fileNumberSeparator = commandOptions.fileNumberSeparator,
162170
viewport = commandOptions.viewport,
163171
screenshots = commandOptions.screenshots,
164172
commandInstructions = instructions.map(function (instruction) {
@@ -204,12 +212,15 @@ module.exports = function (grunt) {
204212
}, viewportSet)
205213
.then(function () {
206214
if (snapshots && screenshots) {
215+
++fileCount;
207216
return Promise.all([makeSnapshot(), makeScreenshot()]);
208217
}
209218
if (snapshots) {
219+
++fileCount;
210220
return makeSnapshot();
211221
}
212222
if (screenshots) {
223+
++fileCount;
213224
return makeScreenshot();
214225
}
215226
});
@@ -226,51 +237,64 @@ module.exports = function (grunt) {
226237

227238
function saveContent(html) {
228239
if (file) {
229-
const testFile = file.toLowerCase(),
230-
htmlFile = testFile.endsWith('.html') ||
231-
testFile.endsWith('.htm') ? file : file + '.html',
232-
target = path.join(snapshots, htmlFile);
233-
grunt.log.ok('Write snapshot to "' + target + '".');
240+
var fileName = file.toLowerCase();
241+
fileName = fileName.endsWith('.html') ||
242+
fileName.endsWith('.htm') ? file : file + '.html';
243+
if (fileNumbering) {
244+
fileName = numberFileName(fileName);
245+
}
246+
fileName = path.join(snapshots, fileName);
247+
grunt.log.ok('Write snapshot to "' + fileName + '".');
234248
return ensureDirectory(snapshots)
235249
.then(function () {
236250
return new Promise(function (resolve, reject) {
237-
fs.writeFile(target, commandOptions.doctype + html, function (error) {
238-
if (error) {
239-
reject(error);
240-
} else {
241-
++snapshotCount;
242-
resolve();
243-
}
244-
});
251+
fs.writeFile(fileName, commandOptions.doctype + html,
252+
function (error) {
253+
if (error) {
254+
reject(error);
255+
} else {
256+
++snapshotCount;
257+
resolve();
258+
}
259+
});
245260
});
246261
});
247262
}
248263
}
249264

250265
function saveImage(png) {
251266
if (file) {
252-
const testFile = file.toLowerCase(),
253-
baseFile = testFile.endsWith('.html') ?
254-
file.substr(0, file.length - 5) :
255-
testFile.endsWith('.htm') ?
256-
file.substr(0, file.length - 4) : file,
257-
target = path.join(screenshots, baseFile + '.png');
258-
grunt.log.ok('Write screenshot to "' + target + '".');
267+
var fileName = file.toLowerCase();
268+
fileName = fileName.endsWith('.html') ?
269+
file.substr(0, file.length - 5) :
270+
fileName.endsWith('.htm') ?
271+
file.substr(0, file.length - 4) : file;
272+
if (fileNumbering) {
273+
fileName = numberFileName(fileName);
274+
}
275+
fileName = path.join(screenshots, fileName + '.png');
276+
grunt.log.ok('Write screenshot to "' + fileName + '".');
259277
return ensureDirectory(screenshots)
260278
.then(function () {
261279
return new Promise(function (resolve, reject) {
262-
fs.writeFile(target, new Buffer(png.value, 'base64'), function (error) {
263-
if (error) {
264-
reject(error);
265-
} else {
266-
++screenshotCount;
267-
resolve();
268-
}
269-
});
280+
fs.writeFile(fileName, new Buffer(png.value, 'base64'),
281+
function (error) {
282+
if (error) {
283+
reject(error);
284+
} else {
285+
++screenshotCount;
286+
resolve();
287+
}
288+
});
270289
});
271290
});
272291
}
273292
}
293+
294+
function numberFileName(fileName) {
295+
return pad(fileCount.toString(), fileNumberDigits, '0') +
296+
fileNumberSeparator + fileName;
297+
}
274298
}
275299
});
276300
};

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ exports['html-dom-snapshot'] = {
2525

2626
'static': function (test) {
2727
const snapshot = fs.existsSync('test/snapshots/static.html'),
28-
screenshot = fs.statSync('test/screenshots/static.png');
28+
screenshot = fs.statSync('test/screenshots/002.static.png');
2929
test.ok(!snapshot, 'static.html');
3030
test.ok(screenshot.size > 1024, 'static.png');
3131
test.done();

0 commit comments

Comments
 (0)