@oceanbase/codemod 1.0.0-alpha.4 → 1.0.0-alpha.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/cli.js CHANGED
@@ -142,14 +142,21 @@ async function upgradeDetect(targetDir, needOBCharts, needObUtil) {
142
142
  const result = [];
143
143
  const cwd = path.join(process.cwd(), targetDir);
144
144
  const { readPackageUp } = await import('read-pkg-up');
145
- const closetPkgJson = await readPackageUp({ cwd });
145
+ let closetPkgJson;
146
+ try {
147
+ closetPkgJson = await readPackageUp({ cwd });
148
+ } catch (err) {
149
+ // 处理无效的 package.json 文件(如版本格式错误)
150
+ console.log(chalk.yellow(`Warning: Failed to read package.json: ${err.message}`));
151
+ closetPkgJson = null;
152
+ }
146
153
 
147
154
  let pkgJsonPath;
148
155
  if (!closetPkgJson) {
149
156
  pkgJsonPath = "we didn't find your package.json";
150
157
  // unknown dependency property
151
- result.push(['install', '@oceanbase/design', pkgUpgradeList['@oceanbase/design']]);
152
- result.push(['install', '@oceanbase/ui', pkgUpgradeList['@oceanbase/ui']]);
158
+ result.push(['install', '@oceanbase/design', pkgUpgradeList['@oceanbase/design'].version]);
159
+ result.push(['install', '@oceanbase/ui', pkgUpgradeList['@oceanbase/ui'].version]);
153
160
  if (needOBCharts) {
154
161
  result.push(['install', '@oceanbase/charts', pkgUpgradeList['@oceanbase/charts'].version]);
155
162
  }
@@ -241,14 +248,31 @@ async function upgradeDetect(targetDir, needOBCharts, needObUtil) {
241
248
  console.log(`\nNew package installed!\n`);
242
249
 
243
250
  // uninstall dependencies
244
- console.log(`Deprecated package uninstalling...\n`);
245
- const uninstallDependencies = ['@alipay/ob-ui', '@alipay/ob-util', '@alipay/ob-charts'];
246
- console.log(uninstallDependencies.map(n => `* ${n}`).join('\n'));
247
- console.log('\n');
248
- await execa(npmCommand, ['uninstall', ...uninstallDependencies, '--save'], {
249
- stdio: 'inherit',
250
- });
251
- console.log(`\nDeprecated package uninstalled!\n`);
251
+ const deprecatedPackages = ['@alipay/ob-ui', '@alipay/ob-util', '@alipay/ob-charts'];
252
+ const uninstallDependencies = [];
253
+
254
+ if (closetPkgJson) {
255
+ const { packageJson } = closetPkgJson;
256
+ deprecatedPackages.forEach(depName => {
257
+ dependencyProperties.forEach(property => {
258
+ const versionRange = _.get(packageJson, `${property}.${depName}`);
259
+ if (versionRange && !uninstallDependencies.includes(depName)) {
260
+ uninstallDependencies.push(depName);
261
+ }
262
+ });
263
+ });
264
+ }
265
+ // 如果没有找到 package.json,跳过卸载操作(无法确定包是否存在)
266
+
267
+ if (uninstallDependencies.length > 0) {
268
+ console.log(`Deprecated package uninstalling...\n`);
269
+ console.log(uninstallDependencies.map(n => `* ${n}`).join('\n'));
270
+ console.log('\n');
271
+ await execa(npmCommand, ['uninstall', ...uninstallDependencies, '--save'], {
272
+ stdio: 'inherit',
273
+ });
274
+ console.log(`\nDeprecated package uninstalled!\n`);
275
+ }
252
276
  }
253
277
 
254
278
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oceanbase/codemod",
3
- "version": "1.0.0-alpha.4",
3
+ "version": "1.0.0-alpha.6",
4
4
  "description": "Codemod for OceanBase Design upgrade",
5
5
  "keywords": [
6
6
  "oceanbase",
@@ -23,7 +23,7 @@
23
23
  "build": "father build"
24
24
  },
25
25
  "dependencies": {
26
- "@oceanbase/design": "^1.0.0-alpha.4",
26
+ "@oceanbase/design": "^1.0.0-alpha.6",
27
27
  "chalk": "^3.0.0",
28
28
  "command-exists": "^1.2.9",
29
29
  "execa": "^5.1.1",
@@ -47,5 +47,5 @@
47
47
  "enzyme": "^3.11.0",
48
48
  "enzyme-to-json": "^3.6.2"
49
49
  },
50
- "gitHead": "7619cf0d3f5d29dd4c7b67be8265ace300d7e731"
50
+ "gitHead": "7ce9c565c23262a8ffaeab5904095ca65806def9"
51
51
  }
@@ -1,6 +1,5 @@
1
- import React from 'react';
2
-
3
1
  import { token } from '@oceanbase/design';
2
+ import React from 'react';
4
3
 
5
4
  function getComponent() {
6
5
  return <div style={{ fontSize: token.fontSizeLG }} />;
@@ -0,0 +1,23 @@
1
+ import React from 'react';
2
+
3
+ const Component = () => {
4
+ return (
5
+ <div
6
+ style={{
7
+ border: `1px solid #5c6b8a`,
8
+ }}
9
+ />
10
+ );
11
+ };
12
+
13
+ const getComponent = () => {
14
+ return (
15
+ <div
16
+ style={{
17
+ border: `1px solid #5c6b8a`,
18
+ }}
19
+ />
20
+ );
21
+ };
22
+
23
+ export default Component;
@@ -0,0 +1,25 @@
1
+ import { theme, token } from '@oceanbase/design';
2
+ import React from 'react';
3
+
4
+ const Component = () => {
5
+ const { token } = theme.useToken();
6
+ return (
7
+ <div
8
+ style={{
9
+ border: `1px solid ${token.colorTextSecondary}`,
10
+ }}
11
+ />
12
+ );
13
+ };
14
+
15
+ const getComponent = () => {
16
+ return (
17
+ <div
18
+ style={{
19
+ border: `1px solid ${token.colorTextSecondary}`,
20
+ }}
21
+ />
22
+ );
23
+ };
24
+
25
+ export default Component;
@@ -15,6 +15,7 @@ const tests = [
15
15
  'nested-object',
16
16
  'case-insensitive',
17
17
  'antd-style',
18
+ 'template-string',
18
19
  ];
19
20
 
20
21
  describe(testUnit, () => {
@@ -156,7 +156,6 @@ function addTokenImportToBlockStatement(j, root, path) {
156
156
  moduleName: '@oceanbase/design',
157
157
  importedName: 'token',
158
158
  importKind: 'value',
159
- after: 'react',
160
159
  });
161
160
  }
162
161
  }
@@ -271,35 +270,94 @@ function processTemplateLiterals(j, root) {
271
270
  templateList.forEach(path => {
272
271
  const templateLiteral = path.value;
273
272
  const quasis = templateLiteral.quasis;
273
+ const expressions = templateLiteral.expressions || [];
274
274
 
275
275
  // 检查每个模板字符串片段是否包含需要转换的颜色值
276
+ let needsReconstruction = false;
277
+ const newQuasis = [];
278
+ const newExpressions = [];
279
+
276
280
  for (let i = 0; i < quasis.length; i++) {
277
281
  const quasi = quasis[i];
278
282
  let value = quasi.value.raw;
279
- let newValue = value;
280
- let valueChanged = false;
281
283
 
282
284
  // 查找需要转换的颜色值
283
- const colorMatch = newValue.match(
285
+ const colorMatch = value.match(
284
286
  /rgba?\([^)]+\)|#[0-9a-fA-F]{3,8}|rgb\([^)]+\)|hsl\([^)]+\)|hsla?\([^)]+\)/g
285
287
  );
286
288
  if (colorMatch) {
287
289
  hasChanged = true;
288
- valueChanged = true;
290
+ needsReconstruction = true;
289
291
 
292
+ // 收集所有需要替换的匹配项及其 token
293
+ const replacements = [];
290
294
  colorMatch.forEach(match => {
291
295
  const { token } = tokenParse(match);
292
296
  if (token) {
293
- newValue = newValue.replace(match, `\${token.${token}}`);
297
+ const index = value.indexOf(match);
298
+ replacements.push({ index, match, token });
294
299
  }
295
300
  });
301
+
302
+ // 按位置排序,从后往前处理以避免位置偏移
303
+ replacements.sort((a, b) => b.index - a.index);
304
+
305
+ let processedValue = value;
306
+ replacements.forEach(({ index, match, token }) => {
307
+ const before = processedValue.substring(0, index);
308
+ const after = processedValue.substring(index + match.length);
309
+
310
+ // 如果前面有内容,创建一个 quasi
311
+ if (before) {
312
+ newQuasis.push(j.templateElement({ raw: before, cooked: before }, false));
313
+ }
314
+
315
+ // 创建 token 表达式
316
+ newExpressions.push(j.memberExpression(j.identifier('token'), j.identifier(token)));
317
+
318
+ // 剩余部分继续处理
319
+ processedValue = after;
320
+ });
321
+
322
+ // 如果有剩余部分,添加到 quasis
323
+ if (processedValue || newQuasis.length === 0) {
324
+ const isTail = i === quasis.length - 1 && expressions.length === 0;
325
+ newQuasis.push(
326
+ j.templateElement({ raw: processedValue || '', cooked: processedValue || '' }, isTail)
327
+ );
328
+ } else if (newQuasis.length > 0) {
329
+ // 确保最后一个 quasi 标记为 tail(如果这是最后一个 quasi 且没有更多表达式)
330
+ const isTail = i === quasis.length - 1 && expressions.length === 0;
331
+ if (isTail) {
332
+ const lastQuasi = newQuasis[newQuasis.length - 1];
333
+ lastQuasi.tail = true;
334
+ }
335
+ }
336
+ } else {
337
+ // 如果没有颜色值需要转换,保持原样
338
+ newQuasis.push(quasi);
339
+ if (i < expressions.length) {
340
+ newExpressions.push(expressions[i]);
341
+ }
342
+ }
343
+ }
344
+
345
+ // 如果需要重构,替换整个模板字符串
346
+ if (needsReconstruction) {
347
+ // 确保最后一个 quasi 标记为 tail
348
+ if (newQuasis.length > 0) {
349
+ const lastQuasi = newQuasis[newQuasis.length - 1];
350
+ lastQuasi.tail = true;
296
351
  }
297
352
 
298
- // 如果值发生了变化,更新模板字符串片段
299
- if (valueChanged) {
300
- quasi.value.raw = newValue;
301
- quasi.value.cooked = newValue;
353
+ // 合并原有的表达式(如果有)
354
+ const allExpressions = [...newExpressions];
355
+ // 添加原有的表达式(在 quasis 之后)
356
+ for (let i = 0; i < expressions.length; i++) {
357
+ allExpressions.push(expressions[i]);
302
358
  }
359
+
360
+ path.replace(j.templateLiteral(newQuasis, allExpressions));
303
361
  }
304
362
  });
305
363
 
@@ -68,7 +68,8 @@ const TOKEN_MAP = {
68
68
  'rgba(0,0,0,0.85)': 'colorText',
69
69
  'rgba(0,0,0,0.65)': 'colorTextSecondary',
70
70
  'rgba(0,0,0,0.45)': 'colorTextTertiary',
71
- '#5c6b8a': 'colorTextTertiary',
71
+ '#5c6b8a': 'colorTextSecondary',
72
+ '#5C6B8A': 'colorTextSecondary',
72
73
  'rgba(0,0,0,0.25)': 'colorTextQuaternary',
73
74
  'rgba(0,0,0,.85)': 'colorText',
74
75
  'rgba(0,0,0,.65)': 'colorTextSecondary',