forked from akhilmhdh/contributors-readme-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
86 lines (78 loc) · 3.08 KB
/
core.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
const templateParser = require("./utils/templateParser");
const templateBuilder = require("./utils/templateBuilder");
/**
* build a new array by joining given arrays
* @param {Array} values - priority based order
* @param {Array} prevContributors - contributors list of previous readme
* @param {Array} contributors - current contributors
* @param {Array} collaborators - current colloborators
* @param {Array} bots - current bots
* @returns {Array} prdered list
*/
function joinArray(values, prevContributors, contributors, collaborators, bots) {
let joinedArray = [];
values.forEach((category) => {
category = category.trim().toLowerCase();
switch (category) {
case "contributors":
joinedArray = joinedArray.concat(contributors);
break;
case "collaborators":
joinedArray = joinedArray.concat(collaborators);
break;
case "bots":
joinedArray = joinedArray.concat(bots);
break;
default:
prevContributors[category]
? joinedArray.push({
login: category,
avatar_url: prevContributors[category].url,
name: prevContributors[category].name,
})
: joinedArray.push({ login: category });
break;
}
});
return joinedArray;
}
exports.buildContent = async function (
templateContent,
contributors,
collaborators,
bots,
content,
octokit
) {
/**
* regex expression to parse the options passed inside the readme tags
* eg: <!-- readme:contributors,bots -start --!> anything inside this<!-- readme:contributors,bots -end --!>
* using the regex we get two groups return as
* type: contributors,bots
* use: to get the options passed
* content: anything that was inside the tag
* use: to reuse the html created inside the tah
*/
// get prev contributors in the readme
let prevReadmeContributorsTemplate = templateContent.match(
/<!--\s*readme:(?<type>[\s\S]*?)-start\s*-->(?<content>[\s\S]*?)<!--\s*readme:[\s\S]*?-end\s*-->/
);
const prevContributors = templateParser.parser(prevReadmeContributorsTemplate.groups.content);
const types = prevReadmeContributorsTemplate.groups.type.split(",");
const contributorsPool = joinArray(types, prevContributors, contributors, collaborators, bots);
let contributors_content = await templateBuilder.parser(
contributorsPool,
prevContributors,
prevReadmeContributorsTemplate.groups.type,
octokit
);
/**
* Build back the new template
* replace it with the old one
*/
const re = new RegExp(
`<!--\\s*readme:\\s*${prevReadmeContributorsTemplate.groups.type}\\s*-start\\s*-->([\\s\\S]*?)<!--\\s*readme:\\s*${prevReadmeContributorsTemplate.groups.type}\\s*-end\\s*-->`
);
const postprocess_content = content.replace(re, contributors_content);
return postprocess_content;
};