-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·75 lines (66 loc) · 1.9 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
#!/usr/bin/env node
const { Cluster } = require('puppeteer-cluster');
const chalk = require('chalk');
const mkdirp = require('mkdirp');
const sanitize = require('filenamify');
const { resolve } = require('path');
const { readFileSync } = require('fs');
const { config } = {
config: '.paparazzirc',
...require('minimist')(process.argv.slice(2)),
};
const fetchConfigFile = file => {
try {
return require(file);
} catch (error) {
try {
return JSON.parse(readFileSync(file, 'utf8'));
} catch (error) {
console.error(chalk.red(`error! ${error}`));
return {};
}
}
};
const getConfigOrFail = () => {
const conf = {
...fetchConfigFile(resolve(__dirname, '.defaultpaparazzirc')),
...fetchConfigFile(resolve(process.cwd(), config)),
};
if (!conf.routes || !conf.out) {
console.error(
chalk.red(`error! Could not find ${config} in the current folder`)
);
process.exit(1);
}
conf.out = resolve(process.cwd(), conf.out);
mkdirp.sync(conf.out);
return conf;
};
(async () => {
const { prefix, sizes, routes, screenshot, out } = getConfigOrFail();
const cluster = await Cluster.launch({
concurrency: Cluster.CONCURRENCY_CONTEXT,
maxConcurrency: require('os').cpus().length || 2,
});
await cluster.task(async ({ page, data }) => {
const { route } = data;
console.log(`started ${route}`);
await page.goto(prefix ? [prefix, route].join('/') : route);
for (let size in sizes) {
try {
await page.setViewport(sizes[size]);
await page.screenshot({
path: `${out}/${size}-${sanitize(route, { replacement: '-' })}.png`,
...screenshot,
});
console.log(chalk.blue(`done ${route} – ${size}`));
} catch (error) {
console.error(chalk.red(`error! ${error}`));
}
}
console.log(chalk.green(`done ${route} – ALL`));
});
await Promise.all(routes.map(route => cluster.queue({ route, prefix })));
await cluster.idle();
await cluster.close();
})();