名称: 行尾标准化工具 描述: 通过CRLF/LF转换和git配置实现跨平台文件处理的行尾标准化。 允许工具: 读取, 写入, 编辑, Bash, Glob, Grep
行尾标准化工具
为跨平台兼容性标准化行尾。
功能
- 检测行尾样式
- 在CRLF和LF之间转换
- 配置git行尾设置
- 处理混合行尾
- 设置.gitattributes
生成的模式
export type LineEnding = 'lf' | 'crlf' | 'mixed';
export function detectLineEnding(content: string): LineEnding {
const crlf = (content.match(/\r
/g) || []).length;
const lf = (content.match(/(?<!\r)
/g) || []).length;
if (crlf > 0 && lf > 0) return 'mixed';
if (crlf > 0) return 'crlf';
return 'lf';
}
export function normalizeLineEndings(content: string, target: 'lf' | 'crlf' = 'lf'): string {
const normalized = content.replace(/\r
/g, '
').replace(/\r/g, '
');
return target === 'crlf' ? normalized.replace(/
/g, '\r
') : normalized;
}
// .gitattributes 内容
export const gitattributes = `
* text=auto eol=lf
*.bat text eol=crlf
*.cmd text eol=crlf
*.ps1 text eol=crlf
*.sh text eol=lf
`;
目标流程
- 跨平台命令行兼容性
- 配置管理系统
- Shell脚本开发