-
-
Notifications
You must be signed in to change notification settings - Fork 436
/
rename-v4-font-files.js
39 lines (35 loc) · 1.22 KB
/
rename-v4-font-files.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
/* eslint-disable no-param-reassign */
const fs = require('fs');
const path = require('path');
const listDir = (dir, fileList = []) => {
const files = fs.readdirSync(dir);
files.forEach((file) => {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
fileList = listDir(path.join(dir, file), fileList);
} else {
const fileDetails = file.split('.');
const currentFileNameWithoutExtension = fileDetails[0];
const fileExtension = fileDetails[1];
// e.g. QCF4001_COLOR-Regular will be converted to [ '4001' ]
const matchesArray = /^QCF(\d+).+/g.exec(currentFileNameWithoutExtension);
if (!matchesArray) {
return;
}
// since first page starts from 4001, subtracting 4000 will give us the actual number 4001 -> 1
const pageNumber = Number(matchesArray[1]) - 4000;
// e.g. p1.woff, p1.ttf, p1.woff2
const name = `p${pageNumber}.${fileExtension}`;
const src = path.join(dir, file);
const newSrc = path.join(dir, name);
fileList.push({
oldSrc: src,
newSrc,
});
}
});
return fileList;
};
const foundFiles = listDir('public/fonts/quran/hafs/v4');
foundFiles.forEach((f) => {
fs.renameSync(f.oldSrc, f.newSrc);
});