UNPKG

@vnxjs/service

Version:
290 lines 12.4 kB
"use strict"; 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 }); const helper_1 = require("@vnxjs/helper"); const helper = require("@vnxjs/helper"); const events_1 = require("events"); const lodash_1 = require("lodash"); const path = require("path"); const tapable_1 = require("tapable"); const Config_1 = require("./Config"); const Plugin_1 = require("./Plugin"); const utils_1 = require("./utils"); const constants_1 = require("./utils/constants"); class Kernel extends events_1.EventEmitter { constructor(options) { super(); this.debugger = process.env.DEBUG === 'Vnmf:Kernel' ? (0, helper_1.createDebug)('Vnmf:Kernel') : function () { }; this.appPath = options.appPath || process.cwd(); this.optsPresets = options.presets; this.optsPlugins = options.plugins; this.hooks = new Map(); this.methods = new Map(); this.commands = new Map(); this.platforms = new Map(); this.initHelper(); this.initConfig(); this.initPaths(); } initConfig() { this.config = new Config_1.default({ appPath: this.appPath }); this.initialConfig = this.config.initialConfig; this.debugger('initConfig', this.initialConfig); } initPaths() { this.paths = { appPath: this.appPath, nodeModulesPath: (0, helper_1.recursiveFindNodeModules)(path.join(this.appPath, helper_1.NODE_MODULES)) }; if (this.config.isInitSuccess) { Object.assign(this.paths, { configPath: this.config.configPath, sourcePath: path.join(this.appPath, this.initialConfig.sourceRoot), outputPath: path.join(this.appPath, this.initialConfig.outputRoot) }); } this.debugger(`initPaths:${JSON.stringify(this.paths, null, 2)}`); } initHelper() { this.helper = helper; this.debugger('initHelper'); } initPresetsAndPlugins() { const initialConfig = this.initialConfig; const allConfigPresets = (0, utils_1.mergePlugins)(this.optsPresets || [], initialConfig.presets || [])(); const allConfigPlugins = (0, utils_1.mergePlugins)(this.optsPlugins || [], initialConfig.plugins || [])(); this.debugger('initPresetsAndPlugins', allConfigPresets, allConfigPlugins); process.env.NODE_ENV !== 'test' && (0, helper_1.createSwcRegister)({ only: [...Object.keys(allConfigPresets), ...Object.keys(allConfigPlugins)] }); this.plugins = new Map(); this.extraPlugins = {}; this.resolvePresets(allConfigPresets); this.resolvePlugins(allConfigPlugins); } resolvePresets(presets) { const allPresets = (0, utils_1.resolvePresetsOrPlugins)(this.appPath, presets, constants_1.PluginType.Preset); while (allPresets.length) { this.initPreset(allPresets.shift()); } } resolvePlugins(plugins) { plugins = (0, lodash_1.merge)(this.extraPlugins, plugins); const allPlugins = (0, utils_1.resolvePresetsOrPlugins)(this.appPath, plugins, constants_1.PluginType.Plugin); while (allPlugins.length) { this.initPlugin(allPlugins.shift()); } this.extraPlugins = {}; } initPreset(preset) { this.debugger('initPreset', preset); const { id, path, opts, apply } = preset; const pluginCtx = this.initPluginCtx({ id, path, ctx: this }); const { presets, plugins } = apply()(pluginCtx, opts) || {}; this.registerPlugin(preset); if (Array.isArray(presets)) { const _presets = (0, utils_1.resolvePresetsOrPlugins)(this.appPath, (0, utils_1.convertPluginsToObject)(presets)(), constants_1.PluginType.Preset); while (_presets.length) { this.initPreset(_presets.shift()); } } if (Array.isArray(plugins)) { this.extraPlugins = (0, lodash_1.merge)(this.extraPlugins, (0, utils_1.convertPluginsToObject)(plugins)()); } } initPlugin(plugin) { const { id, path, opts, apply } = plugin; const pluginCtx = this.initPluginCtx({ id, path, ctx: this }); this.debugger('initPlugin', plugin); this.registerPlugin(plugin); apply()(pluginCtx, opts); this.checkPluginOpts(pluginCtx, opts); } checkPluginOpts(pluginCtx, opts) { if (typeof pluginCtx.optsSchema !== 'function') { return; } this.debugger('checkPluginOpts', pluginCtx); const joi = require('joi'); const schema = pluginCtx.optsSchema(joi); if (!joi.isSchema(schema)) { throw new Error(`Plugins${pluginCtx.id}_Other Organiser schema There's been a mistake.,Check it out, please.!`); } const { error } = schema.validate(opts); if (error) { error.message = `Plugins${pluginCtx.id}The parameters obtained did not meet the requirements,Check it out, please.!`; throw error; } } registerPlugin(plugin) { this.debugger('registerPlugin', plugin); if (this.plugins.has(plugin.id)) { throw new Error(`Plugins ${plugin.id} Registered`); } this.plugins.set(plugin.id, plugin); } initPluginCtx({ id, path, ctx }) { const pluginCtx = new Plugin_1.default({ id, path, ctx }); const internalMethods = ['onReady', 'onStart']; const kernelApis = [ 'appPath', 'plugins', 'platforms', 'paths', 'helper', 'runOpts', 'initialConfig', 'applyPlugins' ]; internalMethods.forEach(name => { if (!this.methods.has(name)) { pluginCtx.registerMethod(name); } }); return new Proxy(pluginCtx, { get: (target, name) => { if (this.methods.has(name)) { const method = this.methods.get(name); if (Array.isArray(method)) { return (...arg) => { method.forEach(item => { item.apply(this, arg); }); }; } return method; } if (kernelApis.includes(name)) { return typeof this[name] === 'function' ? this[name].bind(this) : this[name]; } return target[name]; } }); } applyPlugins(args) { return __awaiter(this, void 0, void 0, function* () { let name; let initialVal; let opts; if (typeof args === 'string') { name = args; } else { name = args.name; initialVal = args.initialVal; opts = args.opts; } this.debugger('applyPlugins'); this.debugger(`applyPlugins:name:${name}`); this.debugger(`applyPlugins:initialVal:${initialVal}`); this.debugger(`applyPlugins:opts:${opts}`); if (typeof name !== 'string') { throw new Error('Call Failed,No correct name entered!'); } const hooks = this.hooks.get(name) || []; if (!hooks.length) { return yield initialVal; } const waterfall = new tapable_1.AsyncSeriesWaterfallHook(['arg']); if (hooks.length) { const resArr = []; for (const hook of hooks) { waterfall.tapPromise({ name: hook.plugin, stage: hook.stage || 0, // @ts-ignore before: hook.before }, (arg) => __awaiter(this, void 0, void 0, function* () { const res = yield hook.fn(opts, arg); if (constants_1.IS_MODIFY_HOOK.test(name) && constants_1.IS_EVENT_HOOK.test(name)) { return res; } if (constants_1.IS_ADD_HOOK.test(name)) { resArr.push(res); return resArr; } return null; })); } } return yield waterfall.promise(initialVal); }); } runWithPlatform(platform) { if (!this.platforms.has(platform)) { throw new Error(`No compilation platform exists ${platform}`); } const withNameConfig = this.config.getConfigWithNamed(platform, this.platforms.get(platform).useConfigName); return withNameConfig; } setRunOpts(opts) { this.runOpts = opts; } runHelp(name) { const command = this.commands.get(name); const defaultOptionsMap = new Map(); defaultOptionsMap.set('-h, --help', 'output usage information'); let customOptionsMap = new Map(); if (command === null || command === void 0 ? void 0 : command.optionsMap) { customOptionsMap = new Map(Object.entries(command === null || command === void 0 ? void 0 : command.optionsMap)); } const optionsMap = new Map([...customOptionsMap, ...defaultOptionsMap]); (0, utils_1.printHelpLog)(name, optionsMap, (command === null || command === void 0 ? void 0 : command.synopsisList) ? new Set(command === null || command === void 0 ? void 0 : command.synopsisList) : new Set()); } run(args) { var _a; return __awaiter(this, void 0, void 0, function* () { let name; let opts; if (typeof args === 'string') { name = args; } else { name = args.name; opts = args.opts; } this.debugger('command:run'); this.debugger(`command:run:name:${name}`); this.debugger('command:runOpts'); this.debugger(`command:runOpts:${JSON.stringify(opts, null, 2)}`); this.setRunOpts(opts); this.debugger('initPresetsAndPlugins'); this.initPresetsAndPlugins(); yield this.applyPlugins('onReady'); this.debugger('command:onStart'); yield this.applyPlugins('onStart'); if (!this.commands.has(name)) { throw new Error(`${name} Command does not exist`); } if (opts === null || opts === void 0 ? void 0 : opts.isHelp) { return this.runHelp(name); } if ((_a = opts === null || opts === void 0 ? void 0 : opts.options) === null || _a === void 0 ? void 0 : _a.platform) { opts.config = this.runWithPlatform(opts.options.platform); yield this.applyPlugins({ name: 'modifyRunnerOpts', opts: { opts: opts === null || opts === void 0 ? void 0 : opts.config } }); } yield this.applyPlugins({ name, opts }); }); } } exports.default = Kernel; //# sourceMappingURL=Kernel.js.map