Skip to content

Commit 1eadc61

Browse files
committed
feat(bin): add bin to render pdfs / pngs / etc
1 parent d68583d commit 1eadc61

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

bin/render-vendor

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#! /usr/bin/env node
2+
3+
const _ = require('lodash');
4+
const program = require('commander');
5+
const { version } = require('../package.json');
6+
7+
const COMMANDS = {
8+
kill: 'kill spawned renderers',
9+
list: 'list spawned renderers'
10+
}
11+
12+
let urls = [];
13+
let isCommand = process.argv[0].endsWith('node') &&
14+
process.argv[1].endsWith('render-vendor') &&
15+
Object.keys(COMMANDS).includes(process.argv[2]) ||
16+
Object.keys(COMMANDS).includes(process.argv[1]);
17+
18+
program.version(version);
19+
20+
_.forEach(COMMANDS, (description, name) => {
21+
program.command(name, description)
22+
});
23+
24+
program
25+
.option('-f, --format [value]', 'output format; default = \'pdf\'')
26+
.arguments('[urls...]')
27+
.action((_urls) => {
28+
urls = [...urls, ..._urls];
29+
})
30+
.parse(process.argv);
31+
32+
if (!isCommand) {
33+
if (urls.length === 0) {
34+
process.exit(0);
35+
}
36+
37+
require('./render-vendor-print').call(program, urls);
38+
}

bin/render-vendor-kill

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /usr/bin/env node
2+
3+
const childProcess = require('child_process');
4+
let killScriptComponents = [
5+
'ps aux',
6+
'grep render-vendor',
7+
'grep phantomjs',
8+
'grep -v grep',
9+
'awk \'{ print $2 }\'',
10+
'xargs kill'
11+
]
12+
13+
try {
14+
childProcess.execSync(killScriptComponents.join(' | '));
15+
} catch (err) {
16+
// nop; nothing found
17+
}
18+
19+
process.exit(0);

bin/render-vendor-list

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#! /usr/bin/env node
2+
3+
const childProcess = require('child_process');
4+
let listScriptComponents = [
5+
'ps aux',
6+
'grep render-vendor',
7+
'grep phantomjs',
8+
'grep -v grep',
9+
]
10+
11+
try {
12+
let stdout = childProcess.execSync(listScriptComponents.join(' | '));
13+
14+
console.log(stdout.toString());
15+
} catch (err) {
16+
// nop; nothing found
17+
}
18+
19+
process.exit(0);

bin/render-vendor-print

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#! /usr/bin/env node
2+
3+
const Listr = require('listr');
4+
const { Renderer } = require('../renderer');
5+
6+
module.exports = function(urls) {
7+
process.on('uncaughtException', () => {
8+
Renderer.destroy();
9+
process.exit(1);
10+
});
11+
12+
let tasks = new Listr([{
13+
title: `Booting a ${Renderer.rendererConstructor.name}`,
14+
task: () => Renderer.renderer.boot()
15+
}, {
16+
title: 'Rendering webpages',
17+
task: (ctx, task) => new Listr(urls.map((url) => {
18+
return makeRenderUrlTaskObjectFor.call(this, url, task);
19+
}), {
20+
concurrent: true,
21+
exitOnError: false
22+
})
23+
}, {
24+
title: `Shutting down ${Renderer.renderer.constructor.name}`,
25+
task: () => Renderer.destroy()
26+
}], {
27+
exitOnError: false
28+
});
29+
30+
tasks.run()
31+
.then(() => process.exit(0))
32+
.catch(() => process.exit(1));
33+
}
34+
35+
function makeRenderUrlTaskObjectFor(url, loadTask) {
36+
return {
37+
title: `${url}: Loading...`,
38+
task: (ctx, task) => Renderer.load(url)
39+
.then((page) => {
40+
let format = this.format || 'pdf';
41+
42+
task.title = `${url}: Rendering ${format}...`;
43+
44+
return page.render({ format });
45+
})
46+
.then((filepath) => {
47+
loadTask.title = [
48+
loadTask.title,
49+
` ✔ ${url}: Rendered to ${filepath}`
50+
].join('\n');
51+
})
52+
};
53+
}

0 commit comments

Comments
 (0)