Skip to content

Instantly share code, notes, and snippets.

@kcak11
Last active June 12, 2020 05:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcak11/584150e87be14a0e2f3368bfcb25a6c2 to your computer and use it in GitHub Desktop.
Save kcak11/584150e87be14a0e2f3368bfcb25a6c2 to your computer and use it in GitHub Desktop.
Angular CDNify

angular-cdnify

Install Package:

npm i gist:584150e87be14a0e2f3368bfcb25a6c2

Usage example in code:

const ngCDN = require("angular-cdnify");
ngCDN.updateBasePath("index.html", "https://mycdn.example.com/some-folder/");

Note: The index.html file should contain the placeholder pattern text {[BASE_PATH]}.
This placeholder pattern text will get replaced with the cdnURL provided as the second argument to the updateBasePath function.

e.g.: <base href="{[BASE_PATH]}" />

After invoking updateBasePath from the above example, the index.html file will be updated as below:
<base href="https://mycdn.example.com/some-folder/" />

Angular CDNify

A utility for easily updating the base path in a html file via NodeJS.

const fs = require('fs');
module.exports = {
updateBasePath: async function (sourceFilePath, cdnURL) {
if (!sourceFilePath || !cdnURL) {
throw new Error("Error: Missing MANDATORY Parameters => sourceFilePath & cdnURL");
} else {
let placeHolderRegex = /\{\[BASE_PATH\]\}/g;
let placeHolderSource = placeHolderRegex.source.replace(/\\/g, "");
let fileContents = "";
fs.readFile(sourceFilePath, (err, data) => {
if (err) {
throw new Error(err);
}
fileContents = data.toString();
if (fileContents.indexOf(placeHolderSource) === -1) {
throw new Error(`Placeholder missing in markup.\nLookup failed for string pattern: ${placeHolderSource}\n`);
}
fileContents = fileContents.replace(placeHolderRegex, cdnURL);
fs.writeFile(sourceFilePath, fileContents, (err) => {
if (err) {
throw err;
}
});
});
}
}
};
{
"name": "angular-cdnify",
"version": "1.0.0",
"description": "A utility for easily updating the base path in a html file via NodeJS.",
"main": "index.js",
"keywords": [
"angular",
"cdn",
"cdnify",
"base",
"basepath"
],
"author": "K.C.Ashish Kumar",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment