forked from NationalBankBelgium/stark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(stark-demo): add baseHref dynamically via Angular provider to fix…
… Showcase when published in GitHub pages ISSUES CLOSED: #466
- Loading branch information
1 parent
e823761
commit 8dc6e8e
Showing
3 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
let fs = require("fs"); | ||
|
||
const deployUrlPlaceholder = "<stark-dummy-deploy-url>"; | ||
const baseHrefPlaceholder = "<stark-dummy-base-href>"; | ||
|
||
let replacements = [ | ||
{ searchValue: `"deployUrl": ""`, replaceValue: `"deployUrl": "${deployUrlPlaceholder}"` }, | ||
{ searchValue: `"baseHref": ""`, replaceValue: `"baseHref": "${baseHrefPlaceholder}"` } | ||
]; | ||
|
||
replaceValuesInFile("angular.json", replacements); | ||
|
||
function replaceValuesInFile(fileName, valueReplacements) { | ||
fs.readFile(fileName, "utf8", function(err, data) { | ||
if (err) { | ||
return console.error("Error while reading file => " + err); | ||
} | ||
|
||
let result = data; | ||
|
||
for (const replacement of valueReplacements) { | ||
const searchValueRegex = new RegExp(replacement.searchValue, "g"); | ||
result = result.replace(searchValueRegex, replacement.replaceValue); | ||
} | ||
|
||
fs.writeFile(fileName, result, "utf8", function(err) { | ||
if (err) { | ||
return console.error(err); | ||
} else { | ||
return console.log(`${fileName} updated successfully`); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
let fs = require("fs"); | ||
let path = require("path"); | ||
|
||
const filesToChange = [ | ||
/index.html/, | ||
/main.*\.css$/, | ||
/runtime~main.*\.js$/, | ||
/runtime~main.*\.js\.map$/, | ||
/runtime~polyfills.*\.js$/, | ||
/runtime~polyfills.*\.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}"`, replaceValue: `"${urlWithTrailingSlash}"` }, | ||
{ searchValue: `"${deployUrlPlaceholder}"`, replaceValue: `"${urlWithoutTrailingSlash}"` }, | ||
{ searchValue: `"${deployUrlPlaceholder}/`, replaceValue: `"${urlWithTrailingSlash}` }, | ||
{ searchValue: `/${baseHrefPlaceholder}/${deployUrlPlaceholder}/`, replaceValue: urlWithTrailingSlash } | ||
]; | ||
|
||
// 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 have one single replacement | ||
let oldDeployDir = "showcase/" + process.argv[3]; // no slashes at the beginning nor the end to have one single replacement | ||
|
||
replacements = [{ searchValue: oldDeployDir, replaceValue: deployDir }]; | ||
} | ||
|
||
let outputDir = "." + 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; | ||
|
||
for (const replacement of valueReplacements) { | ||
const searchValueRegex = new RegExp(replacement.searchValue, "g"); | ||
result = result.replace(searchValueRegex, replacement.replaceValue); | ||
} | ||
|
||
fs.writeFile(fileName, result, "utf8", function(err) { | ||
if (err) { | ||
return console.error(err); | ||
} else { | ||
return console.log(`${fileName} updated successfully`); | ||
} | ||
}); | ||
}); | ||
} |