I am writing a nodejs script (that will be used in azure pipeline) to check that my Web developer didn't forget to regenerate object contracts used in the web application. So the goal is to read his file and compare objects within with the latest objects version stored somewhere else.
The file appends a subobject in the global object (window in browser) within an anonymous function like https://medium.com/@tkssharma/javascript-module-pattern-b4b5012ada9f
I tried the solution base on the "vm" module found here : Load "Vanilla" Javascript Libraries into Node.js
generatedModels.js
/***************************/
/* AUTO GENERATED via T4 */
/***************************/
(function(){
"use strict";
mynamespace = mynamespace || {};
mynamespace.factories = mynamespace.factories || {};
mynamespace.factories.models = mynamespace.factories.models || {};
mynamespace.factories.models.AdresseLibreModel_Factory = function() {
return {"CodePays":null,"ComplementAdresse":null, /*...*/};
};
})();
main.js
var vm = require("vm");
var fs = require("fs");
var data = fs.readFileSync("generatedModels.js");
var window = {};
vm.runInNewContext(data, window, "generatedModels.js");
Seems I can't use global object. It always fails during "runInNewContext" --> mynamespace is not defined at generatedModels.js:9:2
How can I use that file like it's used in browser? If needed, I can modify the T4 template to change the structure of the "generatedModels.js" file, but it still needs to be used by the browser.