Skip to content

Commit

Permalink
feat(codemod): Add @oceanbase/design color token map for style-to-tok…
Browse files Browse the repository at this point in the history
…en and less-to-token transformer
  • Loading branch information
dengfuping committed Apr 3, 2024
1 parent 6493aa8 commit d8850e2
Show file tree
Hide file tree
Showing 5 changed files with 4,552 additions and 13,481 deletions.
1 change: 1 addition & 0 deletions packages/codemod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"build": "father build"
},
"dependencies": {
"@oceanbase/design": "workspace:^",
"chalk": "^3.0.0",
"command-exists": "^1.2.9",
"execa": "^5.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { Alert, Button, Tooltip } from '@oceanbase/design';

const Demo = () => {
const tokenList = ['rgb(0 0 0 / 45%)'];
const tokenList = ['rgb(0 0 0 / 45%)', '#006AFF', '#f3f6fc'];
return (
<div>
<Alert style={{ color: 'rgba(0, 0, 0, 0.85)', background: 'rgba(0, 0, 0,0.65)', backgroundColor: 'rgba(0,0,0,0.45)', border: '1px solid #d9d9d9' }} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Alert, Button, theme, Tooltip } from '@oceanbase/design';

const Demo = () => {
const { token } = theme.useToken();
const tokenList = [token.colorTextTertiary];
const tokenList = [token.colorTextTertiary, token.colorInfo, token.colorBgLayout];
return (
(<div>
<Alert style={{ color: token.colorText, background: token.colorTextSecondary, backgroundColor: token.colorTextTertiary, border: `1px solid ${token.colorBorder}` }} />
Expand Down
35 changes: 32 additions & 3 deletions packages/codemod/transforms/utils/token.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
const { toLower } = require('lodash');
const { isString, fromPairs, toLower } = require('lodash');
const { default: defaultTheme } = require('@oceanbase/design/lib/theme/default');

Check failure on line 2 in packages/codemod/transforms/utils/token.js

View workflow job for this annotation

GitHub Actions / test (16.x, ubuntu-latest)

transforms/__tests__/less-to-token.test.ts

Error: Cannot find module '@oceanbase/design/lib/theme/default' Require stack: - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/less-to-token.js ❯ Object.<anonymous> transforms/utils/token.js:2:35 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js', '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/less-to-token.js' ] }

Check failure on line 2 in packages/codemod/transforms/utils/token.js

View workflow job for this annotation

GitHub Actions / test (16.x, ubuntu-latest)

transforms/__tests__/style-to-token.test.ts > style-to-token > style-to-token > transforms correctly using "style-to-token/function-component" data

Error: Cannot find module '@oceanbase/design/lib/theme/default' Require stack: - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js - /home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js ❯ Object.<anonymous> transforms/utils/token.js:2:35 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js', '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js', '/home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js' ] }

Check failure on line 2 in packages/codemod/transforms/utils/token.js

View workflow job for this annotation

GitHub Actions / test (16.x, ubuntu-latest)

transforms/__tests__/style-to-token.test.ts > style-to-token > style-to-token > transforms correctly using "style-to-token/class-component" data

Error: Cannot find module '@oceanbase/design/lib/theme/default' Require stack: - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js - /home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js ❯ Object.<anonymous> transforms/utils/token.js:2:35 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js', '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js', '/home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js' ] }

Check failure on line 2 in packages/codemod/transforms/utils/token.js

View workflow job for this annotation

GitHub Actions / test (16.x, ubuntu-latest)

transforms/__tests__/style-to-token.test.ts > style-to-token > style-to-token > transforms correctly using "style-to-token/block-statement" data

Error: Cannot find module '@oceanbase/design/lib/theme/default' Require stack: - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js - /home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js ❯ Object.<anonymous> transforms/utils/token.js:2:35 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js', '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js', '/home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js' ] }

Check failure on line 2 in packages/codemod/transforms/utils/token.js

View workflow job for this annotation

GitHub Actions / test (16.x, ubuntu-latest)

transforms/__tests__/style-to-token.test.ts > style-to-token > style-to-token > transforms correctly using "style-to-token/nested-block-statement" data

Error: Cannot find module '@oceanbase/design/lib/theme/default' Require stack: - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js - /home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js ❯ Object.<anonymous> transforms/utils/token.js:2:35 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js', '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js', '/home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js' ] }

Check failure on line 2 in packages/codemod/transforms/utils/token.js

View workflow job for this annotation

GitHub Actions / test (16.x, ubuntu-latest)

transforms/__tests__/style-to-token.test.ts > style-to-token > style-to-token > transforms correctly using "style-to-token/existed-useToken" data

Error: Cannot find module '@oceanbase/design/lib/theme/default' Require stack: - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js - /home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js ❯ Object.<anonymous> transforms/utils/token.js:2:35 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js', '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js', '/home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js' ] }

Check failure on line 2 in packages/codemod/transforms/utils/token.js

View workflow job for this annotation

GitHub Actions / test (16.x, ubuntu-latest)

transforms/__tests__/style-to-token.test.ts > style-to-token > style-to-token > transforms correctly using "style-to-token/top-identifier" data

Error: Cannot find module '@oceanbase/design/lib/theme/default' Require stack: - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js - /home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js ❯ Object.<anonymous> transforms/utils/token.js:2:35 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js', '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js', '/home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js' ] }

Check failure on line 2 in packages/codemod/transforms/utils/token.js

View workflow job for this annotation

GitHub Actions / test (16.x, ubuntu-latest)

transforms/__tests__/style-to-token.test.ts > style-to-token > style-to-token > transforms correctly using "style-to-token/case-insensitive" data

Error: Cannot find module '@oceanbase/design/lib/theme/default' Require stack: - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js - /home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js ❯ Object.<anonymous> transforms/utils/token.js:2:35 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js', '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/style-to-token.js', '/home/runner/work/oceanbase-design/oceanbase-design/node_modules/.pnpm/[email protected]_@[email protected]/node_modules/jscodeshift/src/testUtils.js' ] }

Check failure on line 2 in packages/codemod/transforms/utils/token.js

View workflow job for this annotation

GitHub Actions / test (16.x, ubuntu-latest)

transforms/__tests__/token.test.ts

Error: Cannot find module '@oceanbase/design/lib/theme/default' Require stack: - /home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js ❯ transforms/utils/token.js:2:35 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'MODULE_NOT_FOUND', requireStack: [ '/home/runner/work/oceanbase-design/oceanbase-design/packages/codemod/transforms/utils/token.js' ] }

const token = defaultTheme.token;

const tokenMap = fromPairs(
Object.keys(token)
.filter(key =>
// filter token by token key
key.startsWith('color')
)
.filter(
key =>
// filter token by token value
isString(token[key]) && !!token[key]
)
.map(key => {
return [token[key], key];
})
// sort by token value and token key length
.sort((a, b) => {
if (a[0] > b[0]) {
return 1;
} else if (a[0] < b[0]) {
return -1;
}
return a[1]?.length > b[1]?.length ? -1 : a[1]?.length < b[1]?.length ? 1 : 0;
})
);

const TOKEN_MAP = {
// obui 3.x style => token
...tokenMap,
// antd style => token
'#1677ff': 'colorInfo',
'#1890ff': 'colorInfo',
Expand Down Expand Up @@ -51,8 +81,7 @@ const TOKEN_MAP = {
'rgba(0,0,0,0.04)': 'colorBgLayout',
'#f5f6fa': 'colorBgLayout',
'#edeff2': 'colorBgLayout',
// obui style => token
'#006aff': 'colorInfo',
// obui legacy style => token
'#086fff': 'colorInfo',
'rgba(24,144,255,0.1)': 'colorInfoBg',
'rgba(95,149,255,0.10)': 'colorInfoBg',
Expand Down
Loading

0 comments on commit d8850e2

Please sign in to comment.