-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·53 lines (48 loc) · 1.86 KB
/
Copy pathcli.js
File metadata and controls
executable file
·53 lines (48 loc) · 1.86 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
#!/usr/bin/node
const fs = require('fs');
const commander = require('commander');
const chromeLauncher = require('chrome-launcher');
const screenshot = require(__dirname + '/src/lib/screenshot');
const now = new Date();
const _default = {
html: `<center><img src="http://c.gumgum.com/ads/com/gumgum/icons/new/info_dark_3x.png" /><br /><a href="#">Hi ${process.env.USER}! Today is ${now}</a></center>`,
width: 600,
height: 400,
delay: 500,
filename: 'screenshot.png',
debug: process.env.DEBUG || false,
};
commander
.version('0.0.1')
.usage('[options] <file>')
.option('--html <s>', `html to screenshot, defaults to some basic html`)
.option('-W, --width <n>', `Viewport width, default=${_default.width}`, parseInt)
.option('-H, --height <n>', `Viewport height, default=${_default.width}`, parseInt)
.option('-D, --delay <n>', `Delay (ms) for screenshot, default=${_default.delay}ms`, parseInt)
.option('--debug', `Enables debug messages`)
.parse(process.argv);
const html = commander.html || _default.html;
const width = commander.width || _default.width;
const height = commander.height || _default.height;
const delay = commander.delay || _default.delay;
const filename = commander.args[0] || _default.filename;
const debug = commander.debug || _default.debug;
function launchHeadlessChrome() {
return chromeLauncher.launch({
port: 9222,
chromeFlags: [
`--window-size=${width},${height}`,
'--disable-gpu',
'--headless'
]
});
}
launchHeadlessChrome().then(chrome => {
screenshot(html, width, height, delay, debug).then((data) => {
debug && console.log('Writing to:', __dirname + '/' + filename);
fs.writeFile(__dirname + '/' + filename, new Buffer(data, 'base64'), () => {
chrome.kill();
process.exit();
});
});
});