Skip to content

Commit

Permalink
feat(block): 检测区块特性和本地项目的匹配度
Browse files Browse the repository at this point in the history
  • Loading branch information
sorrycc committed Sep 29, 2019
1 parent 86fd74a commit 3b7c216
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export default (api: IApi) => {
success: true,
});
break;

// 取消任务
case 'org.umi.block.cancel':
success({
Expand Down Expand Up @@ -179,16 +180,84 @@ export default (api: IApi) => {
break;

case 'org.umi.block.checkIfCanAdd':
function haveFeature(feature) {
return payload.item.features && payload.item.features.includes(feature);
}

if (!api.config.routes) {
failure({
return failure({
message:
lang === 'zh-CN'
? '区块添加不支持约定式路由,请转成配置式路由。'
: 'The block adding does not support the conventional route, please convert to a configuration route.',
});
} else {
success({});
}

const payloadType = payload.type === 'block' ? '区块' : '模板';
const isBigfish = !!process.env.BIGFISH_COMPAT;
const reactPlugin = api.config.plugins.find(p => {
return p === 'umi-plugin-react' || p[0] === 'umi-plugin-react';
});
const reactPluginOpts = reactPlugin
? typeof reactPlugin === 'string'
? {}
: reactPlugin[1]
: {};

// antd 特性依赖
// bigfish 默认开了 antd
if (haveFeature('antd') && !isBigfish) {
if (!reactPlugin || !reactPluginOpts.antd) {
return failure({
message:
lang === 'zh-CN'
? `${payloadType}依赖 antd,请安装 umi-plugin-react 插件并开启 antd 。`
: 'Block depends on antd, please install umi-plugin-react and enable antd.',
});
}
}

// dva 特性依赖
if (haveFeature('dva')) {
if (isBigfish) {
if (!api.config.dva) {
return failure({
message: `${payloadType}依赖 dva,请开启 dva 配置。`,
});
}
} else {
if (!reactPlugin || !reactPluginOpts.locale) {
return failure({
message:
lang === 'zh-CN'
? `${payloadType}依赖 dva,请安装 umi-plugin-react 插件并开启 dva 。`
: 'Block depends on dva, please install umi-plugin-react and enable dva.',
});
}
}
}

// locale 特性依赖
if (haveFeature('i18n')) {
if (isBigfish) {
if (!api.config.locale) {
return failure({
message: `${payloadType}依赖 locale,请开启 locale 配置。`,
});
}
} else {
if (!reactPlugin || !reactPluginOpts.locale) {
return failure({
message:
lang === 'zh-CN'
? `${payloadType}依赖国际化(i18n),请安装 umi-plugin-react 插件并开启 locale 。`
: 'Block depends on i18n, please install umi-plugin-react and enable locale.',
});
}
}
}

success({});
break;

// 检查路由是否存在
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ const onBeforeOpenModal = async (api, { item, type, onShowModal }) => {
try {
await api.callRemote({
type: 'org.umi.block.checkIfCanAdd',
payload: {
item,
type,
},
});
} catch (e) {
message.error(e.message);
Expand Down

0 comments on commit 3b7c216

Please sign in to comment.