forked from sindresorhus/pageres-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·185 lines (152 loc) · 4.72 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
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',
'darkMode'
],
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> ]
Examples
$ pageres https://sindresorhus.com https://example.com 1366x768 1600x900
$ pageres https://sindresorhus.com https://example.com 1366x768 1600x900 --overwrite
$ pageres [ https://example.com 1366x768 1600x900 --no-crop ] [ https://sindresorhus.com 1024x768 480x320 ] --crop
$ pageres https://sindresorhus.com 1024x768 --filename='<%= date %> - <%= url %>'
$ pageres https://example.com 1366x768 --selector='.page-header'
$ pageres unicorn.html 1366x768
Options
--verbose, -v Verbose output
--crop, -c Crop to the set height
--delay=<seconds>, -d 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
--darkMode Emulate preference of dark color scheme
<url> can also be a local file path
`, options);
async function generate(args, options) {
const pageres = new Pageres({
incrementalName: !options.overwrite,
launchOptions: {args: ['--no-sandbox', '--disable-setuid-sandbox']}
}).dest(process.cwd());
for (const argument of args) {
pageres.src(argument.url, argument.sizes, argument.options);
}
await pageres.run();
pageres.successMessage();
}
function get(args) {
const returnValue = [];
for (const argument of args) {
if (argument.url.length === 0) {
console.error(logSymbols.warning, 'Specify a url');
process.exit(1);
}
if (argument.sizes.length === 0 && argument.keywords.length === 0) {
argument.sizes = ['1366x768'];
}
if (argument.keywords.length > 0) {
argument.sizes = argument.sizes.concat(argument.keywords);
}
for (const url of argument.url) {
returnValue.push({
url,
sizes: argument.sizes,
options: argument.options
});
}
}
return returnValue;
}
function parse(args, globalOptions) {
const filename = arrify(globalOptions.filename);
delete globalOptions.filename;
return args.map((arg, index) => {
const options = {...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'));
}
if (filename[index]) {
options.filename = filename[index];
}
// 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
};
});
}
async 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);
await generate(items, options);
}
sudoBlock();
updateNotifier({pkg: cli.pkg}).notify();
init(subarg(cli.input, options)._, cli.flags).catch(error => {
console.error(error);
process.exit(1);
});