forked from jspsych/jspsych-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-readme.js
93 lines (78 loc) · 3.05 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* 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 pluginListHead = `
### Plugins\n
Plugin | Contributor | Description
----------- | ----------- | -----------\n`;
const extensionListHead = `
\n### Extensions\n
Extension | Contributor | Description
----------- | ----------- | -----------\n`;
const guidelinesHead = "## Guidelines for contributions\n";
let pluginList = "";
let extensionList = "";
packageInfos.map((info) => {
const packageReadmeLink = `https://github.com/jspsych/jspsych-contrib/blob/main/packages/${info.name}/README.md`;
if (info.name.match(/^\@jspsych-contrib\/plugin-/g)) {
const pluginName = info.name.replace(/^\@jspsych-contrib\/plugin-/g, "");
const authorRender =
info.authorUrl != "" ? `[${info.author}](${info.authorUrl})` : info.author;
pluginList = pluginList.concat(
`[${pluginName}](${packageReadmeLink}) | ${authorRender} | ${
info.description ? info.description : "foo"
} \n`
);
} else {
extensionList = extensionList.concat(
`[${info.name.replace(/^\@jspsych-contrib\/extension-/g, "")}](${packageReadmeLink}) | [${
info.author
}](${info.authorUrl}) | ${info.description ? info.description : "foo"} \n`
);
}
});
const pluginTable = [pluginListHead, pluginList, extensionListHead];
const extensionTable = [extensionListHead, extensionList, guidelinesHead];
function generatePluginTable() {
return src(`${__dirname}/README.md`)
.pipe(replace(/### Plugins[\s\S]*?### Extensions/g, pluginTable.join("")))
.pipe(dest(__dirname));
}
function generateExtensionTable() {
return src(`${__dirname}/README.md`)
.pipe(
replace(/### Extensions[\s\S]*?## Guidelines for contributions/g, extensionTable.join(""))
)
.pipe(dest(__dirname));
}
series(generatePluginTable, generateExtensionTable)();
}
export { updateRootReadme };