UNPKG

@omnia/tooling-vue

Version:

Used to bundle and serve manifests web component that build on Vue framework.

127 lines (126 loc) 4.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.registerOmniaMiddleware = exports.ManifestChangedMessage = exports.resolvableManifest = void 0; const tslib_1 = require("tslib"); const chokidar_1 = tslib_1.__importDefault(require("chokidar")); const tooling_1 = require("@omnia/tooling"); const tooling_2 = require("@omnia/tooling"); const $ = tslib_1.__importStar(require("../../variables")); let _manifestJsonObject = null; let _app = null; class ImplementManifestChangedMessage { constructor() { this._subcribers = []; } publish() { this._subcribers.forEach(fn => fn()); } subcribe(fn) { this._subcribers.push(fn); } } exports.resolvableManifest = null; exports.ManifestChangedMessage = new ImplementManifestChangedMessage(); function registerOmniaMiddleware(server, hmr) { _app = server.middlewares; server.middlewares.use('/omnia', function (req, res, next) { ensureLoadManifest(); exports.resolvableManifest.promise.then(() => { next(); }); }); server.middlewares.use('/omnia/localhost/service/id', function (req, res, next) { res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify({ id: $.composers.ServiceManifestRegistry.getServiceInfo().id, hmr: hmr })); }); server.middlewares.use('/omnia/localhost/service/manifests.json', function (req, res, next) { let manifestJson = JSON.parse(JSON.stringify($.tooling.composer.getManifestData())); // remove group manifest in hmr mode //if (server.config.server.hmr) { // manifestJson.groupedResouresAndComponents = []; //} res.setHeader('Cache-Control', 'no-cache'); res.setHeader('Content-Type', 'application/json'); res.end(JSON.stringify({ hmr: server.config.server.hmr, manifest: manifestJson })); }); let watchManifests = chokidar_1.default.watch(["**/*.manifest.ts", "omnia.service.ts"], { ignored: [/[\/\\]\./, '**/node_modules/**'], persistent: true }); watchManifests.on('ready', function () { watchManifests .on('add', path => { $.tooling.utils.log(`Found manifest added -> ${path}`); rebuildManifests(); }) .on('change', path => { $.tooling.utils.log(`Found manifest changed -> ${path}`); rebuildManifests(); }) .on('unlink', path => { $.tooling.utils.log(`Found manifest removed -> ${path}`); rebuildManifests(); }) .on('error', error => console.log(`Watcher manifest error: ${error}`)); }); // // trigger build manifest // setTimeout(ensureLoadManifest, 100); } exports.registerOmniaMiddleware = registerOmniaMiddleware; function rebuildManifests() { tooling_1.utils.timeWatch("omniamiddleware_watchmanifest", () => { if (exports.resolvableManifest) { exports.resolvableManifest.promise.then(() => { exports.resolvableManifest = null; try { ensureLoadManifest(); } catch (e) { tooling_1.utils.log(e, tooling_2.LogTypes.Error); } }); } else { try { ensureLoadManifest(); } catch (e) { tooling_1.utils.log(e, tooling_2.LogTypes.Error); } } }, 1000); } function ensureLoadManifest() { if (_app && !exports.resolvableManifest) { exports.resolvableManifest = new tooling_2.ResolvablePromise(); exports.ManifestChangedMessage.publish(); var tasks = tooling_1.core.getRegisteredServeTasks().filter(function (task) { return task.stage === tooling_1.core.TaskStage.AfterCreateServer; }).sort(sortTask) || []; executeTasks(tasks, _app, () => { exports.resolvableManifest.resolve(); }, err => { exports.resolvableManifest.resolve(); tooling_1.utils.log(err, tooling_2.LogTypes.Error); }); } } function executeTasks(tasks, data, resolve, reject) { if (tasks.length === 0) { if (resolve) resolve(data); } else { tasks.shift() .task(data) .then(function (data) { executeTasks(tasks, data, resolve, reject); }, reject) .catch((error) => { if (reject) reject(error); }); } } function sortTask(task1, task2) { return task1.order - task2.order; }