forked from axios/axios
/
injectContributorsList.js
55 lines (38 loc) · 1.47 KB
/
injectContributorsList.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
import fs from 'fs/promises';
import path from 'path';
import {renderContributorsList} from './contributors.js';
import asyncReplace from 'string-replace-async';
import {fileURLToPath} from "url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const injectContributors = async (infile, injector) => {
console.log(`Checking contributors sections in ${infile}`);
infile = path.resolve(__dirname, infile);
const content = String(await fs.readFile(infile));
const headerRE = /^#+\s+\[([-_\d.\w]+)].+?$/mig;
const contributorsRE = /^\s*### Contributors/mi;
let tag;
let index = 0;
const newContent = await asyncReplace(content, headerRE, async (match, nextTag, offset) => {
const releaseContent = content.slice(index, offset);
const hasContributorsSection = contributorsRE.test(releaseContent);
const currentTag = tag;
tag = nextTag;
index = offset + match.length;
if(currentTag) {
if (hasContributorsSection) {
console.log(`[${currentTag}]: found contribution section`);
} else {
const section = await injector(currentTag);
console.log(`[${currentTag}]: contribution section not found. Adding...`);
console.log(section);
return section + match;
}
}
return match;
});
await fs.writeFile(infile, newContent);
}
await injectContributors(
'../CHANGELOG.md',
(tag) => renderContributorsList(tag, true, path.resolve(__dirname, '../templates/contributors.hbs')
));