This repository has been archived by the owner on Sep 11, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
55 lines (47 loc) · 1.83 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
const core = require('@actions/core');
const github = require('@actions/github');
const fs = require('fs');
const path = require('path');
const readmeBox = require('readme-box').ReadmeBox;
const chunk = require('chunk');
const generateCell = (cell) => {
const objectFieldNames = JSON.parse(core.getInput('object-field-names'));
let htmlCell = core.getInput('html-cell');
objectFieldNames.forEach((name) => {
htmlCell = htmlCell.replace(new RegExp(`{{ ${name} }}`), cell[name]);
});
console.log(objectFieldNames);
console.log(htmlCell);
return htmlCell;
}
const generateRow = (columns, row) => {
const cells = row.map((cell) => generateCell(cell));
if (cells.length < columns) {
cells.push('<td></td>'.repeat(columns - cells.length));
}
return `<tr>${cells.join('')}</tr>`;
};
(async () => {
const githubToken = core.getInput('github-token');
const filePath = path.join(process.env.GITHUB_WORKSPACE, core.getInput('json-file-path'));
const columns = core.getInput('columns');
const data = fs.readFileSync(filePath, 'utf8');
const json = JSON.parse(data);
const fileToUsePath = core.getInput('file-to-use');
const sectionName = core.getInput('section-name');
try {
const content = chunk(json, columns).map((row) => generateRow(columns, row));
const table = `<table width="100%">${content.join('')}</table>`;
await readmeBox.updateSection(table, {
owner: process.env.GITHUB_REPOSITORY.split('/')[0],
repo: process.env.GITHUB_REPOSITORY.split('/')[1],
branch: process.env.GITHUB_REF.split('/')[2],
token: githubToken,
section: sectionName,
path: fileToUsePath,
});
} catch (error) {
core.setFailed(JSON.stringify(error));
console.log(error);
}
})();