-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
51 lines (39 loc) · 1.55 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
'use strict';
const core = require('@actions/core');
const path = require('path');
const http = require('https');
const fs = require('fs');
const config = require('./src/config');
const { notBlankOrElse } = require('./src/utils');
async function createSnapshot(url, filePath, fileName, fileExtension) {
const imagePath = path.join(filePath, `${fileName}.${fileExtension}`);
console.log(`Generating screenshot with parameters: url=${url}, file=${imagePath}\n`);
if (!fs.existsSync(filePath)) {
fs.mkdirSync(filePath);
}
const image = fs.createWriteStream(imagePath);
await http.get(url, resp => {
resp.pipe(image);
});
return imagePath;
}
async function run() {
const url = core.getInput('url', { required: true });
const width = notBlankOrElse(core.getInput('width'), config.width);
const height = notBlankOrElse(core.getInput('height'), config.height);
const fileName = notBlankOrElse(core.getInput('name'), config.name);
const filePath = notBlankOrElse(core.getInput('path'), config.path);
const fileExtension = notBlankOrElse(core.getInput('extension'), config.extension);
const target = `${config.url}?url=${url}&width=${width}&height=${height}`;
try {
const imagePath = await createSnapshot(target, filePath, fileName, fileExtension);
core.info(`Storing chart image by path: ${imagePath}`);
core.setOutput('image', imagePath);
} catch (e) {
core.setFailed(`Cannot create chart image by path: ${filePath}/${fileName}, message: ${e.message}`);
}
}
module.exports = run;
if (require.main === module) {
run();
}