-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.js
48 lines (38 loc) · 1.54 KB
/
rename.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
const fs = require('fs');
const path = require('path');
const buildDir = './build';
const htmlFileName = 'index.html';
const jsRegex = /^.*\..*\.(js)$/;
const fileRegex = /^.*\..*\.(css|js|png|jpg)$/;
const manifestFileName = 'manifest.webmanifest';
const renameFile = fileName => {
const newFileName = fileName.replace('.', '-');
const newFilePath = path.join(buildDir, newFileName);
const filePath = path.join(buildDir, fileName);
return fs.renameSync(filePath, newFilePath);
};
const replaceFileName = (fileName, fileText) => {
return fileText.replace(new RegExp(fileName, 'g'), fileName.replace('.', '-'));
};
const renameFiles = async fileNames => {
return fileNames.forEach(file => renameFile(file));
};
const replaceFilesInFileText = async (fileNames, targetFileName) => {
const filePath = path.join(buildDir, targetFileName);
let fileText = fs.readFileSync(filePath, 'utf8');
fileNames.forEach(fileName => (fileText = replaceFileName(fileName, fileText)));
fs.writeFileSync(filePath, fileText, 'utf8');
};
const filesToRename = fs.readdirSync(buildDir).filter(file => file.match(fileRegex));
const jsFilesToRename = filesToRename.filter(file => file.match(jsRegex));
const renameBuildOutput = async () => {
await Promise.all(
jsFilesToRename.map(jsFile => replaceFilesInFileText(filesToRename, jsFile))
);
await Promise.all([
replaceFilesInFileText(filesToRename, htmlFileName),
replaceFilesInFileText(filesToRename, manifestFileName),
]);
await renameFiles(filesToRename);
};
renameBuildOutput().catch(console.error);