@criticalmanufacturing/node-package-bundler
Version:
Connect IoT Package Bundler
187 lines • 10.4 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.LibraryTemplatesProcessor = void 0;
const template_1 = require("../models/template");
const library_1 = require("../models/library");
const log_1 = require("./log");
const transpiler_1 = require("./transpiler");
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 LibraryTemplatesProcessor = class LibraryTemplatesProcessor {
constructor() {
this._finalTemplates = {};
}
process(templateRules, destination) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h;
this._logger.info(` [Templates] Processing library templates`);
const json = io.readJSONSync(destination);
if (((_a = json === null || json === void 0 ? void 0 : json.criticalManufacturing) === null || _a === void 0 ? void 0 : _a.tasksLibrary) == null) {
throw new Error("Unable to read TasksLibrary section of the package.json file");
}
const libraryMetadata = json.criticalManufacturing.tasksLibrary;
this._finalTemplates = (_b = libraryMetadata.metadata) !== null && _b !== void 0 ? _b : {};
if (this._finalTemplates != null && (((_d = (_c = this._finalTemplates.converters) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) !== 0 || ((_f = (_e = this._finalTemplates.tasks) === null || _e === void 0 ? void 0 : _e.length) !== null && _f !== void 0 ? _f : 0) !== 0)) {
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.converters = (_g = this._finalTemplates.converters) !== null && _g !== void 0 ? _g : [];
this._finalTemplates.tasks = (_h = this._finalTemplates.tasks) !== null && _h !== void 0 ? _h : [];
if (typeof templateRules === "string") {
// Assume a path!
templateRules = this._paths.transform(templateRules);
if (!io.existsSync(templateRules)) {
throw new Error(` [Templates] Directory '${templateRules}' doesn't exist`);
}
else {
this._templateDirectory = templateRules;
const files = io.readdirSync(templateRules);
for (const file of files) {
if (file.endsWith(".json")) {
yield this.merge(path.join(templateRules, file));
}
}
}
}
else {
// Process each template entry
for (const templateRule of templateRules) {
switch (templateRule.type) {
case template_1.TemplateType.Index:
yield this.processIndex(this._paths.transform(templateRule.source));
break;
case template_1.TemplateType.Template:
yield this.merge(this._paths.transform(templateRule.source));
break;
}
}
}
libraryMetadata.metadata = 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) {
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(` [Templates] 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(templateFile) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
this._logger.info(` [Templates] Merging '${templateFile}'`);
const newTemplates = io.readJSONSync(templateFile);
if (newTemplates != null) {
this._logger.debug(` [Templates] Merging Tasks & Converters '${templateFile}'`);
yield this.mergeConverters((_a = newTemplates.converters) !== null && _a !== void 0 ? _a : []);
this._logger.debug(` [Templates] Merged Converters '${templateFile}'`);
yield this.mergeTasks((_b = newTemplates.tasks) !== null && _b !== void 0 ? _b : []);
this._logger.debug(` [Templates] Merged Tasks '${templateFile}'`);
}
this._logger.debug(` [Templates] Merged '${templateFile}'`);
});
}
mergeConverters(converters) {
return __awaiter(this, void 0, void 0, function* () {
converters.forEach(converter => {
var _a, _b, _c, _d;
const newOne = Object.assign({}, library_1.LibraryConverterDefaults, converter);
// Check if there is another with the same name
const existing = ((_a = this._finalTemplates.converters) !== null && _a !== void 0 ? _a : []).find(c => c.name === newOne.name);
if (existing != null) {
const existingJson = JSON.stringify(existing);
const newOneJson = JSON.stringify(newOne);
if (existingJson !== newOneJson) {
this._logger.warn(` [Templates] Overwriting converter '${(_b = newOne.displayName) !== null && _b !== void 0 ? _b : newOne.name}' with a new one`);
Object.assign(existing, newOne);
}
}
else {
// New one, so simply add it into the array
(_c = this._finalTemplates.converters) === null || _c === void 0 ? void 0 : _c.push(newOne);
this._logger.info(` [Templates] Found new converter '${(_d = newOne.displayName) !== null && _d !== void 0 ? _d : newOne.name}'`);
}
});
});
}
mergeTasks(tasks) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d;
for (const task of tasks) {
const newOne = /*await this.preProcessTaskScripts*/ (Object.assign({}, library_1.LibraryTaskDefaults, task));
yield this._transpiler.preProcessTaskScripts(this._templateDirectory, newOne);
// Check if there is another with the same name
const existing = ((_a = this._finalTemplates.tasks) !== null && _a !== void 0 ? _a : []).find(c => c.name === newOne.name);
if (existing != null) {
const existingJson = JSON.stringify(existing);
const newOneJson = JSON.stringify(newOne);
if (existingJson !== newOneJson) {
this._logger.warn(` [Templates] Overwriting task '${(_b = newOne.displayName) !== null && _b !== void 0 ? _b : newOne.name}' with a new one`);
Object.assign(existing, newOne);
}
}
else {
// New one, so simply add it into the array
(_c = this._finalTemplates.tasks) === null || _c === void 0 ? void 0 : _c.push(newOne);
this._logger.info(` [Templates] Found new task '${(_d = newOne.displayName) !== null && _d !== void 0 ? _d : newOne.name}'`);
}
}
});
}
};
exports.LibraryTemplatesProcessor = LibraryTemplatesProcessor;
__decorate([
(0, inversify_1.inject)(types_1.TYPES.Logger),
__metadata("design:type", log_1.Log)
], LibraryTemplatesProcessor.prototype, "_logger", void 0);
__decorate([
(0, inversify_1.inject)(types_1.TYPES.Paths),
__metadata("design:type", paths_1.Paths)
], LibraryTemplatesProcessor.prototype, "_paths", void 0);
__decorate([
(0, inversify_1.inject)(types_1.TYPES.Transpiler),
__metadata("design:type", transpiler_1.Transpiler)
], LibraryTemplatesProcessor.prototype, "_transpiler", void 0);
exports.LibraryTemplatesProcessor = LibraryTemplatesProcessor = __decorate([
(0, inversify_1.injectable)()
], LibraryTemplatesProcessor);
//# sourceMappingURL=libraryTemplates.js.map