Skip to content

引入

ts
import {
  getComponentPascalReg,
  getThreeStageRenameConfig,
  getMetaConfigPascalReplaceRules,
  getComponentContentReplaceRules,
  batchReplaceFileContent,
  batchRenameDirEntries
} from 't-comm';

// 不支持 tree-shaking 的项目
import {
  getComponentPascalReg,
  getThreeStageRenameConfig,
  getMetaConfigPascalReplaceRules,
  getComponentContentReplaceRules,
  batchReplaceFileContent,
  batchRenameDirEntries
} from 't-comm/lib/turn-plus/index';

// 只支持 ESM 的项目
import {
  getComponentPascalReg,
  getThreeStageRenameConfig,
  getMetaConfigPascalReplaceRules,
  getComponentContentReplaceRules,
  batchReplaceFileContent,
  batchRenameDirEntries
} from 't-comm/es/turn-plus/index';

getComponentPascalReg(value)

描述:生成 PascalCase 匹配正则(前后必须是非单词字符)

参数

参数名类型描述
valuestringkebab-case 名称

示例

ts
const reg = getComponentPascalReg('press-icon');
'use <PressIcon/>'.match(reg); // ['PressIcon']

getThreeStageRenameConfig(rawList, [extraList])

描述:生成三阶段重命名配置(plus -> temp, key -> plus, temp -> key)

参数

参数名类型默认值描述
rawListArray<string>组件名列表,如 ['dialog', 'icon']
[extraList]Array<string>[]额外需要重命名的组件名列表

示例

ts
const { renameConfig, renameConfig2, renameConfig3 } = getThreeStageRenameConfig(['dialog', 'icon']);
// renameConfig:  { 'press-dialog-plus': '<random>', 'press-icon-plus': '<random>' }
// renameConfig2: { 'press-dialog': 'press-dialog-plus', 'press-icon': 'press-icon-plus' }
// renameConfig3: { '<random>': 'press-dialog', '<random>': 'press-icon' }

getMetaConfigPascalReplaceRules(rawList, dirList)

描述:生成 meta config(如 component-config.json)的 PascalCase 替换规则

参数

参数名类型描述
rawListArray<string>组件名列表
dirListArray<string>目标文件 glob 列表

示例

ts
const rules = getMetaConfigPascalReplaceRules(
  ['dialog', 'icon'],
  ['src/component-config.json'],
);
// rules 为三阶段替换规则数组,供 batchReplaceFileContent 使用

getComponentContentReplaceRules(rawList, dirList, [options])

描述:生成文件内容的三阶段替换规则(kebab-case + PascalCase + 类名)

参数

参数名类型默认值描述
rawListArray<string>组件名列表
dirListArray<string>目标文件 glob 列表
[options]Object{}可选配置
[options.needClassReplaceList]Array<string>[]需要额外替换类名的组件列表

示例

ts
const rules = getComponentContentReplaceRules(
  ['dialog', 'icon'],
  ['src/**\/*.{ts,vue,less}'],
  { needClassReplaceList: ['icon'] },
);
batchReplaceFileContent(rules);

batchReplaceFileContent(replaceList)

描述:批量替换文件内容(性能优化版) 将同 dirList 的规则合并,每个文件只读写一次

参数

参数名类型描述
replaceListArray<{list: Array, dirList: (string|Array<string>)}>替换规则列表

示例

ts
batchReplaceFileContent([
  {
    list: [
      ['<PressIconPlus', '<PressIcon'],
      [/press-icon-plus/g, 'press-icon'],
    ],
    dirList: ['src/**\/*.vue'],
  },
]);
// 同 dirList 的规则会被合并,避免重复读写

batchRenameDirEntries(dirPath, renameConfig, [recursive])

描述:批量重命名文件和文件夹(同步版本)

参数

参数名类型默认值描述
dirPathstring要处理的目录路径
renameConfigObject重命名配置对象
[recursive]booleantrue是否递归处理子目录

示例

ts
batchRenameDirEntries('src/components', {
  'press-icon-plus': 'press-icon',
  'press-dialog-plus': 'press-dialog',
});
// 递归将 src/components 下名为 press-icon-plus、press-dialog-plus 的文件/文件夹重命名

// 只重命名当前层、不递归
batchRenameDirEntries('src/components', { 'a': 'b' }, false);