文件批量重命名和内容替换工具 - 支持正则表达式和普通文本替换。
npm install -g @liyuanlin/namechangergit clone <仓库地址>
cd namechanger
npm install
npm run build
npm link# 打包(生成 tgz 文件,用于本地测试)
npm run pack
# 完整打包(清理 + 构建 + 打包)
npm run pack:local# 快速发布补丁版本 (1.0.0 -> 1.0.1)
npm run publish:npm
# 快速发布次要版本 (1.0.0 -> 1.1.0)
npm run publish:minor
# 快速发布主要版本 (1.0.0 -> 2.0.0)
npm run publish:major
# 完整发布流程 (清理 + 构建 + 版本递增 + 发布)
npm run release
# 发布公开包 (Scoped 包需要)
npm run publish:public
# 使用当前版本号发布 (不推荐,仅当版本号已手动更新时)
npm run publish:current# Linux/Mac
npm run clean
# Windows
npm run clean:winnamechanger -c config.jsonnamechanger --helpnamechanger --version# 使用命令行参数
namechanger -c config.json --rename-only
# 或在配置文件中设置
{
"renameOnly": true,
...
}# 使用命令行参数
namechanger -c config.json --remove-empty-dirs
# 或在配置文件中设置
{
"removeEmptyDirs": true,
...
}创建一个 JSON 配置文件来定义替换规则:
{
"name": "my-project",
"version": "1.0.0",
"root": "C:/projects/my-project",
"files": [".sln", ".ps1", ".yml", ".md", ".css", ".scss", ".js", ".ts", ".html", ".cshtml"],
"excludes": ["node_modules", ".git", ".vscode", "dist", ".idea", ".vs"],
"renameOnly": false,
"removeEmptyDirs": false,
"binaryExtensions": [".exe", ".dll", ".png", ".jpg", ".pdf"],
"replacements": [
{
"regex": false,
"old": "src/",
"new": "dist/"
},
{
"regex": true,
"old": "OldProjectName",
"new": "NewProjectName"
}
]
}| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
name |
string | 否 | 项目名称 |
version |
string | 否 | 版本号 |
root |
string | 是 | 要处理的根目录路径 |
files |
string[] | 是 | 要处理的文件扩展名列表(例如:[".ts", ".js"]) |
excludes |
string[] | 否 | 要排除的目录名列表 |
renameOnly |
boolean | 否 | 仅重命名文件,不替换文件内容(默认:false) |
removeEmptyDirs |
boolean | 否 | 完成后移除空目录(默认:false) |
binaryExtensions |
string[] | 否 | 二进制文件扩展名列表,跳过内容替换(默认:[]) |
replacements |
array | 是 | 替换规则数组 |
每个替换规则包含以下字段:
| 字段 | 类型 | 说明 |
|---|---|---|
regex |
boolean | 是否使用正则表达式匹配 |
old |
string | 要查找的旧文本或正则表达式 |
new |
string | 替换后的新文本 |
- 文件重命名: 根据替换规则批量重命名文件
- 内容替换: 在文件内容中查找并替换文本
- 二进制文件保护: 自动跳过二进制文件的内容替换
- 正则支持: 支持正则表达式进行复杂匹配
- 路径处理: 正确处理 Windows 和 Unix 路径分隔符
- 安全排除: 自动排除 node_modules、.git 等目录
- 移除空目录: 处理完成后自动清理空目录
将项目中的所有 foo 替换为 bar:
{
"root": "./my-project",
"files": [".txt", ".md"],
"excludes": ["node_modules"],
"replacements": [
{
"regex": false,
"old": "foo",
"new": "bar"
}
]
}将所有 v1.x.x 格式的版本号替换为 v2.x.x:
{
"root": "./my-project",
"files": [".json", ".md"],
"replacements": [
{
"regex": true,
"old": "v1\\.(\\d+)\\.(\\d+)",
"new": "v2.$1.$2"
}
]
}将源代码目录从 src/ 改为 lib/:
{
"root": "./my-project",
"files": [".ts", ".js", ".json"],
"excludes": ["node_modules", "dist"],
"replacements": [
{
"regex": false,
"old": "src/",
"new": "lib/"
},
{
"regex": false,
"old": "src\\",
"new": "lib\\"
}
]
}- 备份数据: 操作前请备份重要文件,替换操作不可逆
- 测试配置: 建议先在小范围测试配置文件
- 路径格式: Windows 路径可以使用正斜杠
/或双反斜杠\\ - 正则转义: 使用正则时,特殊字符需要正确转义
ISC