Skip to content

Instantly share code, notes, and snippets.

@toddwseattle
Created July 16, 2019 16:10
Show Gist options
  • Save toddwseattle/c16d396f187ff3e0df164d994c5e1460 to your computer and use it in GitHub Desktop.
Save toddwseattle/c16d396f187ff3e0df164d994c5e1460 to your computer and use it in GitHub Desktop.
Script to update environment.prod.ts during build to match npm semver
/**
* replace.build.js: puts the semver from package.json in angular environment.prod.ts
* see: http://www.bilyachat.com/2017/01/angular-2-build-version.html
*/
var replace = require("replace-in-file");
var package = require("./package.json");
var buildVersion = package.version;
const options = {
files: "src/environments/environment.prod.ts",
from: /version: \"(.*)\"/g,
to: 'version: "' + buildVersion + '"',
allowEmptyPaths: false
};
try {
let changedFiles = replace.sync(options);
if (changedFiles == 0) {
throw "Please make sure that file '" +
options.files +
"' has \"version: ''\"";
}
console.log("Build version set: " + buildVersion);
} catch (error) {
console.error("Error occurred:", error);
throw error;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment