forked from sindresorhus/pageres-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·185 lines (153 loc) · 4.36 KB
/
Copy pathcli.js
File metadata and controls
executable file
·185 lines (153 loc) · 4.36 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env node
'use strict';
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',
'overwrite'
],
default: {
delay: 0,
scale: 1
},
alias: {
v: 'verbose',
c: 'crop',
d: 'delay'
}
};
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 todomvc.com yeoman.io 1366x768 1600x900 --overwrite
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
--overwrite Overwrite file if it exists
--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) {
const pageres = new Pageres({incrementalName: !options.overwrite})
.dest(process.cwd());
args.forEach(arg => {
pageres.src(arg.url, arg.sizes, arg.options);
});
if (options.verbose) {
pageres.on('warn', console.error.bind(console));
}
pageres.run()
.then(() => {
pageres.successMessage();
})
.catch(err => {
if (err.noStack) {
console.error(err.message);
process.exit(1);
} else {
throw err;
}
});
}
function get(args) {
const ret = [];
args.forEach(arg => {
if (arg.url.length === 0) {
console.error(logSymbols.warning, 'Specify a url');
process.exit(1);
}
if (arg.sizes.length === 0 && arg.keywords.length === 0) {
arg.sizes = ['1366x768'];
}
if (arg.keywords.length > 0) {
arg.sizes = arg.sizes.concat(arg.keywords);
}
arg.url.forEach(el => {
ret.push({
url: el,
sizes: arg.sizes,
options: arg.options
});
});
});
return ret;
}
function parse(args, globalOptions) {
return args.map(arg => {
const options = Object.assign({}, globalOptions, arg);
arg = arg._;
delete options._;
if (options.cookie) {
options.cookie = arrify(options.cookie);
}
if (options.header) {
options.header = parseHeaders(arrify(options.header).join('\n'));
}
// Plural makes more sense for programmatic options
options.cookies = options.cookie;
options.headers = options.header;
delete options.cookie;
delete options.header;
if (options.hide) {
options.hide = arrify(options.hide);
}
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,
sizes,
keywords,
options
};
});
}
function init(args, options) {
if (args.length === 0) {
cli.showHelp(1);
}
const nonGroupedArgs = args.filter(x => !x._);
// Filter grouped args
args = args.filter(x => x._);
if (nonGroupedArgs.length > 0) {
args.push({_: nonGroupedArgs});
}
const parsedArgs = parse(args, options);
const items = get(parsedArgs);
generate(items, options);
}
sudoBlock();
updateNotifier({pkg: cli.pkg}).notify();
init(subarg(cli.input, options)._, cli.flags);