UNPKG

@foxpage/foxpage-node-sdk

Version:

foxpage node sdk

162 lines (161 loc) 5.96 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCurReportData = exports.runtimeReporter = exports.appReporter = exports.pluginReporter = exports.componentReporterClear = exports.componentReporter = exports.igniteReporter = exports.cost = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = require("path"); const lodash_1 = __importDefault(require("lodash")); const foxpage_manager_1 = require("@foxpage/foxpage-manager"); const pm2_1 = require("../pm2"); const timer_1 = require("./timer"); const COMMON = 'common'; const reportData = { igniteInfo: {}, appInfo: {}, runtimeInfo: { processInfo: {}, sdkVersion: {}, }, }; const cost = (label) => { return (0, timer_1.timeStart)(label); }; exports.cost = cost; /** * ignite reporter * @param label cost label * @returns */ const igniteReporter = (label) => { return (0, timer_1.timeStart)(label, cost => { // cost cache reportData.igniteInfo[label] = cost; }); }; exports.igniteReporter = igniteReporter; /** * component reporter * @param name * @param value */ const componentReporter = (name, value, opt) => { const { appId = COMMON } = opt || {}; let appInfo = reportData.appInfo[appId]; if (!appInfo) { appInfo = reportData.appInfo[appId] = { components: {}, plugins: {} }; } appInfo.components[name] = value; if (appId && appId !== COMMON) { const app = (0, foxpage_manager_1.getApplication)(appId); appInfo.componentCount = (app === null || app === void 0 ? void 0 : app.packageManager.getPackageCount()) || 0; } }; exports.componentReporter = componentReporter; /** * clear component data * @param appId */ const componentReporterClear = (appId) => { if (appId) { reportData.appInfo[appId].components = {}; } else { Object.keys(reportData.appInfo).forEach(_appId => { reportData.appInfo[_appId].components = {}; }); } }; exports.componentReporterClear = componentReporterClear; /** * plugin reporter * @param name * @param value */ const pluginReporter = (name, value, opt) => { const { appId = COMMON } = opt || {}; let appInfo = reportData.appInfo[appId]; if (!appInfo) { appInfo = reportData.appInfo[appId] = { components: {}, plugins: {} }; } appInfo.plugins[name] = value; appInfo.pluginCount = Object.keys(appInfo.plugins).length; }; exports.pluginReporter = pluginReporter; /** * app info reporter * @param appId * @param name * @param value */ const appReporter = (appId, name, value) => { let appInfo = reportData.appInfo[appId]; if (!appInfo) { appInfo = reportData.appInfo[appId] = { components: {}, plugins: {} }; } appInfo[name] = value; }; exports.appReporter = appReporter; /** * runtime reporter */ const runtimeReporter = () => { try { const pm2 = (0, pm2_1.getPm2)(); const root = (0, path_1.join)(process.cwd(), 'node_modules', '@foxpage'); const nodeSDKJson = JSON.parse(fs_1.default.readFileSync((0, path_1.join)(root, 'foxpage-node-sdk', 'package.json'), 'utf8')); const managerJson = JSON.parse(fs_1.default.readFileSync((0, path_1.join)(root, 'foxpage-manager', 'package.json'), 'utf8')); const coreJson = JSON.parse(fs_1.default.readFileSync((0, path_1.join)(root, 'foxpage-core', 'package.json'), 'utf8')); reportData.runtimeInfo = { processInfo: { pid: pm2 === null || pm2 === void 0 ? void 0 : pm2.id, isMaster: pm2 === null || pm2 === void 0 ? void 0 : pm2.isMaster, isWork: pm2 === null || pm2 === void 0 ? void 0 : pm2.isWorker, }, sdkVersion: { manager: managerJson === null || managerJson === void 0 ? void 0 : managerJson.version, nodeSDK: nodeSDKJson === null || nodeSDKJson === void 0 ? void 0 : nodeSDKJson.version, core: coreJson === null || coreJson === void 0 ? void 0 : coreJson.version, }, }; } catch (e) { console.error(`[ Foxpage ][ runtimeReporter ] failed: `, e); } }; exports.runtimeReporter = runtimeReporter; /** * get report data * @param appId * @returns */ const getCurReportData = (appId) => { const cloned = lodash_1.default.cloneDeep(reportData); if (!appId) { // clear (0, exports.componentReporterClear)(); return cloned; } const { components: commonComponents = {}, plugins: commonPlugins = {} } = cloned.appInfo[COMMON] || {}; const _a = cloned.appInfo[appId] || {}, { components = {}, plugins = {} } = _a, rest = __rest(_a, ["components", "plugins"]); const app = (0, foxpage_manager_1.getApplication)(appId); const result = Object.assign(Object.assign({}, cloned), { appInfo: { [appId]: Object.assign(Object.assign({}, rest), { componentCount: (app === null || app === void 0 ? void 0 : app.packageManager.getPackageCount()) || 0, components: Object.assign(Object.assign({}, commonComponents), components), plugins: Object.assign(Object.assign({}, commonPlugins), plugins) }), } }); // clear (0, exports.componentReporterClear)(appId); return result; }; exports.getCurReportData = getCurReportData;