Skip to content

Commit

Permalink
Close GH-65: Add ability to specify different resolutions for differe…
Browse files Browse the repository at this point in the history
…nt URLs.
  • Loading branch information
kevva authored and sindresorhus committed Mar 31, 2014
1 parent 50b2a30 commit 5c6ddc6
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 40 deletions.
80 changes: 58 additions & 22 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var stdin = require('get-stdin');
var eachAsync = require('each-async');
var getRes = require('get-res');
var multiline = require('multiline');
var subarg = require('subarg');
var pageres = require('./index');

function showHelp() {
Expand All @@ -19,25 +20,35 @@ Specify urls and screen resolutions as arguments. Order doesn't matter.
Screenshots are saved in the current directory.
Usage
pageres <url> <resolution> [<resolution> <url> ...]
pageres <url> <resolution>
pagesres [<url> <resolution>] [<url> <resolution>]
pageres [<url> <resolution> ...] < <file>
cat <file> | pageres [<url> <resolution> ...]
Example
pageres todomvc.com yeoman.io 1366x768 1600x900
pageres [yeoman.io 1366x768 1600x900] [todomvc.com 1024x768 480x320]
pageres 1366x768 < urls.txt
cat screen-resolutions.txt | pageres todomvc.com yeoman.io
You can also pipe in a newline separated list of urls and screen resolutions which will get merged with the arguments. If no screen resolutions are specified it will fall back to the ten most popular ones according to w3counter.
*/}));
}

