-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.js
More file actions
68 lines (63 loc) · 2.45 KB
/
patch.js
File metadata and controls
68 lines (63 loc) · 2.45 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
const path = require('path');
const fs = require('fs');
const cssDirPath = ['_sitefinal', 'assets', 'css'];
const cssAssetsDirPath = path.join(...cssDirPath);
const jsDirPath = ['_sitefinal', 'assets', 'js'];
const jsAssetsDirPath = path.join(...jsDirPath);
const fiDirPath = ['_sitefinal', 'fi'];
const fiContentDirPath = path.join(...fiDirPath);
const enDirPath = ['_sitefinal', 'en'];
const enContentDirPath = path.join(...enDirPath);
const repDefault = {search: /\/fi\/assets\//g, replace: '/assets/'};
const repCustomUrls = {search: '"/assets/', replace: '"/dev-docs/assets/'};
fs.readdirSync(cssAssetsDirPath, {withFileTypes: true}).forEach(fsEnt => {
if (fsEnt.name.endsWith('.map')) fs.unlinkSync(path.join(...cssDirPath.concat(fsEnt.name)));
else replace(fsEnt, cssDirPath, [repDefault]);
});
fs.readdirSync(jsAssetsDirPath, {withFileTypes: true}).forEach(fsEnt => {
replace(fsEnt, jsDirPath, [repDefault]);
});
recurse(fs.readdirSync(fiContentDirPath, {withFileTypes: true}), fiDirPath, (fsEnt, baseDir) => {
replace(fsEnt, baseDir, [
repDefault,
repCustomUrls,
{search: /"\/tutorials\//g, replace: '"/dev-docs/fi/tutorials/'},
]);
});
recurse(fs.readdirSync(enContentDirPath, {withFileTypes: true}), enDirPath, (fsEnt, baseDir) => {
replace(fsEnt, baseDir, [
{search: /\/en\/assets\//g, replace: '/assets/'},
repCustomUrls,
{search: /"\/tutorials\//g, replace: '"/dev-docs/en/tutorials/'},
]);
});
/**
* @param {fs.Dirent} fsEnt
* @param {Array<string>} baseDir
* @param {Array<{search: string; replace: string;}>} reps
*/
function replace(fsEnt, baseDir, reps) {
if (fsEnt.isDirectory()) return;
const filePath = path.join(...baseDir.concat(fsEnt.name));
const rep = reps.reduce((out, {search, replace}) =>
out.replace(search, replace)
, fs.readFileSync(filePath, 'utf8'));
fs.writeFileSync(filePath, rep, 'utf8');
}
/**
* @param {Array<fs.Dirent>} branch
* @param {Array<string>} base
* @param {(fsEnt: fs.Dirent, baseDir: Array<string>) => void} doFn
* @param {Array<string>} cur = []
*/
function recurse(branch, base, doFn, cur = []) {
branch.forEach(fsEnt => {
if (!fsEnt.isDirectory()) {
doFn(fsEnt, base.concat(cur));
} else {
const newCur = cur.concat([fsEnt.name]);
const p = path.join(...base.concat(newCur));
recurse(fs.readdirSync(p, {withFileTypes: true}), base, doFn, newCur);
}
});
}