template-syncer
Version:
智能模板同步工具 - 让你的项目与模板仓库保持同步,支持智能合并、差异对比和交互式更新
181 lines (180 loc) • 5.32 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.platform = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const child_process_1 = require("child_process");
/**
* 跨平台工具类
*/
exports.platform = {
/** 是否为 Windows */
isWindows: process.platform === 'win32',
/**
* 安全删除目录
*/
removeDir(dir) {
if (!fs.existsSync(dir))
return;
try {
fs.rmSync(dir, { recursive: true, force: true });
}
catch {
// 回退到系统命令
const cmd = this.isWindows
? `rmdir /s /q "${dir}"`
: `rm -rf "${dir}"`;
try {
(0, child_process_1.execSync)(cmd, { stdio: 'ignore' });
}
catch {
// 忽略错误
}
}
},
/**
* 安全删除文件
*/
removeFile(filePath) {
if (!fs.existsSync(filePath))
return;
try {
fs.unlinkSync(filePath);
}
catch {
// 回退到系统命令
const cmd = this.isWindows
? `del /f /q "${filePath}"`
: `rm -f "${filePath}"`;
try {
(0, child_process_1.execSync)(cmd, { stdio: 'ignore' });
}
catch {
// 忽略错误
}
}
},
/**
* 确保目录存在
*/
ensureDir(dir) {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
},
/**
* 复制文件
*/
copyFile(src, dest) {
this.ensureDir(path.dirname(dest));
fs.copyFileSync(src, dest);
},
/**
* 读取文件内容
*/
readFile(filePath) {
return fs.readFileSync(filePath, 'utf8');
},
/**
* 写入文件内容
*/
writeFile(filePath, content) {
this.ensureDir(path.dirname(filePath));
fs.writeFileSync(filePath, content, 'utf8');
},
/**
* 读取 JSON 文件 (同步)
*/
readJson(filePath) {
try {
return JSON.parse(this.readFile(filePath));
}
catch {
return null;
}
},
/**
* 读取 JSON 文件 (异步)
*/
async readJsonAsync(filePath) {
try {
const content = await fs.promises.readFile(filePath, 'utf8');
return JSON.parse(content);
}
catch {
return null;
}
},
/**
* 写入 JSON 文件
*/
writeJson(filePath, data) {
this.writeFile(filePath, JSON.stringify(data, null, 2));
},
/**
* 检测文件是否为二进制
*/
isBinary(filePath) {
// 常见二进制扩展名
const binaryExts = new Set([
'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.ico', '.webp', '.avif', '.svg',
'.mp3', '.mp4', '.wav', '.ogg', '.webm', '.avi', '.mov', '.flac',
'.zip', '.tar', '.gz', '.rar', '.7z', '.bz2', '.xz',
'.pdf', '.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx',
'.exe', '.dll', '.so', '.dylib', '.bin', '.dat',
'.woff', '.woff2', '.ttf', '.otf', '.eot',
'.sqlite', '.db', '.lockb'
]);
const ext = path.extname(filePath).toLowerCase();
if (binaryExts.has(ext))
return true;
// 读取文件头检测 null 字节
try {
const fd = fs.openSync(filePath, 'r');
const buffer = Buffer.alloc(512);
const bytesRead = fs.readSync(fd, buffer, 0, 512, 0);
fs.closeSync(fd);
for (let i = 0; i < bytesRead; i++) {
if (buffer[i] === 0)
return true;
}
}
catch {
// 默认为文本
}
return false;
}
};