-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathregenerate-readmes.mts
More file actions
59 lines (47 loc) · 2.44 KB
/
regenerate-readmes.mts
File metadata and controls
59 lines (47 loc) · 2.44 KB
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
import { writeFileSync, readFileSync, readdirSync, statSync, existsSync } from 'fs';
import { strictEqual, ok } from 'assert'
const template = (path: string, description: string) => `# ${path}
${description}
## Installation
1. Install a userscript addon for your browser. Violentmonkey is recommended, but other ones such as Greasemonkey or Tampermonkey should also work although they're untested.
[Violentmonkey for Mozilla Firefox](https://addons.mozilla.org/en-US/firefox/addon/violentmonkey/)
[Violentmonkey for Microsoft Edge](https://microsoftedge.microsoft.com/addons/detail/eeagobfjdenkkddmbclomhiblgggliao)
[Violentmonkey for Google Chrome](https://chrome.google.com/webstore/detail/violent-monkey/jinjaccalgkegednnccohejagnlnfdag)
2. [Click Here](https://github.com/PotcFdk/userscripts/raw/master/${path}/${path}.user.js) to open \`https://github.com/PotcFdk/userscripts/raw/master/${path}/${path}.user.js\`.
3. Click \`Install\`.`;
const RGX_DESCRIPTION = /^\/\/ @description\s+(.+)$/m;
function getDescription (path: string) {
const metaJs = readFileSync (`${path}/${path}.user.js`, 'utf8');
let match = RGX_DESCRIPTION.exec(metaJs);
if (match) {
const description = match[1];
return description;
}
}
const RGX_MARKDOWN_IMG = /!\[[^\]]+\]\(([^\)]+)\)/g;
function fixupImagePaths (markdown: string) {
return markdown.replaceAll(RGX_MARKDOWN_IMG, (match, name) => match.replace (name, 'info/' + name));
}
readdirSync('.')
.filter(e => statSync(e).isDirectory())
.filter(name => existsSync(`${name}/${name}.user.js`))
.forEach(name => {
let description = getDescription(name);
ok(description);
strictEqual(typeof description, 'string');
ok(description.length > 6)
if (existsSync(`${name}/info`) && statSync(`${name}/info/info.md`).isFile()) {
const info = readFileSync(`${name}/info/info.md`, 'utf8');
description += `\n\n${fixupImagePaths(info).trim()}`;
}
writeFileSync(`${name}/README.md`, template(name, description), 'utf8');
});
writeFileSync('README.md', `# userscripts
A collection of small/simple user scripts.
` + readdirSync('.')
.filter(e => statSync(e).isDirectory())
.filter(name => existsSync(`${name}/${name}.user.js`))
.map(name => ({ name, description: getDescription(name) }))
.map(({ name, description }) => `### [${name}](${name})\n${description}`)
.join('\n') + '\n',
'utf8');