-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathscriptUtils.js
More file actions
73 lines (62 loc) · 2.24 KB
/
Copy pathscriptUtils.js
File metadata and controls
73 lines (62 loc) · 2.24 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 getFiles(fileExtensions) {
const fileExtensionsPattern = fileExtensions.join('|');
return globSync(__dirname + `/src/pages/**/*+(${fileExtensionsPattern})`).map((f) => path.relative(__dirname, f));
}
function getDeployableFiles() {
// files types deployed to EDS in process-mds.sh
return getFiles(['.md', '.json']);
}
function getMarkdownFiles() {
return getFiles(['.md']);
}
function removeFileExtension(file) {
const base = path.basename(file);
const ext = path.extname(file);
const end = file.length - base.length;
const baseWithoutExt = base.substring(0, base.length - ext.length);
return `${file.substring(0, end)}${baseWithoutExt}`;
}
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,
getDeployableFiles,
getMarkdownFiles,
getFindPatternForMarkdownFiles,
getReplacePatternForMarkdownFiles,
removeFileExtension,
replaceLinksInFile,
};