diff options
author | Markus Hatvan <markus_hatvan@aon.at> | 2021-09-18 11:15:25 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-18 11:15:25 +0200 |
commit | d4101a48b3eee8b1fb177831aa02a4b4fbec2588 (patch) | |
tree | c92f2fbe91197fde8589207463d0d6526b4ff76b /scripts/link-readme.ts | |
parent | 5.6.3-nightly.6 [skip ci] (diff) | |
download | ferdium-app-d4101a48b3eee8b1fb177831aa02a4b4fbec2588.tar.gz ferdium-app-d4101a48b3eee8b1fb177831aa02a4b4fbec2588.tar.zst ferdium-app-d4101a48b3eee8b1fb177831aa02a4b4fbec2588.zip |
chore: convert various files from JS to TS (#1959)
Diffstat (limited to 'scripts/link-readme.ts')
-rw-r--r-- | scripts/link-readme.ts | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/scripts/link-readme.ts b/scripts/link-readme.ts new file mode 100644 index 000000000..77b384ff4 --- /dev/null +++ b/scripts/link-readme.ts | |||
@@ -0,0 +1,63 @@ | |||
1 | /** | ||
2 | * Script that automatically creates links to issues and users inside README.md | ||
3 | * | ||
4 | * e.g. "#123" => "[#123](https://github.com/getferdi/ferdi/issues/123)" | ||
5 | * and "franz/#123" => "[franz#123](https://github.com/meetfranz/franz/issues/123)" | ||
6 | * and "@abc" => "[@abc](https://github.com/abc)" | ||
7 | */ | ||
8 | |||
9 | import fs from 'fs-extra'; | ||
10 | import path from 'path'; | ||
11 | |||
12 | console.log('Linking issues and PRs in README.md'); | ||
13 | |||
14 | const readmepath = path.join(__dirname, '..', 'README.md'); | ||
15 | |||
16 | // Read README.md | ||
17 | let readme = fs.readFileSync(readmepath, 'utf-8'); | ||
18 | |||
19 | let replacements = 0; | ||
20 | |||
21 | // Replace Franz issues | ||
22 | // Regex matches strings that don't begin with a "[", i.e. are not already linked | ||
23 | // followed by a "franz#" and digits to indicate | ||
24 | // a GitHub issue, and not ending with a "]" | ||
25 | readme = readme.replace(/(?<!\[)franz#\d+(?![\d\]])/gi, match => { | ||
26 | const issueNr = match.replace('franz#', ''); | ||
27 | replacements += 1; | ||
28 | return `[franz#${issueNr}](https://github.com/meetfranz/franz/issues/${issueNr})`; | ||
29 | }); | ||
30 | |||
31 | // Replace external issues | ||
32 | // Regex matches strings that don't begin with a "[", followed a repo name in the format "user/repo" | ||
33 | // followed by a "#" and digits to indicate a GitHub issue, and not ending with a "]" | ||
34 | readme = readme.replace(/(?<!\[)\w+\/\w+#\d+(?![\d\]])/gi, match => { | ||
35 | const issueNr = match.replace(/\D/g, ''); | ||
36 | const repo = match.replace(/#\d+/g, ''); | ||
37 | replacements += 1; | ||
38 | return `[${repo}#${issueNr}](https://github.com/${repo}/issues/${issueNr})`; | ||
39 | }); | ||
40 | |||
41 | // Replace Ferdi issues | ||
42 | // Regex matches strings that don't begin with a "[", i.e. are not already linked and | ||
43 | // don't begin with "franz", i.e. are not Franz issues, followed by a "#" and digits to indicate | ||
44 | // a GitHub issue, and not ending with a "]" | ||
45 | readme = readme.replace(/(?<!\[|franz)#\d+(?![\d\]])/gi, match => { | ||
46 | const issueNr = match.replace('#', ''); | ||
47 | replacements += 1; | ||
48 | return `[#${issueNr}](https://github.com/getferdi/ferdi/issues/${issueNr})`; | ||
49 | }); | ||
50 | |||
51 | // Link GitHub users | ||
52 | // Regex matches strings that don't begin with a "[", i.e. are not already linked | ||
53 | // followed by a "@" and at least one word character and not ending with a "]" | ||
54 | readme = readme.replace(/(?<!\[)@\w+(?!])/gi, match => { | ||
55 | const username = match.replace('@', ''); | ||
56 | replacements += 1; | ||
57 | return `[@${username}](https://github.com/${username})`; | ||
58 | }); | ||
59 | |||
60 | // Write to file | ||
61 | fs.writeFileSync(readmepath, readme); | ||
62 | |||
63 | console.log(`Added ${replacements} strings`); | ||