-
Notifications
You must be signed in to change notification settings - Fork 26
/
patch-readme.js
26 lines (22 loc) · 1.01 KB
/
patch-readme.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
import fs from 'node:fs';
import { URL } from 'node:url';
/* paths to all required files */
const packageDist = new URL('dist/fastify-decorators/', import.meta.url);
const packageJsonPath = new URL('package.json', packageDist);
const readmePath = new URL('README.md', packageDist);
/* urls for patches */
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
const baseUrl = `https://github.com/L2jLiga/fastify-decorators/blob/v${packageJson.version}/`;
const rawUrl = `https://raw.githubusercontent.com/L2jLiga/fastify-decorators/v${packageJson.version}/`;
/* patching */
const content = fs
.readFileSync(readmePath, 'utf8')
.replace(/]: (\.\/.+)/g, (match, file) => `]: ${baseUrl}${makeUrl(baseUrl, file)}`)
.replace(/(!\[.+])\((\.\/[a-zA-Z/\\.-_0-9]+)\)/g, (match, imageName, imageFile) => `${imageName}(${makeUrl(rawUrl, imageFile)})`);
fs.writeFileSync(readmePath, content, 'utf8');
/* helpers */
function makeUrl(uri, filepath) {
const url = new URL(uri);
url.href += filepath;
return url.href;
}