-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
cli.js
executable file
·209 lines (176 loc) · 4.97 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env node
import process from 'node:process';
import updateNotifier from 'update-notifier';
import subarg from 'subarg';
import sudoBlock from 'sudo-block';
import logSymbols from 'log-symbols';
import arrayUniq from 'array-uniq';
import arrayDiffer from 'array-differ';
import Pageres from 'pageres';
import parseHeaders from 'parse-headers';
import meow from 'meow';
const subargOptions = {
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
`, {
importMeta: import.meta,
flags: {
verbose: {
type: 'boolean',
shortFlag: 'v',
},
crop: {
type: 'boolean',
shortFlaf: 'c',
},
overwrite: {
type: 'boolean',
},
darkMode: {
type: 'boolean',
},
delay: {
type: 'number',
shortFlag: 'd',
default: 0,
},
scale: {
type: 'number',
default: 1,
},
},
});
async function generate(arguments_, options) {
const pageres = new Pageres({
incrementalName: !options.overwrite,
})
.destination(process.cwd());
for (const argument of arguments_) {
pageres.source(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, ...argument.keywords];
}
for (const url of argument.url) {
returnValue.push({
url,
sizes: argument.sizes,
options: argument.options,
});
}
}
return returnValue;
}
function parse(arguments_, globalOptions) {
const filename = [globalOptions.filename].flat();
delete globalOptions.filename;
return arguments_.map((argument, index) => {
const options = {
...globalOptions,
...argument,
};
argument = argument._;
delete options._;
if (options.cookie) {
options.cookies = [options.cookie].flat();
delete options.cookie;
}
if (options.header) {
options.headers = parseHeaders([options.header].flat().join('\n'));
delete options.header;
}
if (filename[index]) {
options.filename = filename[index];
}
if (options.hide) {
options.hide = [options.hide].flat();
}
const urlRegex = /https?:\/\/|localhost|\./;
const sizeRegex = /^\d{3,4}x\d{3,4}$/i;
const url = arrayUniq(argument.filter(x => urlRegex.test(x)));
const sizes = arrayUniq(argument.filter(x => sizeRegex.test(x)));
const keywords = arrayDiffer(argument, [...url, ...sizes]);
return {
url,
sizes,
keywords,
options,
};
});
}
async function init(arguments_, options) {
if (arguments_.length === 0) {
cli.showHelp(1);
}
const nonGroupedArgs = arguments_.filter(argument => !argument._);
// Filter grouped args
arguments_ = arguments_.filter(argument => argument._);
if (nonGroupedArgs.length > 0) {
arguments_.push({_: nonGroupedArgs});
}
const parsedArgs = parse(arguments_, options);
const items = get(parsedArgs);
await generate(items, options);
}
sudoBlock();
updateNotifier({pkg: cli.pkg}).notify();
const arguments_ = subarg(cli.input, subargOptions)._;
await init(arguments_, cli.flags);