forked from sindresorhus/pageres
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·109 lines (92 loc) · 2.89 KB
/
cli.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
#!/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 eachAsync = require('each-async');
var getres = require('getres');
var pageres = require('./index');
function showHelp() {
console.log('');
console.log('Get screenshots of websites in different resolutions.');
console.log('');
console.log('Specify urls and screen resolutions as arguments. Order doesn\'t matter.');
console.log('Screenshots are saved in the current directory.');
console.log('');
console.log(chalk.underline('Usage'));
console.log(' pageres <url> <resolution> [<resolution> <url> ...]');
console.log(' pageres [<url> <resolution> ...] < <file>');
console.log(' cat <file> | pageres [<url> <resolution> ...]');
console.log('');
console.log(chalk.underline('Example'));
console.log(' pageres todomvc.com yeoman.io 1366x768 1600x900');
console.log(' pageres 1366x768 < urls.txt');
console.log(' cat screen-resolutions.txt | pageres todomvc.com yeoman.io');
console.log('');
console.log('You can also pipe in a newline separated list of urls and screen resolutions which will get merged with the arguments.');
console.log('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) {
if (err) {
throw err;
}
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;
}
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'));
});
});
}
function init(args) {
if (opts.help) {
return showHelp();
}
if (opts.version) {
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 (sizes.length === 0) {
return getres(function (err, sizes) {
if (err) {
throw err;
}
console.log('No sizes specified. Falling back to the ten most popular screen resolutions according to w3counter:\n' + sizes.join(' '));
generate(urls, sizes);
});
}
generate(urls, sizes);
}
sudoBlock();
var opts = nopt({
help: Boolean,
version: Boolean
}, {
h: '--help',
v: '--version'
});
var args = opts.argv.remain;
if (process.stdin.isTTY) {
init(args);
} else {
stdin(function (data) {
[].push.apply(args, data.trim().split('\n'));
init(args);
});
}