-
Notifications
You must be signed in to change notification settings - Fork 4
/
update-readme.js
69 lines (54 loc) · 2.32 KB
/
update-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
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
/**
* This script updates the root README.md file with the list of all packages when a new one is
* published, or when the description/author of an existing package is updated.
*/
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "url";
import { dest, series, src } from "gulp";
import replace from "gulp-replace";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packagesDir = path.join(__dirname, "packages");
const readmePath = path.join(__dirname, "README.md");
function getPackageInfo(packageDir) {
const packageJsonPath = path.join(packageDir, "package.json");
if (!fs.existsSync(packageJsonPath)) return null;
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf-8"));
return {
name: packageJson.name,
description: packageJson.description,
author: packageJson.author.name,
authorUrl: packageJson.author.url,
};
};
function updateRootReadme() {
const packageInfos = fs
.readdirSync(packagesDir)
.map((dir) => path.join(packagesDir, dir))
.filter((dir) => fs.lstatSync(dir).isDirectory())
.map(getPackageInfo)
.filter((info) => info !== null);
const timelineListHead = `
### Timelines\n
Timeline | Contributor | Description
----------- | ----------- | -----------\n`;
const guidelinesHead = "## Using timelines from this repository\n\n";
let timelineList = "";
packageInfos.map((info) => {
const authorRender = info.authorUrl ? `[${info.author}](${info.authorUrl})` : info.author;
const packageName = info.name.replace(/^\@jspsych-timelines\//g, "");
const packageReadmeLink = `https://github.com/jspsych/jspsych-timelines/blob/main/packages/${packageName}/README.md`;
timelineList = timelineList.concat(
`[${packageName}](${packageReadmeLink}) | ${authorRender} | ${info.description ? info.description : "foo"} \n`
);
});
const timelineTable = [timelineListHead, timelineList, guidelinesHead];
function generateTimelineTable() {
return src(`${__dirname}/README.md`)
.pipe(replace(/### Timelines[\s\S]*?## Using timelines from this repository/g, timelineTable.join("")))
.pipe(dest(__dirname));
};
series(generateTimelineTable)();
};
export { updateRootReadme };