|
| 1 | +# Module System |
| 2 | + |
| 3 | +一个简易的模块管理系统,主要职责: |
| 4 | +1. 管理模块列表 |
| 5 | +2. 管理模块的生命周期 |
| 6 | +3. 定义模块间的交互方式 |
| 7 | + |
| 8 | +# 模块设计文档: Module System |
| 9 | + |
| 10 | +[](https://www.npmjs.com/package/@itharbors/module) |
| 11 | +[](https://github.com/itharbors/module/actions/workflows/ci.yaml) |
| 12 | + |
| 13 | +一个简易的模块管理系统,主要职责: |
| 14 | +1. 管理模块列表 |
| 15 | +2. 管理模块的生命周期 |
| 16 | +3. 定义模块间的交互方式 |
| 17 | + |
| 18 | +## 需求分析 |
| 19 | + |
| 20 | +### 功能需求 |
| 21 | + |
| 22 | +- 管理模块的生命周期函数 |
| 23 | + - register、load、unload、unregister |
| 24 | +- 管理模块对外暴露的方法 |
| 25 | + - 将所有方法收敛到一个出入口 |
| 26 | +- 简易的数据管理 |
| 27 | + - 定义、修改数据 |
| 28 | + - 数据变化的监听 |
| 29 | +- 对象上的缓存数据管理 |
| 30 | + |
| 31 | +### 非功能需求 |
| 32 | + |
| 33 | +- 无 |
| 34 | + |
| 35 | +## 整体架构设计 |
| 36 | + |
| 37 | +## 代码范例 |
| 38 | + |
| 39 | +### 基础用法 |
| 40 | + |
| 41 | +```ts |
| 42 | +import { generateModule } from '@itharbors/module'; |
| 43 | + |
| 44 | +export const instance = generateModule({ |
| 45 | + stash(): {} { |
| 46 | + return {}; |
| 47 | + }, |
| 48 | + |
| 49 | + data(): {} { |
| 50 | + return {}; |
| 51 | + }, |
| 52 | + |
| 53 | + register() { |
| 54 | + |
| 55 | + }, |
| 56 | + |
| 57 | + register() { |
| 58 | + |
| 59 | + }, |
| 60 | + |
| 61 | + load() { |
| 62 | + |
| 63 | + }, |
| 64 | + |
| 65 | + unload() { |
| 66 | + |
| 67 | + }, |
| 68 | + |
| 69 | + method: { |
| 70 | + async test(num: string) { |
| 71 | + return num + 1; |
| 72 | + } |
| 73 | + }, |
| 74 | +}); |
| 75 | + |
| 76 | + |
| 77 | +instance.run('register'); |
| 78 | +instance.run('load'); |
| 79 | +instance.run('unload'); |
| 80 | +instance.run('unregister'); |
| 81 | + |
| 82 | +const num = await instance.execture('test', 1); // 2 |
| 83 | +``` |
| 84 | + |
| 85 | +```ts |
| 86 | +// params 是 initWorkflow 时传入的那个 params |
| 87 | +// 主要用于项目内控制部分流程,比如构建的时候只构建脚本、只构建样式等功能 |
| 88 | +exports.remove = function(params) { |
| 89 | + return ['./dist']; |
| 90 | +}; |
| 91 | +exports.npm = function(params) { |
| 92 | + return [{ |
| 93 | + message: '安装依赖', |
| 94 | + path: './', |
| 95 | + params: ['install'], |
| 96 | + detail: '依赖安装失败,请检查网络和配置', |
| 97 | + }]; |
| 98 | +}; |
| 99 | +exports.tsc = function(params) { |
| 100 | + return ['./']; |
| 101 | +}; |
| 102 | +``` |
| 103 | + |
| 104 | +### 配置文件 |
| 105 | + |
| 106 | +### 注册自定义任务 |
| 107 | + |
| 108 | +```ts |
| 109 | +import { readFile } from 'fs'; |
| 110 | +import { ModuleContainer, TModule } from '@itharbors/module'; |
| 111 | + |
| 112 | +export class Plugin extends ModuleContainer { |
| 113 | + public info: TPluginInfo; |
| 114 | + public path: string; |
| 115 | + |
| 116 | + constructor(path: string) { |
| 117 | + const pkg = require(join(path, 'package.json')); |
| 118 | + const module = require(pkg.main) as Partial<TModule>; |
| 119 | + super({ |
| 120 | + stash: module.stash || function () { return {}; }, |
| 121 | + data: module.data || function () { return {}; }, |
| 122 | + method: module.method || {}, |
| 123 | + }); |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +## 决策点 |
| 129 | + |
| 130 | +- 无 |
| 131 | + |
| 132 | +## 异常处理设计 |
| 133 | + |
| 134 | +- 无 |
| 135 | + |
| 136 | +## 性能优化 |
| 137 | + |
| 138 | +- 无 |
| 139 | + |
| 140 | +## 附件与参考文档 |
| 141 | + |
| 142 | +- 无 |
0 commit comments