forked from open-wa/wa-automate-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease-image.js
118 lines (118 loc) · 4.01 KB
/
release-image.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
"use strict";
/**
* @license
* Copyright (c) 2019 The Polymer Project Authors. All rights reserved.
* This code may only be used under the BSD style license found at
* http://polymer.github.io/LICENSE.txt
* The complete set of authors may be found at
* http://polymer.github.io/AUTHORS.txt
* The complete set of contributors may be found at
* http://polymer.github.io/CONTRIBUTORS.txt
* Code distributed by Google as part of the polymer project is also
* subject to an additional IP rights grant found at
* http://polymer.github.io/PATENTS.txt
*/
Object.defineProperty(exports, "__esModule", { value: true });
const parseChangelog = require("changelog-parser");
const commandLineArgs = require("command-line-args");
const marked = require("marked");
const puppeteer = require("puppeteer");
const fs = require("fs");
const util = require("util");
const path = require("path");
const readFile = util.promisify(fs.readFile);
const optionDefinitions = [
{ name: 'file', type: String, defaultOption: true },
{ name: 'version', type: String },
];
const stripMarkdown = (text) => {
var str = String(text).replace(/__|\*|\#|(?:\[([^\]]*)\]\([^)]*\))/gm, '$1');
return str;
};
exports.run = async () => {
const options = commandLineArgs(optionDefinitions);
const filename = options.file || 'CHANGELOG.md';
const version = options.version;
const packageJson = JSON.parse(await readFile(path.join(path.dirname(filename), 'package.json'), { encoding: 'utf-8' }));
const packageName = packageJson.name;
console.log(`Reading ${packageName} release ${version} from ${filename}`);
const changelog = (await parseChangelog({
filePath: filename,
removeMarkdown: true,
}));
const release = await getRelease(changelog, version);
if (release === undefined) {
throw new Error('no release found');
}
const body = marked(release.body);
// colors taken from https://github.com/dracula/dracula-theme
const html = `
<!doctype html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400|Roboto+Mono:300|Roboto+Slab:400,700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Open Sans', sans-serif;
font-weight: 300;
background: #282a36;
color: #f8f8f2;
margin: 1.5em;
}
h1, h2, h3 {
font-family: 'Roboto Slab', serif;
color: #ff79c6;
font-weight: 400;
}
span.name {
color: #8be9fd;
}
code {
font-family: 'Roboto Mono', monospace;
font-size: 14px;
background: #44475a;
border-radius: 3px;
padding: 0 4px;
}
a {
color: inherit;
text-decoration: none;
}
</style>
</head>
<body>
<h2><span class="name">${packageName}</span> ${stripMarkdown(release.title)}</h2>
${body}
</body>
</html>
`;
const browser = await puppeteer.launch({ headless: true });
const page = await browser.newPage();
await page.setViewport({ width: 800, height: 800, deviceScaleFactor: 2 });
await page.setContent(html);
await page.evaluate(`document.fonts.ready`);
const bounds = await page.evaluate(`
document.documentElement.getBoundingClientRect().toJSON()
`);
await page.screenshot({
path: 'release.png',
encoding: 'binary',
type: 'png',
clip: {
x: bounds.x,
y: bounds.y,
width: bounds.width,
height: bounds.height,
},
});
console.log('Wrote screenshot to release.png');
await browser.close();
process.exit();
};
const latestVersion = {};
const getRelease = async (changelog, version = latestVersion) => {
return changelog.versions.find((r) => (r.version !== null && version === latestVersion) ||
r.version === version ||
(r.version === null && version === undefined));
};
exports.run();