-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·102 lines (86 loc) · 3.06 KB
/
index.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
#!/usr/bin/env node
const puppeteer = require('puppeteer');
const devices = require('puppeteer/DeviceDescriptors');
const Confirm = require('prompt-confirm');
const fs = require('fs');
const parameters = process.argv.slice(2);
let numImages = 0;
//Required inputs
const inputtedURL = parameters[0];
let selectedDevices = parameters[1];
//Modifier inputs
const modifiers = parameters.slice(2);
let fullScreenStatus = false;
let forceYes = false;
//Check user has provided enough information
if (process.argv.length <= 2 || inputtedURL === undefined || selectedDevices === undefined) {
console.log('Incorrect Usage. Please use the following format:\n npm run generate-screenshots https://www.google.com "iPhone 6, iPhone 5"');
process.exit(1);
}
//Check for screenshots folder, if not found then create it
const dir = './generated-screenshots';
if (!fs.existsSync(dir)){
console.log(`Creating folder ${dir}...`);
fs.mkdirSync(dir);
}
//Handle device input
if ( selectedDevices === "all" ) {
numImages = devices.length;
} else {
selectedDevices = selectedDevices.split(/,\s*/);
numImages = selectedDevices.length;
}
//Handle modifier input
for (let modifier in modifiers) {
switch (modifiers[modifier]) {
case 'fullscreen':
fullScreenStatus = true;
break;
case 'force-yes':
forceYes = true;
break;
default:
console.log(`Incorrect modifier provided: ${modifiers[modifier]}`);
process.exit(0);
}
}
let generate = async () => {
const browser = await puppeteer.launch();
let page = await browser.newPage();
let currentDevice;
try {
if (selectedDevices !== "all") {
for (let i=0; i<selectedDevices.length; i++) {
currentDevice = selectedDevices[i]
await page.emulate( devices[ currentDevice ] );
await page.goto(inputtedURL);
await page.screenshot({path: 'generated-screenshots/' + currentDevice + '.jpg', fullPage: fullScreenStatus});
console.log('Created file: ' + currentDevice + '.jpg' + ' inside folder: ' + dir);
}
} else {
for (let i=0; i<devices.length; i++) {
currentDevice = devices[i];
await page.emulate( currentDevice );
await page.goto(inputtedURL);
await page.screenshot({path: 'generated-screenshots/' + currentDevice.name + '.jpg', fullPage: fullScreenStatus});
console.log('Created file: ' + currentDevice.name + '.jpg' + ' inside folder: ' + dir);
}
}
} catch (e) {
console.log(e);
process.exitCode = 1;
}
browser.close();
};
//Prompt user for confirmation - as long as force-yes isn't provided as a modifier
if (forceYes) {
generate();
} else {
new Confirm(`This will generate ${numImages} images, are you sure you wish to proceed`).ask(function(answer) {
if (answer) {
generate();
} else {
process.exitCode = 0;
}
});
}