sync-upstream
Version:
A tool for synchronizing code with upstream repositories with incremental updates and parallel processing.
50 lines (49 loc) • 1.87 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizePath = normalizePath;
exports.loadIgnorePatterns = loadIgnorePatterns;
exports.shouldIgnore = shouldIgnore;
// src/ignore.ts
const node_path_1 = __importDefault(require("node:path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const micromatch_1 = __importDefault(require("micromatch"));
/**
* 规范化路径分隔符为正斜杠
*/
function normalizePath(filePath) {
return filePath.split(node_path_1.default.sep).join('/');
}
/**
* 加载忽略模式
*/
async function loadIgnorePatterns(baseDir) {
// 默认忽略模式,确保node_modules被排除
const defaultPatterns = ['**/node_modules/**', '**/.git/**', '**/dist/**', '**/build/**'];
const ignoreFile = node_path_1.default.join(baseDir, '.syncignore');
if (await fs_extra_1.default.pathExists(ignoreFile)) {
try {
const content = await fs_extra_1.default.readFile(ignoreFile, 'utf8');
const filePatterns = content
.split('\n')
.map(line => line.trim())
.filter(line => line && !line.startsWith('#'));
// 合并默认模式和文件中的模式
return [...defaultPatterns, ...filePatterns];
}
catch (error) {
console.error('读取 .syncignore 文件失败:', error);
}
}
return defaultPatterns;
}
/**
* 检查路径是否应该被忽略
*/
function shouldIgnore(filePath, ignorePatterns) {
// 规范化路径分隔符,确保在Windows上也能正确匹配
const normalizedPath = normalizePath(filePath);
return micromatch_1.default.isMatch(normalizedPath, ignorePatterns);
}