@criticalmanufacturing/node-package-bundler
Version:
Connect IoT Package Bundler
213 lines • 11.9 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DriverTemplatesProcessor = void 0;
const template_1 = require("../models/template");
const log_1 = require("./log");
const io = require("fs-extra");
const path = require("path");
const inversify_1 = require("inversify");
const types_1 = require("../types");
const paths_1 = require("./paths");
let DriverTemplatesProcessor = class DriverTemplatesProcessor {
constructor() {
this._finalTemplates = {};
this._protocolDatatypes = [];
}
process(templateRules, destination) {
var _a, _b, _c, _d, _e;
const json = io.readJSONSync(destination);
const automationProtocol = json.criticalManufacturing.automationProtocol;
this._protocolDatatypes = (_a = automationProtocol.dataTypes) !== null && _a !== void 0 ? _a : [];
this._finalTemplates = (_b = automationProtocol.templates) !== null && _b !== void 0 ? _b : {};
if (this._finalTemplates != null && (this._finalTemplates.property != null || this._finalTemplates.event != null)) {
this._logger.warn(" [Templates] Existing templates found in the package.json file found. Merging the new ones with the existing");
}
// Prepare the initial object
this._finalTemplates.property = (_c = this._finalTemplates.property) !== null && _c !== void 0 ? _c : [];
this._finalTemplates.event = (_d = this._finalTemplates.event) !== null && _d !== void 0 ? _d : [];
this._finalTemplates.command = (_e = this._finalTemplates.command) !== null && _e !== void 0 ? _e : [];
if (typeof templateRules === "string") {
// Assume a path!
templateRules = this._paths.transform(templateRules);
const files = io.readdirSync(templateRules);
for (const file of files) {
if (file.endsWith(".json")) {
this.merge(path.join(templateRules, file));
}
}
}
else {
// Process each template entry
for (const templateRule of templateRules) {
switch (templateRule.type) {
case template_1.TemplateType.Index:
this.processIndex(this._paths.transform(templateRule.source));
break;
case template_1.TemplateType.Template:
this.merge(this._paths.transform(templateRule.source));
break;
}
}
}
automationProtocol.templates = this._finalTemplates;
io.writeFileSync(destination, JSON.stringify(json, null, 2), "utf8");
}
/**
* Use an index file with an array of files to process
* using the index as order:
* [
* "first.json",
* "second.json"
* ]
* @param indexFile The json file with the array of files
*/
processIndex(indexFile) {
const indexPath = path.dirname(indexFile);
// read array with files
const files = io.readJSONSync(indexFile);
if (!Array.isArray(files)) {
this._logger.error(` [Templates] Index file '${indexFile}' doesn't contain an array of files to process!`);
}
else {
for (const file of files) {
this.merge(path.join(indexPath, file));
}
}
}
merge(templateFile) {
var _a, _b, _c;
this._logger.info(` [Templates] Merging '${templateFile}'`);
const newTemplates = io.readJSONSync(templateFile);
if (newTemplates != null) {
this.mergeProperties((_a = newTemplates.property) !== null && _a !== void 0 ? _a : []);
this.mergeEvents((_b = newTemplates.event) !== null && _b !== void 0 ? _b : []);
this.mergeCommands((_c = newTemplates.command) !== null && _c !== void 0 ? _c : []);
}
}
mergeProperties(properties) {
properties.forEach(property => {
var _a, _b;
const newOne = Object.assign({}, template_1.JsonTemplatePropertyDefaults, property);
// Check if there is another with the same name
const existing = ((_a = this._finalTemplates.property) !== null && _a !== void 0 ? _a : []).find(p => p.Name === newOne.Name);
if (!this._protocolDatatypes.find(obj => obj.name === newOne.AutomationProtocolDataType)) {
throw new Error(`Property '${newOne.Name}'contains an invalid Automation Protocol DataType '${newOne.AutomationProtocolDataType}'`);
}
if (existing != null) {
const existingJson = JSON.stringify(existing);
const newOneJson = JSON.stringify(newOne);
if (existingJson !== newOneJson) {
this._logger.warn(` [Templates] Overwriting property '${newOne.Name}' with a new one`);
Object.assign(existing, newOne);
}
}
else {
// New one, so simply add it into the array
(_b = this._finalTemplates.property) === null || _b === void 0 ? void 0 : _b.push(newOne);
this._logger.info(` [Templates] Found new property '${newOne.Name}'`);
}
});
}
mergeEvents(events) {
events.forEach(event => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const newOne = Object.assign({}, template_1.JsonTemplateEventDefaults, event);
let set = new Set();
for (let i = 0; i < ((_a = newOne.EventProperties) !== null && _a !== void 0 ? _a : []).length; i++) {
((_b = newOne.EventProperties) !== null && _b !== void 0 ? _b : [])[i] = Object.assign({}, template_1.JsonTemplateEventPropertyDefaults, ((_c = newOne.EventProperties) !== null && _c !== void 0 ? _c : [])[i]);
const eventProperty = ((_d = newOne.EventProperties) !== null && _d !== void 0 ? _d : [])[i];
set = this.validateOrder(set, "Event", newOne.Name, eventProperty.Property, (_e = eventProperty.Order) !== null && _e !== void 0 ? _e : -1, i);
((_f = newOne.EventProperties) !== null && _f !== void 0 ? _f : [])[i].Order = [...set][set.size - 1];
}
let validateEvent = false;
// Check if there is another with the same name
const existing = ((_g = this._finalTemplates.event) !== null && _g !== void 0 ? _g : []).find(p => p.Name === newOne.Name);
if (existing != null) {
const existingJson = JSON.stringify(existing);
const newOneJson = JSON.stringify(newOne);
if (existingJson !== newOneJson) {
this._logger.warn(` [Templates] Overwriting event '${newOne.Name}' with a new one`);
Object.assign(existing, newOne);
validateEvent = true;
}
}
else {
// New one, so simply add it into the array
(_h = this._finalTemplates.event) === null || _h === void 0 ? void 0 : _h.push(newOne);
this._logger.info(` [Templates] Found new event '${newOne.Name}'`);
validateEvent = true;
}
if (validateEvent) {
for (const property of (_j = newOne.EventProperties) !== null && _j !== void 0 ? _j : []) {
if (((_k = this._finalTemplates.property) !== null && _k !== void 0 ? _k : []).find(p => p.Name === property.Property) == null) {
this._logger.error(` [Templates] Event '${event.Name}' has a reference to the unknown property '${property.Property}'`);
}
}
}
});
}
mergeCommands(commands) {
commands.forEach(command => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
const newOne = Object.assign({}, template_1.JsonTemplateCommandDefaults, command);
let set = new Set();
for (let i = 0; i < ((_a = newOne.CommandParameters) !== null && _a !== void 0 ? _a : []).length; i++) {
((_b = newOne.CommandParameters) !== null && _b !== void 0 ? _b : [])[i] = Object.assign({}, template_1.JsonTemplateCommandParameterDefaults, ((_c = newOne.CommandParameters) !== null && _c !== void 0 ? _c : [])[i]);
if (!this._protocolDatatypes.find(obj => { var _a; return obj.name === ((_a = newOne.CommandParameters) !== null && _a !== void 0 ? _a : [])[i].AutomationProtocolDataType; })) {
throw new Error(`Command '${newOne.Name}' with Command Parameter '${((_d = newOne.CommandParameters) !== null && _d !== void 0 ? _d : [])[i].Name}' contains an invalid Automation Protocol DataType '${((_e = newOne.CommandParameters) !== null && _e !== void 0 ? _e : [])[i].AutomationProtocolDataType}'`);
}
const commandParameter = ((_f = newOne.CommandParameters) !== null && _f !== void 0 ? _f : [])[i];
set = this.validateOrder(set, "Command", newOne.Name, commandParameter.Name, (_g = commandParameter.Order) !== null && _g !== void 0 ? _g : -1, i);
((_h = newOne.CommandParameters) !== null && _h !== void 0 ? _h : [])[i].Order = [...set][set.size - 1];
}
// Check if there is another with the same name
const existing = ((_j = this._finalTemplates.command) !== null && _j !== void 0 ? _j : []).find(p => p.Name === newOne.Name);
if (existing != null) {
const existingJson = JSON.stringify(existing);
const newOneJson = JSON.stringify(newOne);
if (existingJson !== newOneJson) {
this._logger.warn(` [Templates] Overwriting command '${newOne.Name}' with a new one`);
Object.assign(existing, newOne);
}
}
else {
// New one, so simply add it into the array
(_k = this._finalTemplates.command) === null || _k === void 0 ? void 0 : _k.push(newOne);
this._logger.info(` [Templates] Found new command '${newOne.Name}'`);
}
});
}
validateOrder(set, type, name, item, order, iteration) {
// Fix order if needed
if (order === -1) {
order = iteration + 1;
}
if (set.has(order)) {
throw new Error(`In '${type}' '${name}', parameter '${item}' has a repeated order '${order}'`);
}
set.add(order);
return set;
}
};
exports.DriverTemplatesProcessor = DriverTemplatesProcessor;
__decorate([
(0, inversify_1.inject)(types_1.TYPES.Logger),
__metadata("design:type", log_1.Log)
], DriverTemplatesProcessor.prototype, "_logger", void 0);
__decorate([
(0, inversify_1.inject)(types_1.TYPES.Paths),
__metadata("design:type", paths_1.Paths)
], DriverTemplatesProcessor.prototype, "_paths", void 0);
exports.DriverTemplatesProcessor = DriverTemplatesProcessor = __decorate([
(0, inversify_1.injectable)()
], DriverTemplatesProcessor);
//# sourceMappingURL=driverTemplates.js.map