-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghpages-adapt-bundle-urls.js
82 lines (66 loc) · 2.75 KB
/
ghpages-adapt-bundle-urls.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
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
let fs = require("fs");
let path = require("path");
const filesToChange = [/index.html/, /\.css$/, /\.js$/, /\.js\.map$/];
if (process.argv.length <= 2) {
console.log("Usage: " + __filename + " deployDir oldDeployDir");
process.exit(-1);
}
let deployDir = "/showcase/" + process.argv[2];
let baseHrefPlaceholder = "<stark-dummy-base-href>";
let deployUrlPlaceholder = "<stark-dummy-deploy-url>";
let urlWithTrailingSlash = deployDir.endsWith("/") ? deployDir : deployDir + "/";
let urlWithoutTrailingSlash = deployDir.endsWith("/") ? deployDir.substring(0, deployDir.length - 1) : deployDir;
let replacements = [
{ searchValue: `/${baseHrefPlaceholder}/${deployUrlPlaceholder}/`, replaceValue: urlWithTrailingSlash },
{ searchValue: `"${baseHrefPlaceholder}"`, replaceValue: `"${urlWithTrailingSlash}"` },
{ searchValue: `"${deployUrlPlaceholder}/`, replaceValue: `"${urlWithTrailingSlash}` },
{ searchValue: `${deployUrlPlaceholder}`, replaceValue: urlWithTrailingSlash } // these should also have a trailing slash, otherwise the URL of lazy loaded modules will be created incorrectly!
];
// if the 3rd param is given (oldDeployDir) then it will be appended to the "showcase" folder and replaced by the new deployDir
if (process.argv[3]) {
deployDir = "showcase/" + process.argv[2]; // no slashes at the beginning nor the end to cover all replacements at once
let oldDeployDir = "showcase/" + process.argv[3]; // no slashes at the beginning nor the end cover all replacements at once
replacements = [{ searchValue: oldDeployDir, replaceValue: deployDir }];
}
let outputDir = "showcase" + path.sep + "dist";
fs.readdir(outputDir, function (err, items) {
if (err) {
return console.error("Error while reading directory => " + err);
}
for (const item of items) {
for (const fileRegex of filesToChange) {
if (item.match(fileRegex)) {
let fullFilePath = outputDir + path.sep + item;
replaceValuesInFile(fullFilePath, replacements);
break;
}
}
}
});
function replaceValuesInFile(fileName, valueReplacements) {
fs.readFile(fileName, "utf8", function (err, data) {
if (err) {
return console.error("Error while reading file => " + err);
}
let result = data;
let replacementsDone = false;
for (const replacement of valueReplacements) {
const searchValueRegex = new RegExp(replacement.searchValue, "g");
if (searchValueRegex.test(result)) {
result = result.replace(searchValueRegex, replacement.replaceValue);
replacementsDone = true;
}
}
if (replacementsDone) {
fs.writeFile(fileName, result, "utf8", function (err) {
if (err) {
return console.error(err);
} else {
return console.log(`${fileName} updated successfully`);
}
});
} else {
return console.log(`${fileName} remained unchanged`);
}
});
}