function generate(urls, sizes) {
pageres(urls, sizes, null, function (err, items) {
function generate(args) {
var sizes = [];
var urls = [];

pageres(args, null, function (err, items) {
if (err) {
throw err;
}

args.forEach(function (arg) {
sizes = sizes.concat(arg.sizes);
urls = urls.concat(arg.url);
});

eachAsync(items, function (el, i, next) {
var stream = el.pipe(fs.createWriteStream(el.filename));
el.on('error', next);
Expand All @@ -48,15 +59,20 @@ function generate(urls, sizes) {
throw err;
}

var i = sizes.length;
var s = sizes.filter(function (el, i, self) {
return self.indexOf(el) === i;
}).length;
var u = urls.length;
var s = sizes.length;

console.log(chalk.green('\n✓ Successfully generated %d screenshots from %d %s and %d %s'), u * s, u, (u === 1 ? 'url' : 'urls'), s, (s === 1 ? 'resolution': 'resolutions'));
console.log(chalk.green('\n✓ Successfully generated %d screenshots from %d %s and %d %s'), i, u, (u === 1 ? 'url' : 'urls'), s, (s === 1 ? 'resolution': 'resolutions'));
});
});
}

function init(args) {
var items = [];

if (opts.help) {
return showHelp();
}
Expand All @@ -65,27 +81,47 @@ function init(args) {
return console.log(require('./package').version);
}

var urls = _.uniq(args.filter(/./.test, /\.|localhost/));
var sizes = _.uniq(args.filter(/./.test, /^\d{3,4}x\d{3,4}$/i));

if (urls.length === 0) {
console.error(chalk.yellow('Specify at least one url'));
return showHelp();
if (!args.some(function (arr) { return arr._ !== undefined; })) {
args = [{ _: args }];
}

if (sizes.length === 0) {
return getRes(function (err, sizes) {
if (err) {
throw err;
}
eachAsync(args, function (el, i, next) {
el = el._;

console.log('No sizes specified. Falling back to the ten most popular screen resolutions according to w3counter:\n' + sizes.join(' '));
var url = _.uniq(el.filter(/./.test, /\.|localhost/));
var size = _.uniq(el.filter(/./.test, /^\d{3,4}x\d{3,4}$/i));

generate(urls, sizes);
});
}
if (url.length === 0) {
console.error(chalk.yellow('Specify a url'));
return showHelp();
}

if (size.length === 0) {
return getRes(function (err, data) {
if (err) {
throw err;
}

generate(urls, sizes);
size = data;
console.log('No sizes specified. Falling back to the ten most popular screen resolutions according to w3counter as of January 2014:\n' + size.join(' '));

items.push({ url: url, sizes: size });
next();
});
}

if (url.length > 1) {
url.forEach(function (el) {
items.push({ url: el, sizes: size });
});
} else {
items.push({ url: url, sizes: size });
}

next();
}, function () {
generate(items);
});
}

sudoBlock();
Expand All @@ -98,7 +134,7 @@ var opts = nopt({
v: '--version'
});

var args = opts.argv.remain;
var args = subarg(opts.argv.remain)._;

if (process.stdin.isTTY) {
init(args);
Expand Down
32 changes: 21 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
var spawn = require('child_process').spawn;
var path = require('path');
var _ = require('lodash');
var urlMod = require('url');
var slugifyUrl = require('slugify-url');
var phantomjsBin = require('phantomjs').path;
Expand Down Expand Up @@ -60,23 +61,32 @@ function generateSizes(url, size, opts) {
return stream;
}

module.exports = function (urls, sizes, opts, cb) {
module.exports = function (args, opts, cb) {
var items = [];

if (!cb && _.isFunction(opts)) {
cb = opts;
opts = {};
}

args = args || [];
opts = opts || {};
cb = cb || function () {};

if (urls.length === 0) {
return cb(new Error('URLs required'));
}
args.forEach(function (arg) {
if (!arg.url || arg.url.length === 0) {
return cb(new Error('URLs required'));
}

if (sizes.length === 0) {
return cb(new Error('Sizes required'));
}
if (!arg.sizes || arg.sizes.length === 0) {
return cb(new Error('Sizes required'));
}

var items = [];
arg.url = Array.isArray(arg.url) ? arg.url.join('') : arg.url;
arg.sizes = Array.isArray(arg.sizes) ? arg.sizes : [arg.sizes];

urls.forEach(function (url) {
sizes.forEach(function (size) {
items.push(generateSizes(url, size, opts));
arg.sizes.forEach(function (size) {
items.push(generateSizes(arg.url, size, opts));
});
});

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"base64-stream": "~0.1.2",
"get-res": "~0.1.3",
"object-assign": "~0.2.0",
"multiline": "^0.1.0"
"multiline": "^0.1.0",
"subarg": "~0.0.1"
},
"devDependencies": {
"mocha": "*"
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ Screenshots are saved in the current directory.
Usage
pageres <url> <resolution> [<resolution> <url> ...]
pagesres [<url> <resolution>] [<url> <resolution>]
pageres [<url> <resolution> ...] < <file>
cat <file> | pageres [<url> <resolution> ...]
Example
pageres todomvc.com yeoman.io 1366x768 1600x900
pageres [yeoman.io 1366x768 1600x900] [todomvc.com 1024x768 480x320]
pageres 1366x768 < urls.txt
cat screen-resolutions.txt | pageres todomvc.com yeoman.io
Expand All @@ -62,7 +64,7 @@ $ npm install --save pageres
```js
var pageres = require('pageres');
pageres(['todomvc.com'], ['1366x768', '1600x900'], function () {
pageres({ url: 'yeoman.io', sizes: ['1366x768', '1600x900']}, null, function (err, streams) {
console.log('done');
});
```
Expand Down
22 changes: 17 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
/*global afterEach, beforeEach, it */
/*global it */
'use strict';
var assert = require('assert');
var fs = require('fs');
var pageres = require('./index');

it('should generate screenshots', function (cb) {
this.timeout(20000);

pageres(['yeoman.io', 'todomvc.com'], ['1024x768', '640x480'], null, function (err, streams) {
var items = [{
url: 'yeoman.io',
sizes: ['480x320', '1024x768']
}, {
url: 'todomvc.com',
sizes: ['1280x1024', '1920x1080']
}];

pageres(items, function (err, streams) {
assert(!err);
assert.strictEqual(streams.length, 4);
assert.strictEqual(streams[0].filename, 'yeoman.io-1024x768.png');
assert.strictEqual(streams[0].filename, 'yeoman.io-480x320.png');

streams[0].once('data', function (data) {
assert(data.length > 1000);
Expand All @@ -22,7 +29,12 @@ it('should generate screenshots', function (cb) {
it('should remove special characters from the URL to create a valid filename', function (cb) {
this.timeout(20000);

pageres(['http://microsoft.com/?query=pageres*|<>:"\\'], ['1024x768'], null, function (err, streams) {
var items =[{
url: 'http://microsoft.com/?query=pageres*|<>:"\\',
sizes: '1024x768'
}];

pageres(items, function (err, streams) {
assert(!err);
assert.strictEqual(streams.length, 1);
assert.strictEqual(streams[0].filename, 'microsoft.com!query=pageres-1024x768.png');
Expand Down

0 comments on commit 5c6ddc6

Please sign in to comment.