Skip to content

Commit

Permalink
Incremental number for filename - Fixes #239 (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos authored and sindresorhus committed Jan 7, 2017
1 parent 3671b6a commit b91f0ea
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import filenamifyUrl from 'filenamify-url';
import template from 'lodash.template';
import pify from 'pify';
import plur from 'plur';
import unusedFilename from 'unused-filename';

type PageresStream = Readable & {filename: string};

type Options = {
delay?: number;
timeout?: number;
crop?: boolean;
incrementalName?: boolean;
css?: string;
cookies?: Array<string> | {[key: string]: string};
filename?: string;
Expand Down Expand Up @@ -78,6 +80,7 @@ export default class Pageres<DestValue: string> extends EventEmitter {
this.options = Object.assign({}, options);
this.options.filename = this.options.filename || '<%= url %>-<%= size %><%= crop %>';
this.options.format = this.options.format || 'png';
this.options.incrementalName = this.options.incrementalName || false;

this.stats = {};
this.items = [];
Expand Down Expand Up @@ -220,6 +223,10 @@ export default class Pageres<DestValue: string> extends EventEmitter {
url: filenamifyUrl(uri)
});

if (options.incrementalName) {
stream.filename = unusedFilename.sync(stream.filename);
}

return stream;
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
"protocolify": "^1.0.0",
"rimraf": "^2.2.8",
"screenshot-stream": "^4.1.0",
"unused-filename": "^0.1.0",
"viewport-list": "^5.0.1"
},
"devDependencies": {
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ Available variables:
- `date`: The current date (Y-M-d), eg. 2015-05-18
- `time`: The current time (h-m-s), eg. 21-15-11

##### incrementalName

Type: `boolean`
Default: `false`

When a file exists, append an incremental number.

##### selector

Type: `string`
Expand Down
13 changes: 13 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,16 @@ test('support data uri', async t => {
t.is(pixels[1], 0);
t.is(pixels[2], 0);
});

test('when a file exists, append an incrementer', async t => {
const folderPath = process.cwd();
try {
await new Pageres({delay: 2}).src('yeoman.io', ['1024x768', '480x320'], {incrementalName: true, filename: '<%= url %>'}).dest(folderPath).run();
t.true(fs.existsSync(path.join(folderPath, `yeoman.io.png`)));
await new Pageres({delay: 2}).src('yeoman.io', ['1024x768', '480x320'], {incrementalName: true, filename: '<%= url %>'}).dest(folderPath).run();
t.true(fs.existsSync(path.join(folderPath, `yeoman.io (1).png`)));
} finally {
await fsP.unlink(path.join(folderPath, `yeoman.io.png`));
await fsP.unlink(path.join(folderPath, `yeoman.io (1).png`));
}
});

0 comments on commit b91f0ea

Please sign in to comment.