Skip to content

Commit

Permalink
improve(codemod): @alipay/ob-ui => @oceanbase/ui for locale
Browse files Browse the repository at this point in the history
  • Loading branch information
dengfuping committed Oct 20, 2023
1 parent c9e0c1c commit d430340
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import enUS from '@alipay/ob-ui/es/locale/en-US';
import zhCN from '@alipay/ob-ui/es/locale/zh-CN';

const Demo = () => {
return <div />;
};

export default Demo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import enUS from '@oceanbase/ui/es/locale/en-US';
import zhCN from '@oceanbase/ui/es/locale/zh-CN';

const Demo = () => {
return <div />;
};

export default Demo;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { Alert, Button, BackgroundTaskManager, BackgroundTaskManagerConstants, BasicLayout, Login, PageContainer } from '@alipay/ob-ui';
import { Alert, Button, BackgroundTaskManager, BackgroundTaskManagerConstants, BasicLayout, Boundary, ConfigProvider, Login, PageContainer, theme } from '@alipay/ob-ui';
import type { BackgroundTaskManagerRef, ITaskMgrPreset, ITaskMgrQueue, TaskMgrID } from '@alipay/ob-ui';
import type { BasicLayoutProps } from '@alipay/ob-ui/es/BasicLayout';
import type { LoginProps } from '@alipay/ob-ui/es/Login';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import { Alert, Button } from '@oceanbase/design';
import { BackgroundTaskManager, BackgroundTaskManagerConstants, BasicLayout, Login, PageContainer } from '@oceanbase/ui';
import { Alert, Button, ConfigProvider, theme } from '@oceanbase/design';
import { BackgroundTaskManager, BackgroundTaskManagerConstants, BasicLayout, Boundary, Login, PageContainer } from '@oceanbase/ui';
import type { BackgroundTaskManagerRef, ITaskMgrPreset, ITaskMgrQueue, TaskMgrID } from '@oceanbase/ui';
import type { BasicLayoutProps } from '@oceanbase/ui/es/BasicLayout';
import type { LoginProps } from '@oceanbase/ui/es/Login';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineTest } from 'jscodeshift/src/testUtils';

const testUnit = 'obui-to-oceanbase-design-and-ui';
const tests = ['obui'];
const tests = ['obui', 'locale'];

describe(testUnit, () => {
tests.forEach(test =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ module.exports = (file, api, options) => {
'LottieProps',
'NavMenuProps',
],
paths: ['/locale/', '/locale/'],
},
{
name: '@oceanbase/design',
Expand Down
26 changes: 15 additions & 11 deletions packages/codemod/transforms/utils/import-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { markDependency } = require('./marker');
const { printOptions } = require('./config');
const { isPlainObject } = require('lodash');
const { flatten } = require('lodash');
const { some } = require('lodash');

function importComponent(j, root, options) {
const { fromPkgNames, toPkgList } = options;
Expand All @@ -19,44 +20,47 @@ function importComponent(j, root, options) {
path.value.specifiers.forEach(specifier => {
const toPkgByComponents = toPkgList.find(
toPkg =>
toPkg.components?.includes(specifier.imported.name) ||
toPkg.components?.find(component => component[specifier.imported.name])
toPkg.components?.includes(specifier.imported?.name) ||
toPkg.components?.find(component => component[specifier.imported?.name])
);
const toPkgByTypes = toPkgList.find(
toPkg =>
toPkg.types?.includes(specifier.imported.name) ||
toPkg.types?.find(type => type[specifier.imported.name])
toPkg.types?.includes(specifier.imported?.name) ||
toPkg.types?.find(type => type[specifier.imported?.name])
);
const toPkg = toPkgByComponents || toPkgByTypes;
const toPkgByPaths = toPkgList.find(toPkg =>
some(toPkg.paths, pathString => path.value.source.value?.includes(pathString))
);
const toPkg = toPkgByComponents || toPkgByTypes || toPkgByPaths;
if (toPkg) {
// replace to toPkg for xxx/es/xxx、xxx/lib/xxx
if (new RegExp(`${fromPkgName}/(es|lib|locale)/`).test(path.value.source.value)) {
path.value.source.value = path.value.source.value?.replace(fromPkgName, toPkg.name);
} else {
// remove old imports
path.value.specifiers = path.value.specifiers.filter(
item => !item.imported || item.imported.name !== specifier.imported.name
item => !item.imported || item.imported?.name !== specifier.imported?.name
);
const renameComponent = toPkg.components?.find(
component => component[specifier.imported.name]
component => component[specifier.imported?.name]
);
const renameType = toPkg.types?.find(type => type[specifier.imported.name]);
const renameType = toPkg.types?.find(type => type[specifier.imported?.name]);
const rename = renameComponent || renameType;
// add and rename new imports
addSubmoduleImport(j, root, {
moduleName: toPkg.name,
importedName: rename ? rename[specifier.imported.name] : specifier.imported.name,
importedName: rename ? rename[specifier.imported?.name] : specifier.imported?.name,
importKind: toPkgByTypes ? 'type' : 'value',
after: fromPkgName,
});
// rename used part
if (rename) {
root
.find(j.Identifier, {
name: specifier.imported.name,
name: specifier.imported?.name,
})
.forEach(path => {
path.node.name = rename[specifier.imported.name];
path.node.name = rename[specifier.imported?.name];
});
}
}
Expand Down

0 comments on commit d430340

Please sign in to comment.