forked from AdobeDocs/adobe-dev-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscriptUtils.js
More file actions
55 lines (47 loc) · 1.64 KB
/
scriptUtils.js
File metadata and controls
55 lines (47 loc) · 1.64 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
const path = require('path');
const fs = require('node:fs');
const { globSync }= require('glob');
function getRedirectionsFilePath() {
const redirectionsFilePath = path.join(__dirname, 'src', 'pages', 'redirects.json');
return path.resolve(redirectionsFilePath);
}
function readRedirectionsFile() {
const redirectionsFilePath = getRedirectionsFilePath();
return JSON.parse(fs.readFileSync(redirectionsFilePath)).data;
}
function writeRedirectionsFile(data) {
let redirectionsData =
{
"total" : data.length,
"offset": 0,
"limit": data.length,
"data" : data,
":type": "sheet"
};
let redirectionsFilePath = getRedirectionsFilePath();
fs.writeFileSync(redirectionsFilePath, JSON.stringify(redirectionsData));
}
function getMarkdownFiles() {
return globSync(__dirname + '/src/pages/**/*.md')
.map(f => path.resolve(f));
}
const getFindPatternForMarkdownFiles = (from) => `(\\[[^\\]]*]\\()(/|./)?(${from})(#[^\\()]*)?(\\))`;
const getReplacePatternForMarkdownFiles = (to) => `$1$2${to}$4$5`;
function replaceLinksInFile({ file, linkMap, getFindPattern, getReplacePattern }) {
let data = fs.readFileSync(file, 'utf8');
linkMap.forEach((to, from) => {
const find = getFindPattern(from);
const replace = getReplacePattern(to);
data = data.replaceAll(new RegExp(find, "gm"), replace);
});
fs.writeFileSync(file, data, 'utf-8');
}
module.exports = {
getRedirectionsFilePath,
readRedirectionsFile,
writeRedirectionsFile,
getMarkdownFiles,
getFindPatternForMarkdownFiles,
getReplacePatternForMarkdownFiles,
replaceLinksInFile
};