forked from AdobeDocs/adobe-dev-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenameFiles.js
More file actions
142 lines (125 loc) · 4.21 KB
/
renameFiles.js
File metadata and controls
142 lines (125 loc) · 4.21 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const path = require('path');
const fs = require('node:fs');
const { pathPrefix } = require('./gatsby-config.js');
const {
readRedirectionsFile,
writeRedirectionsFile,
getRedirectionsFilePath,
getMarkdownFiles,
getFindPatternForMarkdownFiles,
getReplacePatternForMarkdownFiles,
replaceLinksInFile
} = require('./scriptUtils.js');
function toKebabCase(str) {
const isScreamingSnakeCase = new RegExp(/^[A-Z0-9_]*$/gm).test(str);
str = isScreamingSnakeCase ? str.toLowerCase() : str;
return str
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
.map(x => x.toLowerCase())
.join('-');
}
function toEdsCase(str) {
const isValid = Boolean((/^([a-z0-9-]*)$/.test(str)));
return isValid ? str : toKebabCase(str);
}
function toUrl(file, renameBaseWithoutExt = name => name) {
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);
const newBaseWithoutExt = renameBaseWithoutExt(baseWithoutExt);
return `${file.substring(0, end)}${newBaseWithoutExt}`
}
function renameFile(file, renameBaseWithoutExt) {
const url = toUrl(file, renameBaseWithoutExt);
const ext = path.extname(file);
return `${url}${ext}`
}
function getFileMap(files) {
const map = new Map();
files.forEach(from => {
const to = renameFile(from, toEdsCase)
if(to !== from) {
map.set(from, to)
}
});
return map;
}
function getLinkMap(fileMap, relativeToDir) {
const linkMap = new Map();
fileMap.forEach((toFile, fromFile) => {
let fromRelFile = path.relative(relativeToDir, fromFile);
fromRelFile = fromRelFile.replaceAll(path.sep, '/');
let toRelFile = path.relative(relativeToDir, toFile);
toRelFile = toRelFile.replaceAll(path.sep, '/');
linkMap.set(fromRelFile, toRelFile);
});
return linkMap;
}
function renameLinksInMarkdownFile(fileMap, file) {
const dir = path.dirname(file);
replaceLinksInFile({
file,
linkMap: getLinkMap(fileMap, dir),
getFindPattern: getFindPatternForMarkdownFiles,
getReplacePattern: getReplacePatternForMarkdownFiles,
});
}
function renameLinksInRedirectsFile(fileMap) {
const file = getRedirectionsFilePath();
const dir = path.dirname(file);
replaceLinksInFile({
file,
linkMap: getLinkMap(fileMap, dir),
getFindPattern: (from) => `(['"]?)(Source|Destination)(['"]?\\s*:\\s*['"])(${pathPrefix}${toUrl(from)})(/?)(#[^'"]*)?(['"])`,
getReplacePattern: (to) => `$1$2$3${pathPrefix}${toUrl(to)}$5$6$7`,
});
}
function renameLinksInGatsbyConfigFile(fileMap, file) {
const dir = path.join('src', 'pages');
replaceLinksInFile({
file,
linkMap: getLinkMap(fileMap, dir),
getFindPattern: (from) => `(['"]?path['"]?\\s*:\\s*['"])(/|./)?(${from})(#[^'"]*)?(['"])`,
getReplacePattern: (to) => `$1$2${to}$4$5`,
});
}
function appendRedirects(fileMap) {
const file = getRedirectionsFilePath();
const dir = path.dirname(file);
const linkMap = getLinkMap(fileMap, dir);
const newData = [];
linkMap.forEach((to, from) => {
newData.push({
Source: `${pathPrefix}${toUrl(from)}`,
Destination: `${pathPrefix}${toUrl(to)}`,
})
});
const currData = readRedirectionsFile();
const data = [...currData, ...newData];
writeRedirectionsFile(data);
}
function renameFiles(map) {
map.forEach((to, from) => {
fs.renameSync(from, to);
});
}
try {
const markdownFiles = getMarkdownFiles();
const fileMap = getFileMap(markdownFiles);
markdownFiles.forEach(file => {
renameLinksInMarkdownFile(fileMap, file);
});
renameFiles(fileMap);
const redirectsFile = getRedirectionsFilePath();
if(fs.existsSync(redirectsFile)) {
renameLinksInRedirectsFile(fileMap);
appendRedirects(fileMap);
}
const gatsbyConfigFile = 'gatsby-config.js';
if(fs.existsSync(gatsbyConfigFile)) {
renameLinksInGatsbyConfigFile(fileMap, gatsbyConfigFile);
}
} catch (err) {
console.error(err);
}