一个强大的JavaScript工具,用于在指定目录下查找符合条件的子目录,并递归替换其中的特定字符串。
- 🔍 智能目录查找: 支持正则表达式和字符串匹配来查找符合条件的目录
- 🔄 递归搜索: 自动递归搜索所有子目录
- 📝 多文件类型支持: 支持多种文件扩展名的字符串替换
- 🚫 排除目录: 可配置排除不需要处理的目录(如node_modules、.git等)
- 👀 预览模式: 支持dry-run模式,预览替换结果而不实际修改文件
- 📊 详细报告: 提供详细的操作报告和统计信息
- ⚡ 高性能: 异步处理,支持大项目
- Node.js 8.0 或更高版本
- 无需额外依赖包
node directory-string-replacer.js [选项]| 选项 | 长选项 | 描述 | 默认值 |
|---|---|---|---|
-r |
--root |
指定根目录 | 当前目录 |
-p |
--pattern |
目录名匹配模式(正则表达式或字符串) | .* |
-s |
--search |
要搜索的字符串 | 必需 |
-t |
--replace |
替换字符串 | 必需 |
-e |
--extensions |
文件扩展名列表,用逗号分隔 | .js,.ts,.jsx,.tsx,.json,.html,.css,.scss,.md,.txt |
-x |
--exclude |
排除的目录列表,用逗号分隔 | node_modules,.git,.vscode,dist,build |
-d |
--dry-run |
预览模式,不实际修改文件 | false |
-v |
--verbose |
详细输出 | false |
-h |
--help |
显示帮助信息 | - |
在src目录下查找名称包含"component"的目录,将"oldName"替换为"newName":
node directory-string-replacer.js -r src -p component -s oldName -t newName查找以"test-"开头的目录,将"describe"替换为"describe.skip":
node directory-string-replacer.js -p "^test-.*" -s "describe" -t "describe.skip"使用--dry-run选项预览会进行哪些替换,而不实际修改文件:
node directory-string-replacer.js -p "utils" -s "console.log" -t "logger.info" --dry-run只处理JavaScript和JSX文件:
node directory-string-replacer.js -p "components" -s "React" -t "React" -e ".js,.jsx"排除temp和cache目录:
node directory-string-replacer.js -p "src" -s "TODO" -t "FIXME" -x "node_modules,.git,temp,cache"使用--verbose选项查看详细的操作过程:
node directory-string-replacer.js -p ".*" -s "oldApi" -t "newApi" --verbose目录匹配模式支持完整的正则表达式语法:
# 匹配以"page-"开头的目录
node directory-string-replacer.js -p "^page-.*" -s "oldText" -t "newText"
# 匹配包含"test"或"spec"的目录
node directory-string-replacer.js -p ".*(test|spec).*" -s "oldText" -t "newText"
# 匹配数字结尾的目录
node directory-string-replacer.js -p ".*[0-9]$" -s "oldText" -t "newText"支持替换包含特殊字符的字符串:
# 替换包含正则表达式特殊字符的字符串
node directory-string-replacer.js -s "function\s+\w+\(" -t "const \1 = ("
# 替换多行文本
node directory-string-replacer.js -s "old\nmultiline\ntext" -t "new\nmultiline\ntext"开始执行目录字符串替换...
根目录: /path/to/project
目录匹配模式: component
搜索字符串: "oldName"
替换字符串: "newName"
文件扩展名: .js, .ts, .jsx, .tsx, .json, .html, .css, .scss, .md, .txt
排除目录: node_modules, .git, .vscode, dist, build
预览模式: 否
---
找到 3 个符合条件的目录:
- /path/to/project/src/components/Button
- /path/to/project/src/components/Input
- /path/to/project/src/components/Modal
---
处理目录: /path/to/project/src/components/Button
处理文件: /path/to/project/src/components/Button/index.js (2 次替换)
处理目录: /path/to/project/src/components/Input
处理文件: /path/to/project/src/components/Input/index.js (1 次替换)
处理目录: /path/to/project/src/components/Modal
处理文件: /path/to/project/src/components/Modal/index.js (3 次替换)
---
操作完成!
找到目录数: 3
处理文件数: 3
总替换次数: 6
开始执行目录字符串替换...
根目录: /path/to/project
目录匹配模式: component
搜索字符串: "oldName"
替换字符串: "newName"
文件扩展名: .js, .ts, .jsx, .tsx, .json, .html, .css, .scss, .md, .txt
排除目录: node_modules, .git, .vscode, dist, build
预览模式: 是
---
找到 3 个符合条件的目录:
- /path/to/project/src/components/Button
- /path/to/project/src/components/Input
- /path/to/project/src/components/Modal
---
处理目录: /path/to/project/src/components/Button
[预览] 处理文件: /path/to/project/src/components/Button/index.js (2 次替换)
处理目录: /path/to/project/src/components/Input
[预览] 处理文件: /path/to/project/src/components/Input/index.js (1 次替换)
处理目录: /path/to/project/src/components/Modal
[预览] 处理文件: /path/to/project/src/components/Modal/index.js (3 次替换)
---
操作完成!
找到目录数: 3
处理文件数: 3
总替换次数: 6
- 备份重要文件: 在执行替换操作前,建议备份重要的项目文件
- 使用预览模式: 首次使用时建议使用
--dry-run选项预览结果 - 测试环境: 在测试环境中先验证替换结果
- 版本控制: 确保项目在版本控制系统中,以便回滚更改
- 权限错误: 确保对目标目录有读写权限
- 文件被占用: 确保要修改的文件没有被其他程序占用
- 路径问题: 使用绝对路径或确保相对路径正确
- 使用
--verbose选项查看详细输出 - 使用
--dry-run选项预览操作 - 先在小范围目录中测试
MIT License
欢迎提交Issue和Pull Request来改进这个工具。