Skip to content

Commit

Permalink
ES2015ify and require Node.js 4
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 7, 2017
1 parent b41b03f commit 1dfa478
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* text=auto
*.js text eol=lf
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ language: node_js
node_js:
- '6'
- '4'
- '0.12'
162 changes: 73 additions & 89 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
#!/usr/bin/env node
'use strict';
var multiline = require('multiline');
var updateNotifier = require('update-notifier');
var subarg = require('subarg');
var sudoBlock = require('sudo-block');
var logSymbols = require('log-symbols');
var arrayUniq = require('array-uniq');
var arrayDiffer = require('array-differ');
var arrify = require('arrify');
var objectAssign = require('object-assign');
var Pageres = require('pageres');
var parseHeaders = require('parse-headers');
var meow = require('meow');

var options = {
const updateNotifier = require('update-notifier');
const subarg = require('subarg');
const sudoBlock = require('sudo-block');
const logSymbols = require('log-symbols');
const arrayUniq = require('array-uniq');
const arrayDiffer = require('array-differ');
const arrify = require('arrify');
const Pageres = require('pageres');
const parseHeaders = require('parse-headers');
const meow = require('meow');

const options = {
boolean: [
'verbose',
'crop'
Expand All @@ -29,48 +27,45 @@ var options = {
}
};

var cli = meow(multiline(function () {
/*
Capture screenshots of websites in various resolutions.
Specify urls and screen resolutions as arguments. Order doesn't matter.
Group arguments with [ ]. Options defined inside a group will override the outer ones.
Screenshots are saved in the current directory.
Usage
pageres <url> <resolution>
pageres [ <url> <resolution> ] [ <url> <resolution> ]
Example
pageres todomvc.com yeoman.io 1366x768 1600x900
pageres [ yeoman.io 1366x768 1600x900 --no-crop ] [ todomvc.com 1024x768 480x320 ] --crop
pageres todomvc.com 1024x768 --filename='<%= date %> - <%= url %>'
pageres yeoman.io 1366x768 --selector='.page-header'
pageres unicorn.html 1366x768
Options
-v, --verbose Verbose output
-c, --crop Crop to the set height
-d, --delay=<seconds> Delay screenshot capture
--filename=<template> Custom filename
--selector=<element> Capture DOM element
--hide=<element> Hide DOM element (Can be set multiple times)
--cookie=<cookie> Browser cookie (Can be set multiple times)
--header=<string> Custom HTTP request header (Can be set multiple times)
--username=<username> Username for HTTP auth
--password=<password> Password for HTTP auth
--scale=<number> Scale webpage
--format=<string> Image format
--css=<string> Apply custom CSS
<url> can also be a local file path.
*/}), options);
const cli = meow(`
Specify urls and screen resolutions as arguments. Order doesn't matter.
Group arguments with [ ]. Options defined inside a group will override the outer ones.
Screenshots are saved in the current directory.
Usage
pageres <url> <resolution>
pageres [ <url> <resolution> ] [ <url> <resolution> ]
Example
pageres todomvc.com yeoman.io 1366x768 1600x900
pageres [ yeoman.io 1366x768 1600x900 --no-crop ] [ todomvc.com 1024x768 480x320 ] --crop
pageres todomvc.com 1024x768 --filename='<%= date %> - <%= url %>'
pageres yeoman.io 1366x768 --selector='.page-header'
pageres unicorn.html 1366x768
Options
-v, --verbose Verbose output
-c, --crop Crop to the set height
-d, --delay=<seconds> Delay screenshot capture
--filename=<template> Custom filename
--selector=<element> Capture DOM element
--hide=<element> Hide DOM element (Can be set multiple times)
--cookie=<cookie> Browser cookie (Can be set multiple times)
--header=<string> Custom HTTP request header (Can be set multiple times)
--username=<username> Username for HTTP auth
--password=<password> Password for HTTP auth
--scale=<number> Scale webpage
--format=<string> Image format
--css=<string> Apply custom CSS
<url> can also be a local file path.
`, options);

function generate(args, options) {
var pageres = new Pageres()
const pageres = new Pageres()
.dest(process.cwd());

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

Expand All @@ -79,10 +74,10 @@ function generate(args, options) {
}

pageres.run()
.then(function () {
.then(() => {
pageres.successMessage();
})
.catch(function (err) {
.catch(err => {
if (err.noStack) {
console.error(err.message);
process.exit(1);
Expand All @@ -93,23 +88,23 @@ function generate(args, options) {
}

function get(args) {
var ret = [];
const ret = [];

args.forEach(function (arg) {
if (!arg.url.length) {
args.forEach(arg => {
if (arg.url.length === 0) {
console.error(logSymbols.warning, 'Specify a url');
process.exit(1);
}

if (!arg.sizes.length && !arg.keywords.length) {
if (arg.sizes.length === 0 && arg.keywords.length === 0) {
arg.sizes = ['1366x768'];
}

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

arg.url.forEach(function (el) {
arg.url.forEach(el => {
ret.push({
url: el,
sizes: arg.sizes,
Expand All @@ -122,8 +117,8 @@ function get(args) {
}

function parse(args, globalOptions) {
return args.map(function (arg) {
var options = objectAssign({}, globalOptions, arg);
return args.map(arg => {
const options = Object.assign({}, globalOptions, arg);

arg = arg._;
delete options._;
Expand All @@ -136,7 +131,7 @@ function parse(args, globalOptions) {
options.header = parseHeaders(arrify(options.header).join('\n'));
}

// plural makes more sense for programmatic options
// Plural makes more sense for programmatic options
options.cookies = options.cookie;
options.headers = options.header;
delete options.cookie;
Expand All @@ -146,24 +141,17 @@ function parse(args, globalOptions) {
options.hide = arrify(options.hide);
}

var urlRegex = /https?:\/\/|localhost|\./;
var sizeRegex = /^\d{3,4}x\d{3,4}$/i;

var url = arrayUniq(arg.filter(function (a) {
return urlRegex.test(a);
}));

var sizes = arrayUniq(arg.filter(function (a) {
return sizeRegex.test(a);
}));

var keywords = arrayDiffer(arg, url.concat(sizes));
const urlRegex = /https?:\/\/|localhost|\./;
const sizeRegex = /^\d{3,4}x\d{3,4}$/i;
const url = arrayUniq(arg.filter(x => urlRegex.test(x)));
const sizes = arrayUniq(arg.filter(x => sizeRegex.test(x)));
const keywords = arrayDiffer(arg, url.concat(sizes));

return {
url: url,
sizes: sizes,
keywords: keywords,
options: options
url,
sizes,
keywords,
options
};
});
}
Expand All @@ -173,21 +161,17 @@ function init(args, options) {
cli.showHelp(1);
}

var nonGroupedArgs = args.filter(function (arg) {
return !arg._;
});
const nonGroupedArgs = args.filter(x => !x._);

// filter grouped args
args = args.filter(function (arg) {
return arg._;
});
// Filter grouped args
args = args.filter(x => x._);

if (nonGroupedArgs.length) {
if (nonGroupedArgs.length > 0) {
args.push({_: nonGroupedArgs});
}

var parsedArgs = parse(args, options);
var items = get(parsedArgs);
const parsedArgs = parse(args, options);
const items = get(parsedArgs);

generate(items, options);
}
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"pageres": "cli.js"
},
"engines": {
"node": ">=0.12.0"
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
Expand Down Expand Up @@ -66,8 +66,6 @@
"arrify": "^1.0.0",
"log-symbols": "^1.0.0",
"meow": "^3.5.0",
"multiline": "^1.0.0",
"object-assign": "^4.0.1",
"pageres": "^4.1.0",
"parse-headers": "^2.0.0",
"subarg": "^1.0.0",
Expand All @@ -77,7 +75,9 @@
"devDependencies": {
"ava": "*",
"execa": "^0.4.0",
"path-exists": "^2.1.0",
"xo": "*"
},
"xo": {
"esnext": true
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,4 @@ You can use the most popular resolutions for your site with `pageres` by followi

## License

MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)
13 changes: 6 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import fs from 'fs';
import {spawn} from 'child_process';
import execa from 'execa';
import test from 'ava';
import pathExists from 'path-exists';
import {version as pkgVersion} from '../package.json';
import {version as pkgVersion} from '../package';

process.chdir(__dirname);

test('generate screenshot', async t => {
await execa('../cli.js', ['yeoman.io', '320x240']);

t.true(pathExists.sync('yeoman.io-320x240.png'));
t.true(fs.existsSync('yeoman.io-320x240.png'));
fs.unlinkSync('yeoman.io-320x240.png');
});

Expand All @@ -20,7 +19,7 @@ test.cb('remove temporary files on cancel', t => {
const cp = spawn('../cli.js', ['yeoman.io', '320x240']);

cp.on('exit', () => {
t.false(pathExists.sync('yeoman.io-320x240.png'));
t.false(fs.existsSync('yeoman.io-320x240.png'));
t.end();
});

Expand All @@ -36,19 +35,19 @@ test('show error if no url is specified', t => {
test('use 1366x768 as default resolution', async t => {
await execa('../cli.js', ['yeoman.io']);

t.true(pathExists.sync('yeoman.io-1366x768.png'));
t.true(fs.existsSync('yeoman.io-1366x768.png'));
fs.unlinkSync('yeoman.io-1366x768.png');
});

test('generate screenshots using keywords', async t => {
await execa('../cli.js', ['yeoman.io', 'iphone5s']);

t.true(pathExists.sync('yeoman.io-320x568.png'));
t.true(fs.existsSync('yeoman.io-320x568.png'));
fs.unlinkSync('yeoman.io-320x568.png');
});

test('show help screen', async t => {
t.regex(await execa.stdout('../cli.js', ['--help']), /Capture screenshots of websites in various resolutions./);
t.regex(await execa.stdout('../cli.js', ['--help']), /pageres <url>/);
});

test('show version', async t => {
Expand Down

0 comments on commit 1dfa478

Please sign in to comment.