Skip to content

Commit

Permalink
feat: new command add already enable function tooltip (#5751)
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin authored May 16, 2024
1 parent bcf78e7 commit e759876
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
8 changes: 8 additions & 0 deletions .changeset/tall-bobcats-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@modern-js/generator-common': patch
'@modern-js/new-action': patch
---

feat: new command add already enable function tooltip

feat: new 命令增加已经开启功能的提示
15 changes: 5 additions & 10 deletions packages/generator/generator-common/src/newAction/module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ export const ModuleActionTypesMap: Record<string, string[]> = {

export const ModuleSpecialSchemaMap: Record<string, Schema> = {};

export const getModuleNewActionSchema = (
extra: Record<string, any> = {},
): Schema => {
const { funcMap = {} } = extra;
export const getModuleNewActionSchema = (): Schema => {
return {
type: 'object',
properties: {
Expand All @@ -42,12 +39,10 @@ export const getModuleNewActionSchema = (
[ActionType.Function]: {
type: 'string',
title: ActionTypeQuestionText[ActionType.Function](),
enum: ModuleActionFunctions.filter(func => !funcMap[func]).map(
func => ({
value: func,
label: ActionFunctionText[func](),
}),
),
enum: ModuleActionFunctions.map(func => ({
value: func,
label: ActionFunctionText[func](),
})),
'x-reactions': [
{
dependencies: ['actionType'],
Expand Down
27 changes: 8 additions & 19 deletions packages/generator/generator-common/src/newAction/mwa/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,18 @@ export const MWAActionTypesMap: Record<ActionType, string[]> = {
[ActionType.Refactor]: MWAActionReactors,
};

export const getMWANewActionSchema = (
extra: Record<string, any> = {},
): Schema => {
const { funcMap = {}, refactorMap = {} } = extra;
const funcs = MWAActionFunctions.filter(func => !funcMap[func]);
const refactors = MWAActionReactors.filter(reactor => !refactorMap[reactor]);
export const getMWANewActionSchema = (): Schema => {
return {
type: 'object',
properties: {
actionType: {
type: 'string',
title: i18n.t(localeKeys.action.self),
enum: MWAActionTypes.filter(type =>
type === ActionType.Function ? funcs.length > 0 : true,
)
.filter(type =>
type === ActionType.Refactor ? refactors.length > 0 : true,
)
.map(type => ({
value: type,
label: ActionTypeText[type](),
type: ['string'],
})),
enum: MWAActionTypes.map(type => ({
value: type,
label: ActionTypeText[type](),
type: ['string'],
})),
},
[ActionType.Element]: {
type: 'string',
Expand All @@ -85,7 +74,7 @@ export const getMWANewActionSchema = (
[ActionType.Function]: {
type: 'string',
title: ActionTypeQuestionText[ActionType.Function](),
enum: funcs.map(func => ({
enum: MWAActionFunctions.map(func => ({
value: func,
label: ActionFunctionText[func](),
})),
Expand All @@ -103,7 +92,7 @@ export const getMWANewActionSchema = (
[ActionType.Refactor]: {
type: 'string',
title: ActionTypeQuestionText[ActionType.Refactor](),
enum: refactors.map(refactor => ({
enum: MWAActionReactors.map(refactor => ({
value: refactor,
label: ActionRefactorText[refactor](),
})),
Expand Down
4 changes: 4 additions & 0 deletions packages/generator/new-action/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const enableAlreadyText: Record<string, string> = {
zh: '当前功能已开启,请确认对应插件依赖是否已安装。若需重新开启该功能,请移除插件依赖后再次尝试。',
en: 'The current function has been enabled. Please confirm whether the corresponding plugin dependencies have been installed. If you need to re-enable this function, please remove the plugin dependencies and try again.',
};
10 changes: 8 additions & 2 deletions packages/generator/new-action/src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
hasEnabledFunction,
usePluginNameExport,
} from './utils';
import { enableAlreadyText } from './constants';

interface IModuleNewActionOption {
locale?: string;
Expand Down Expand Up @@ -55,7 +56,8 @@ export const ModuleNewAction = async (options: IModuleNewActionOption) => {
throw new Error('config is not a valid json');
}

i18n.changeLanguage({ locale: (UserConfig.locale as string) || locale });
const language = (UserConfig.locale as string) || locale;
i18n.changeLanguage({ locale: language });

const smith = new CodeSmith({
debug,
Expand Down Expand Up @@ -98,13 +100,17 @@ export const ModuleNewAction = async (options: IModuleNewActionOption) => {

const ans = await formilyAPI.getInputBySchemaFunc(getModuleNewActionSchema, {
...UserConfig,
funcMap,
});

const actionType = ans.actionType as ActionType;

const action = ans[actionType] as string;

if (actionType === ActionType.Function && funcMap[action as ActionFunction]) {
smith.logger.error(enableAlreadyText[language]);
return;
}

const generator = getGeneratorPath(
ModuleNewActionGenerators[actionType]![action],
distTag,
Expand Down
15 changes: 12 additions & 3 deletions packages/generator/new-action/src/mwa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
hasEnabledFunction,
usePluginNameExport,
} from './utils';
import { enableAlreadyText } from './constants';

interface IMWANewActionOption {
locale?: string;
Expand Down Expand Up @@ -59,7 +60,8 @@ export const MWANewAction = async (options: IMWANewActionOption) => {
throw new Error('config is not a valid json');
}

i18n.changeLanguage({ locale: (UserConfig.locale as string) || locale });
const language = (UserConfig.locale as string) || locale;
i18n.changeLanguage({ locale: language });

const smith = new CodeSmith({
debug,
Expand Down Expand Up @@ -104,14 +106,21 @@ export const MWANewAction = async (options: IMWANewActionOption) => {

const ans = await formilyAPI.getInputBySchemaFunc(getMWANewActionSchema, {
...UserConfig,
funcMap,
refactorMap,
});

const actionType = ans.actionType as ActionType;

const action = ans[actionType] as string;

if (
(actionType === ActionType.Function && funcMap[action as ActionFunction]) ||
(actionType === ActionType.Refactor &&
refactorMap[action as ActionRefactor])
) {
smith.logger.error(enableAlreadyText[language]);
return;
}

const generator = getGeneratorPath(
MWANewActionGenerators[actionType][action],
distTag,
Expand Down

0 comments on commit e759876

Please sign in to comment.