|
1 | 1 | import { registerAction } from 'utils/actions' |
2 | 2 | import { PluginRegistry } from 'utils/plugins' |
3 | | -import React from 'react' |
4 | 3 | import { autorun, observable } from 'mobx' |
5 | 4 | import config from 'config' |
6 | 5 | import { notify, NOTIFY_TYPE } from 'components/Notification/actions' |
@@ -167,31 +166,50 @@ export const fetchUserPackage = registerAction(FETCH_USER_PACKAGE, (pkg) => { |
167 | 166 | .then(pkgId => togglePackage({ pkgId, shouldEnable: true, type: 'Required' })) |
168 | 167 | }) |
169 | 168 |
|
| 169 | +const loadUserPackages = async (packages) => { |
| 170 | + const convert = packages.map((p) => ({ |
| 171 | + name: p.pluginName, |
| 172 | + author: p.createdBy || '', |
| 173 | + description: p.remark, |
| 174 | + displayName: p.pluginName, |
| 175 | + enabled: true, |
| 176 | + id: p.id, |
| 177 | + requirement: 'Required', |
| 178 | + status: 'Available', |
| 179 | + version: p.currentVersion || '[pre deploy]', |
| 180 | + userPlugin: true, |
| 181 | + filePath: p.pluginFilePath |
| 182 | + })) |
| 183 | + |
| 184 | + /* eslint-disable */ |
| 185 | + for (const pkg of convert) { |
| 186 | + store.list.push(pkg) |
| 187 | + try { |
| 188 | + await fetchUserPackage(pkg) |
| 189 | + /* eslint-enable */ |
| 190 | + } catch (err) { |
| 191 | + console.error(`Failed to load plugin ${pkg.name}: ${err.message}.`) |
| 192 | + } |
| 193 | + } |
| 194 | +} |
| 195 | + |
170 | 196 | export const loadPackagesByUser = registerAction(PRELOAD_USER_EXTENSION, () => { |
171 | 197 | const userPackagePromise = api.fetchUserPackagelist() |
172 | | - return userPackagePromise.then(async (response) => { |
173 | | - if (response.code === 0) { |
174 | | - const { data } = response |
175 | | - const convert = data.map((p) => ({ |
176 | | - name: p.pluginName, |
177 | | - author: p.createdBy, |
178 | | - description: p.remark, |
179 | | - displayName: p.pluginName, |
180 | | - enabled: true, |
181 | | - id: p.id, |
182 | | - requirement: 'Required', |
183 | | - status: 'Available', |
184 | | - version: p.currentVersion, |
185 | | - userPlugin: true, |
186 | | - filePath: p.pluginFilePath |
187 | | - })) |
188 | | - |
189 | | - for (const pkg of convert) { |
190 | | - store.list.push(pkg) |
191 | | - await fetchUserPackage(pkg) |
192 | | - } |
193 | | - } |
194 | | - }) |
| 198 | + const preDeployPackagePromise = api.fethUserPreDeployPlugins() |
| 199 | + return Promise.all([userPackagePromise, preDeployPackagePromise]) |
| 200 | + .then(async ([userpackages, preDeployPackages]) => { |
| 201 | + const userPlugins = userpackages.data |
| 202 | + const preDeployPlugins = preDeployPackages.data |
| 203 | + const enableUserPackages = userPlugins.reduce((pre, cur) => { |
| 204 | + if (!preDeployPlugins.find(prePackage => prePackage.id === cur.id)) { |
| 205 | + pre.push(cur) |
| 206 | + } |
| 207 | + return pre |
| 208 | + }, []) |
| 209 | + store.preDeployPlugins = preDeployPlugins |
| 210 | + |
| 211 | + loadUserPackages([...enableUserPackages, ...preDeployPlugins]) |
| 212 | + }) |
195 | 213 | }) |
196 | 214 |
|
197 | 215 | export const mountPackagesByType = (type) => { |
@@ -275,7 +293,7 @@ export const startRemoteHMRServer = registerAction('plugin:mount', () => { |
275 | 293 | }) |
276 | 294 |
|
277 | 295 | devSocket.on('connect_error', () => { |
278 | | - notify({ notifyType: NOTIFY_TYPE.ERROR, message: '无法连接到插件开发服务,请确保已经执行 yarn start 且服务正常启动' }) |
| 296 | + notify({ notifyType: NOTIFY_TYPE.ERROR, message: '无法连接到插件服务,请确保已经执行 yarn start 且正常启动' }) |
279 | 297 | }) |
280 | 298 |
|
281 | 299 | devSocket.on('progress', (progress) => { |
@@ -347,7 +365,7 @@ export const startRemoteHMRServer = registerAction('plugin:mount', () => { |
347 | 365 | devSocket.on('disconnect', () => { |
348 | 366 | notify({ |
349 | 367 | notifyType: NOTIFY_TYPE.INFO, |
350 | | - message: '插件开发服务已断开连接' |
| 368 | + message: '插件已断开连接' |
351 | 369 | }) |
352 | 370 | store.pluginDevState.online = false |
353 | 371 | const { infomation: { name, version } } = store.pluginDevState |
|
0 commit comments