-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocs-update.js
64 lines (54 loc) · 1.77 KB
/
docs-update.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
const { readFile, writeFile } = require('node:fs/promises');
const { join } = require('node:path');
const pkg = require('./package.json');
const { unlinkSync, renameSync } = require('node:fs');
const APP_VERSION = '4.0.0'; // JSON.stringify(pkg.version);
const FILENAME = 'README.md';
const inputFilePath = join('./docs', FILENAME);
const outputFilePath = join('./docs', `${FILENAME}.tmp`);
readFile(inputFilePath, { encoding: 'utf8' })
.then(content => {
const markdownLines = content.split(/\r\n|\r|\n/g);
for (let i = 0; i < markdownLines.length; i++) {
if (markdownLines[i].startsWith('[![macOs Download]')) {
const downloadButtons = markdownLines[i]
.split(/(?=\[!)/g)
.map(item => item.trim());
for (let j = 0; j < downloadButtons.length; j++) {
const replacedTag = downloadButtons[j].replace(
/v\d+(\.\d+){1,2}/,
`v${APP_VERSION.replace(/"/g, '')}`
);
const newButton = replacedTag.replace(
/_\d+(\.\d+){1,2}_/,
`_${APP_VERSION.replace(/"/g, '')}_`
);
downloadButtons[j] = newButton;
}
markdownLines[i] = downloadButtons.join(' ');
}
}
writeFile(outputFilePath, markdownLines.join('\n'), { encoding: 'utf8' })
.then(() => {
try {
unlinkSync(inputFilePath);
} catch (error) {
console.error(error);
process.exit(-1);
}
try {
renameSync(outputFilePath, inputFilePath);
} catch (error) {
console.error(error);
process.exit(-1);
}
})
.catch(error => {
console.error(error);
process.exit(-1);
});
})
.catch(error => {
console.error(error);
process.exit(-1);
});