UNPKG

template-syncer

Version:

智能模板同步工具 - 让你的项目与模板仓库保持同步,支持智能合并、差异对比和交互式更新

166 lines (165 loc) 7.44 kB
"use strict"; 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.Merger = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); const platform_1 = require("./platform"); class Merger { constructor(customStrategies = {}) { this.strategies = { 'package.json': 'smart', 'tsconfig.json': 'smart', 'jsconfig.json': 'smart', ...customStrategies }; } getStrategy(filePath) { const fileName = path.basename(filePath); return this.strategies[fileName] ?? this.strategies[filePath] ?? 'overwrite'; } async merge(templatePath, currentPath, targetPath) { const strategy = this.getStrategy(targetPath); const fileName = path.basename(targetPath); if (strategy === 'skip') return { success: false, message: '已跳过' }; if (strategy === 'smart') return this.smartMerge(templatePath, currentPath, targetPath, fileName); // overwrite platform_1.platform.copyFile(templatePath, targetPath); return { success: true, message: '已覆盖' }; } async smartMerge(templatePath, currentPath, targetPath, fileName) { const exists = await fs.promises.access(currentPath).then(() => true).catch(() => false); if (!exists) { platform_1.platform.copyFile(templatePath, targetPath); return { success: true, message: '已创建' }; } if (fileName === 'package.json') return this.mergePackageJson(templatePath, currentPath, targetPath); if (fileName.endsWith('.json')) return this.mergeJson(templatePath, currentPath, targetPath); platform_1.platform.copyFile(templatePath, targetPath); return { success: true, message: '已覆盖' }; } async mergePackageJson(templatePath, currentPath, targetPath) { try { const [template, current] = await Promise.all([ platform_1.platform.readJsonAsync(templatePath), platform_1.platform.readJsonAsync(currentPath) ]); if (!template || !current) { platform_1.platform.copyFile(templatePath, targetPath); return { success: true, message: '已覆盖 (无法解析)' }; } const merged = { name: current.name ?? template.name, version: current.version ?? template.version, description: current.description ?? template.description, author: current.author ?? template.author, license: current.license ?? template.license, repository: current.repository ?? template.repository, homepage: current.homepage ?? template.homepage, bugs: current.bugs ?? template.bugs, keywords: current.keywords ?? template.keywords, type: template.type ?? current.type, main: template.main ?? current.main, module: template.module ?? current.module, types: template.types ?? current.types, exports: template.exports ?? current.exports, bin: template.bin ?? current.bin, files: template.files ?? current.files, engines: template.engines ?? current.engines, scripts: { ...(current.scripts ?? {}), ...(template.scripts ?? {}) }, dependencies: { ...(current.dependencies ?? {}), ...(template.dependencies ?? {}) }, devDependencies: { ...(current.devDependencies ?? {}), ...(template.devDependencies ?? {}) }, peerDependencies: { ...(current.peerDependencies ?? {}), ...(template.peerDependencies ?? {}) }, optionalDependencies: { ...(current.optionalDependencies ?? {}), ...(template.optionalDependencies ?? {}) } }; for (const key of Object.keys(template)) { if (!(key in merged)) merged[key] = template[key]; } // 清理空对象 for (const key of Object.keys(merged)) { const v = merged[key]; if (v && typeof v === 'object' && !Array.isArray(v) && Object.keys(v).length === 0) { delete merged[key]; } } platform_1.platform.writeJson(targetPath, merged); return { success: true, message: '已智能合并' }; } catch { platform_1.platform.copyFile(templatePath, targetPath); return { success: true, message: '已覆盖 (合并失败)' }; } } async mergeJson(templatePath, currentPath, targetPath) { try { const [template, current] = await Promise.all([ platform_1.platform.readJsonAsync(templatePath), platform_1.platform.readJsonAsync(currentPath) ]); if (!template || !current) { platform_1.platform.copyFile(templatePath, targetPath); return { success: true, message: '已覆盖' }; } platform_1.platform.writeJson(targetPath, this.deepMerge(current, template)); return { success: true, message: '已合并' }; } catch { platform_1.platform.copyFile(templatePath, targetPath); return { success: true, message: '已覆盖' }; } } deepMerge(target, source) { const result = { ...target }; for (const key of Object.keys(source)) { const s = source[key], t = target[key]; if (s && typeof s === 'object' && !Array.isArray(s) && t && typeof t === 'object' && !Array.isArray(t)) { result[key] = this.deepMerge(t, s); } else { result[key] = s; } } return result; } setStrategy(pattern, strategy) { this.strategies[pattern] = strategy; } } exports.Merger = Merger;