@criticalmanufacturing/node-package-bundler
Version:
Connect IoT Package Bundler
156 lines • 8.52 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);
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BusinessScenariosProcessor = void 0;
const inversify_1 = require("inversify");
const template_1 = require("../models/template");
const log_1 = require("./log");
const io = require("fs-extra");
const types_1 = require("../types");
const path = require("path");
const paths_1 = require("./paths");
const transpiler_1 = require("./transpiler");
let BusinessScenariosProcessor = class BusinessScenariosProcessor {
constructor() {
this._finalBusinessScenarios = [];
}
process(businessScenariosLocation, destination) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
this._logger.info(` [BusinessScenarios] Processing Business Scenarios`);
const json = io.readJSONSync(destination);
json.criticalManufacturing = (_a = json === null || json === void 0 ? void 0 : json.criticalManufacturing) !== null && _a !== void 0 ? _a : {};
json.criticalManufacturing.businessScenarios = (_b = json.criticalManufacturing.businessScenarios) !== null && _b !== void 0 ? _b : [];
let businessScenarioMetadata = json.criticalManufacturing.businessScenarios;
this._finalBusinessScenarios = businessScenarioMetadata;
if (this._finalBusinessScenarios != null && ((_c = this._finalBusinessScenarios) === null || _c === void 0 ? void 0 : _c.length) > 0) {
this._logger.warn(" [BusinessScenarios] Existing business scenarios found in the package.json file found. Merging the new ones with the existing");
}
if (typeof businessScenariosLocation === "string") {
// Assume a path!
businessScenariosLocation = this._paths.transform(businessScenariosLocation);
if (!io.existsSync(businessScenariosLocation)) {
throw new Error(` [BusinessScenarios] Directory '${businessScenariosLocation}' doesn't exist`);
}
else {
this._businessScenariosDirectory = businessScenariosLocation;
const files = io.readdirSync(businessScenariosLocation);
for (const file of files) {
if (file.endsWith(".json")) {
yield this.merge(path.join(businessScenariosLocation, file));
}
}
}
}
else {
// Process each template entry
for (const businessScenarioLocation of businessScenariosLocation) {
switch (businessScenarioLocation.type) {
case template_1.TemplateType.Index:
yield this.processIndex(this._paths.transform(businessScenarioLocation.source));
break;
case template_1.TemplateType.Template:
yield this.merge(this._paths.transform(businessScenarioLocation.source));
break;
}
}
}
businessScenarioMetadata = this._finalBusinessScenarios;
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) {
return __awaiter(this, void 0, void 0, function* () {
const indexPath = path.dirname(indexFile);
// read array with files
const files = io.readJSONSync(indexFile);
if (!Array.isArray(files)) {
this._logger.error(` [BusinessScenarios] Index file '${indexFile}' doesn't contain an array of files to process!`);
}
else {
for (const file of files) {
yield this.merge(path.join(indexPath, file));
}
}
});
}
merge(businessScenarioFile) {
return __awaiter(this, void 0, void 0, function* () {
this._logger.info(` [BusinessScenarios] Merging '${businessScenarioFile}'`);
const newBusinessScenario = io.readJSONSync(businessScenarioFile);
if (newBusinessScenario != null) {
this._logger.debug(` [BusinessScenarios] Processing Business Scenario '${businessScenarioFile}'`);
yield this.processBusinessScenario(newBusinessScenario !== null && newBusinessScenario !== void 0 ? newBusinessScenario : {});
this._logger.debug(` [BusinessScenarios] Processed Business Scenario '${businessScenarioFile}'`);
}
this._logger.debug(` [BusinessScenarios] Merged '${businessScenarioFile}'`);
});
}
processBusinessScenario(newBusinessScenario) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (newBusinessScenario == null || Object.keys(newBusinessScenario).length === 0) {
return;
}
yield this._transpiler.preProcessTaskScripts(this._businessScenariosDirectory, newBusinessScenario);
// Check if there is another with the same name
const existing = ((_a = this._finalBusinessScenarios) !== null && _a !== void 0 ? _a : []).find(c => c.name === newBusinessScenario.name);
if (existing != null) {
const existingJson = JSON.stringify(existing);
const newOneJson = JSON.stringify(newBusinessScenario);
if (existingJson !== newOneJson) {
this._logger.warn(` [BusinessScenarios] Overwriting Business Scenario '${newBusinessScenario.name}' with a new one`);
Object.assign(existing, newBusinessScenario);
}
}
else {
// New one, so simply add it into the array
this._finalBusinessScenarios.push(newBusinessScenario);
this._logger.info(` [Templates] Found new Business Scenario '${newBusinessScenario.name}'`);
}
});
}
};
exports.BusinessScenariosProcessor = BusinessScenariosProcessor;
__decorate([
(0, inversify_1.inject)(types_1.TYPES.Logger),
__metadata("design:type", log_1.Log)
], BusinessScenariosProcessor.prototype, "_logger", void 0);
__decorate([
(0, inversify_1.inject)(types_1.TYPES.Paths),
__metadata("design:type", paths_1.Paths)
], BusinessScenariosProcessor.prototype, "_paths", void 0);
__decorate([
(0, inversify_1.inject)(types_1.TYPES.Transpiler),
__metadata("design:type", transpiler_1.Transpiler)
], BusinessScenariosProcessor.prototype, "_transpiler", void 0);
exports.BusinessScenariosProcessor = BusinessScenariosProcessor = __decorate([
(0, inversify_1.injectable)()
], BusinessScenariosProcessor);
//# sourceMappingURL=businessScenarios.js.map