Skip to content

Commit

Permalink
Merge pull request #79 from kevva/new
Browse files Browse the repository at this point in the history
New API
  • Loading branch information
sindresorhus committed May 15, 2014
2 parents 5916a7c + 55f258e commit 71f275e
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 138 deletions.
171 changes: 85 additions & 86 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
#!/usr/bin/env node
'use strict';
var fs = require('fs');
var nopt = require('nopt');
var chalk = require('chalk');
var sudoBlock = require('sudo-block');
var _ = require('lodash');
var stdin = require('get-stdin');
var chalk = require('chalk');
var eachAsync = require('each-async');
var getRes = require('get-res');
var multiline = require('multiline');
var subarg = require('subarg');
var nopt = require('nopt');
var updateNotifier = require('update-notifier');
var pageres = require('./index');
var stdin = require('get-stdin');
var subarg = require('subarg');
var sudoBlock = require('sudo-block');
var Pageres = require('./');
var notifier = updateNotifier();

var options = nopt({
help: Boolean,
version: Boolean,
crop: Boolean,
delay: Number
}, {
h: '--help',
v: '--version',
c: '--crop',
d: '--delay'
});

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

function showHelp() {
console.log(multiline.stripIndent(function () {/*
Get screenshots of websites in different resolutions.
Expand Down Expand Up @@ -45,10 +57,14 @@ function showHelp() {
}

function generate(args, opts) {
var sizes = [];
var urls = [];
var pageres = new Pageres(opts)
.dest(process.cwd());

args.forEach(function (arg) {
pageres.src(arg.url, arg.sizes);
});

pageres(args, opts, function (err, items) {
pageres.run(function (err) {
if (err) {
if (err instanceof Error) {
throw err;
Expand All @@ -58,111 +74,94 @@ function generate(args, opts) {
}
}

args.forEach(function (arg) {
sizes = sizes.concat(arg.sizes);
urls = urls.concat(arg.url);
});
var i = pageres.sizes.length;
var s = pageres.stats.screenshots;
var u = pageres.stats.urls;

eachAsync(items, function (el, i, next) {
var stream = el.pipe(fs.createWriteStream(el.filename));
el.on('error', next);
stream.on('finish', next);
stream.on('error', next);
}, function (err) {
if (err) {
throw err;
}
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 get(args, options, cb) {
var ret = [];

var i = sizes.length;
var s = sizes.filter(function (el, i, self) {
return self.indexOf(el) === i;
}).length;
var u = urls.length;
eachAsync(args, function (arg, i, next) {
if (arg.url.length === 0) {
console.error(chalk.yellow('Specify a url'));
return showHelp();
}

if (arg.sizes.length === 0 && arg.keywords.length === 0) {
console.log('No sizes specified. Falling back to the ten most popular screen resolutions according to w3counter.');
}

if (arg.keywords.length > 0) {
arg.sizes = arg.sizes.concat(arg.keywords);
}

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'));
arg.url.forEach(function (el) {
ret.push({ url: el, sizes: arg.sizes });
});

next();
}, function (err) {
if (err) {
cb(err);
}

cb(null, ret);
});
}

function init(args, opts) {
var items = [];
function parse(args) {
var ret = [];

if (opts.help) {
args.forEach(function (arg) {
arg = arg._;

var url = _.uniq(arg.filter(/./.test, /\.|localhost/));
var sizes = _.uniq(arg.filter(/./.test, /^\d{3,4}x\d{3,4}$/i));
var keywords = _.difference(arg, url.concat(sizes));

ret.push({ url: url, sizes: sizes, keywords: keywords });
});

return ret;
}

function init(args, options) {
if (options.help || args.length === 0) {
return showHelp();
}

if (opts.version) {
if (options.version) {
return console.log(require('./package').version);
}

if (!args.some(function (arr) { return arr._ !== undefined; })) {
if (args.some(function (arr) { return arr._ === undefined; })) {
args = [{ _: args }];
}

eachAsync(args, function (el, i, next) {
el = el._;

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

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;
}

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 });
get(parse(args), options, function (err, items) {
if (err) {
throw err;
}

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

sudoBlock();

if (notifier.update) {
notifier.notify(true);
}

var opts = nopt({
help: Boolean,
version: Boolean,
crop: Boolean,
delay: Number
}, {
h: '--help',
v: '--version',
c: '--crop',
d: '--delay'
});

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

if (process.stdin.isTTY) {
init(args, opts);
init(args, options);
} else {
stdin(function (data) {
[].push.apply(args, data.trim().split('\n'));
init(args, opts);
init(args, options);
});
}
32 changes: 9 additions & 23 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,33 +66,19 @@ $ npm install --save pageres
### Usage

```js
var eachAsync = require('each-async');
var pageres = require('pageres');

var items = [{
url: 'yeoman.io',
sizes: ['480x320', '1024x768']
}, {
url: 'todomvc.com',
sizes: ['1280x1024', '1920x1080']
}];

pageres(items, { delay: 2 }, function (err, shots) {
var Pageres = require('pageres');

var pageres = new Pageres({ delay: 2 })
.src('yeoman.io', ['480x320', '1024x768', 'iphone5s'])
.src('todomvc.com', ['1280x1024', '1920x1080'])
.dest(__dirname)

pageres.run(function (err) {
if (err) {
throw err;
}

eachAsync(shots, function (el, i, next) {
var stream = el.pipe(fs.createWriteStream(el.filename));
stream.on('finish', next);
stream.on('error', next);
}, function (err) {
if (err) {
throw err;
}

console.log('done');
});
console.log('done');
});
```

Expand Down
66 changes: 37 additions & 29 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
'use strict';
var assert = require('assert');
var fs = require('fs');
var imageSize = require('image-size');
var concat = require('concat-stream');
var pageres = require('./index');

var def = [{
url: 'http://todomvc.com',
sizes: '1024x768'
}];
var Pageres = require('./index');

before(function () {
this.timeout(20000);
});

it('should generate screenshots', function (cb) {
var items = [{
url: 'yeoman.io',
sizes: ['480x320', '1024x768']
}, {
url: 'todomvc.com',
sizes: ['1280x1024', '1920x1080']
}];

pageres(items, function (err, streams) {
var pageres = new Pageres()
.src('yeoman.io', ['480x320', '1024x768', 'iphone5s'])
.src('todomvc.com', ['1280x1024', '1920x1080']);

pageres.run(function (err, streams) {
assert(!err, err);
assert.strictEqual(streams.length, 4);
assert.strictEqual(streams[0].filename, 'yeoman.io-480x320.png');
assert.strictEqual(streams.length, 5);
assert.strictEqual(streams[0].filename, 'todomvc.com-1280x1024.png');
assert.strictEqual(streams[4].filename, 'yeoman.io-320x568.png');

streams[0].once('data', function (data) {
assert(data.length > 1000);
Expand All @@ -35,12 +28,10 @@ it('should generate screenshots', function (cb) {
});

it('should remove special characters from the URL to create a valid filename', function (cb) {
var items = [{
url: 'http://www.microsoft.com/?query=pageres*|<>:"\\',
sizes: '1024x768'
}];
var pageres = new Pageres()
.src('http://www.microsoft.com/?query=pageres*|<>:"\\', ['1024x768']);

pageres(items, function (err, streams) {
pageres.run(function (err, streams) {
assert(!err, err);
assert.strictEqual(streams.length, 1);
assert.strictEqual(streams[0].filename, 'microsoft.com!query=pageres-1024x768.png');
Expand All @@ -49,7 +40,10 @@ it('should remove special characters from the URL to create a valid filename', f
});

it('should have a `delay` option', function (cb) {
pageres(def, {delay: 2}, function (err, streams) {
var pageres = new Pageres({ delay: 2 })
.src('http://todomvc.com', ['1024x768']);

pageres.run(function (err, streams) {
assert(!err, err);

var now = new Date();
Expand All @@ -62,7 +56,10 @@ it('should have a `delay` option', function (cb) {
});

it('should crop image using the `crop` option', function (cb) {
pageres(def, {crop: true}, function (err, streams) {
var pageres = new Pageres({ crop: true })
.src('http://todomvc.com', ['1024x768']);

pageres.run(function (err, streams) {
assert(!err, err);

streams[0].pipe(concat(function (data) {
Expand All @@ -75,12 +72,10 @@ it('should crop image using the `crop` option', function (cb) {
});

it('should support local relative files', function (cb) {
var items = [{
url: 'fixture/fixture.html',
sizes: ['1024x768']
}];
var pageres = new Pageres()
.src('fixture/fixture.html', ['1024x768']);

pageres(items, function (err, streams) {
pageres.run(function (err, streams) {
assert(!err, err);

assert.strictEqual(streams[0].filename, 'fixture!fixture.html-1024x768.png');
Expand All @@ -91,3 +86,16 @@ it('should support local relative files', function (cb) {
});
});
});

it('should save image', function (cb) {
var pageres = new Pageres()
.src('http://todomvc.com', ['1024x768'])
.dest(__dirname);

pageres.run(function (err) {
assert(!err);
assert(fs.existsSync('todomvc.com-1024x768.png'));
fs.unlinkSync('todomvc.com-1024x768.png');
cb();
});
});

0 comments on commit 71f275e

Please sign in to comment.