2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-08 17:22:34 +03:00
Files
axios/bin/injectContributorsList.js
T
2022-12-23 16:49:47 +02:00

56 lines
1.5 KiB
JavaScript

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')
));