template-syncer 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2025 Template Syncer
3
+ Copyright (c) 2025 Icey Wu
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -2,13 +2,29 @@
2
2
 
3
3
  智能模板同步工具,让你的项目与模板仓库保持同步。
4
4
 
5
+ <a href="https://github.com/iceywu/template-syncer">
6
+ <img alt="GitHub Repo stars" src="https://img.shields.io/github/stars/iceywu/template-syncer?logo=github&color=%234d80f0&link=https%3A%2F%2Fgithub.com%2iceywu%2Ftemplate-syncer">
7
+ </a>
8
+ <a href="https://www.npmjs.com/package/template-syncer">
9
+ <img alt="npm" src="https://img.shields.io/npm/v/template-syncer?logo=npm&color=%234d80f0&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Ftemplate-syncer">
10
+ </a>
11
+ <a href="https://www.npmjs.com/package/template-syncer">
12
+ <img alt="npm" src="https://img.shields.io/npm/dw/template-syncer?logo=npm&link=https%3A%2F%2Fwww.npmjs.com%2Fpackage%2Ftemplate-syncer">
13
+ </a>
14
+ <a href="https://www.npmjs.com/package/template-syncer">
15
+ <img src="https://img.shields.io/npm/dt/template-syncer?style=flat-square" alt="downloads">
16
+ </a>
17
+
5
18
  ## ✨ 特性
6
19
 
7
20
  - 🚀 **智能同步** - 自动检测并同步模板更新
21
+ - 🎇 **自动扫描** - 智能扫描当前目录的所有可同步文件
22
+ - ✅ **批量选择** - 支持全选/反选,批量处理文件
23
+ - 🔍 **智能对比** - 先对比差异,再选择性更新
8
24
  - 📦 **智能合并** - 特别针对 `package.json` 的智能合并策略
9
25
  - 🔄 **差异对比** - 使用 Git diff 显示文件变更
10
26
  - 💾 **安全备份** - 操作前自动创建 Git 备份
11
- - 🎯 **交互式操作** - 每个变更都需要用户确认
27
+ - 🎯 **两阶段确认** - 先选择文件,再选择更新项
12
28
 
13
29
  ## 📦 安装
14
30
 
@@ -22,35 +38,66 @@ npm install -g template-syncer
22
38
 
23
39
  ```bash
24
40
  # 交互式同步(会询问模板仓库)
25
- template-sync
41
+ syn
26
42
 
27
43
  # 指定模板仓库
28
- template-sync --repo https://github.com/antfu/vitesse-lite.git
44
+ syn --repo https://github.com/IceyWu/cloud-template.git
45
+
46
+ # 指定模板仓库和分支
47
+ syn --repo https://github.com/IceyWu/cloud-template.git --branch dev
29
48
 
30
49
  # 详细模式
31
- template-sync --verbose
50
+ syn --verbose
51
+
52
+ # 只指定分支(会使用配置文件中的仓库)
53
+ syn --branch main
32
54
  ```
33
55
 
56
+ ### 同步流程
57
+
58
+ 1. **文件扫描** - 自动扫描当前目录下的所有支持文件
59
+ 2. **仓库克隆** - 克隆指定的模板仓库
60
+ 3. **分支选择** - 如果未指定分支,会列出所有分支供选择
61
+ 4. **文件选择** - 默认全选,可以自定义选择要检查的文件
62
+ 5. **差异对比** - 与模板进行对比,找出有变化的文件
63
+ 6. **变更选择** - 选择要更新的文件,可以预览差异
64
+ 7. **批量更新** - 一次性更新所有选中的文件
65
+
66
+ ### 支持的文件类型
67
+
68
+ - **配置文件**: `.json`, `.yml`, `.yaml`, `.xml`
69
+ - **代码文件**: `.js`, `.ts`, `.jsx`, `.tsx`
70
+ - **样式文件**: `.css`, `.scss`, `.less`
71
+ - **文档文件**: `.md`, `.txt`
72
+ - **配置文件**: `.gitignore`, `.npmrc`, `.eslintrc`, `.prettierrc` 等
73
+ - **容器文件**: `Dockerfile`, `.dockerignore`
74
+ - **构建文件**: `Makefile`, 各种配置文件
75
+
34
76
  ### 初始化配置
35
77
 
36
78
  ```bash
37
- template-sync --init
79
+ syn --init
38
80
  ```
39
81
 
40
- ## 🧪 测试
82
+ ### 分支支持
83
+
84
+ Tool 支持从任意分支同步模板:
85
+
86
+ - **指定分支**: 使用 `--branch` 参数指定要同步的分支
87
+ - **交互选择**: 如果不指定分支,工具会列出所有可用分支供你选择
88
+ - **配置保存**: 可以在初始化配置时设置默认分支
41
89
 
42
90
  ```bash
43
- # 创建 vitesse-lite 测试项目
44
- npm test
91
+ # dev 分支同步
92
+ syn --repo https://github.com/owner/repo.git --branch dev
45
93
 
46
- # 然后在测试项目中修改文件,运行同步命令查看效果
47
- ```
94
+ # 从 feature/new-ui 分支同步
95
+ syn --repo https://github.com/owner/repo.git --branch feature/new-ui
48
96
 
49
- ## 📝 文件处理类型
97
+ # 不指定分支,工具会列出所有分支让你选择
98
+ syn --repo https://github.com/owner/repo.git
99
+ ```
50
100
 
51
- - **merge** - 智能合并(主要用于 `package.json`)
52
- - **diff** - 显示差异并让用户选择是否更新
53
- - **overwrite** - 直接覆盖
54
101
 
55
102
  ## 🛡️ 安全性
56
103
 
@@ -60,4 +107,4 @@ npm test
60
107
 
61
108
  ## 📄 许可证
62
109
 
63
- MIT License
110
+ [MIT License](./LICENSE)
package/bin/syn.js ADDED
@@ -0,0 +1,135 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { Command } = require('commander');
4
+ const { TemplateSyncer } = require('../lib/index');
5
+ const pkg = require('../package.json');
6
+
7
+ const program = new Command();
8
+
9
+ program
10
+ .name('syn')
11
+ .description('智能模板同步工具 - 让你的项目与模板仓库保持同步')
12
+ .version(pkg.version)
13
+ .option('-r, --repo <url>', '指定模板仓库 URL')
14
+ .option('-b, --branch <name>', '指定分支名称')
15
+ .option('-v, --verbose', '显示详细输出信息')
16
+ .option('-i, --init', '初始化配置向导')
17
+ .option('--batch', '高级批量操作模式')
18
+ .option('-p, --preview', '预览所有差异(不执行更新)')
19
+ .option('-s, --smart', '智能同步模式(自动推荐)')
20
+ .helpOption('-h, --help', '显示帮助信息');
21
+
22
+ program.addHelpText('after', `
23
+
24
+ 示例:
25
+ $ syn # 交互式同步
26
+ $ syn --init # 初始化配置
27
+ $ syn --batch # 高级批量操作
28
+ $ syn --preview # 预览所有差异
29
+ $ syn --smart # 智能推荐模式
30
+ $ syn --repo https://github.com/IceyWu/cloud-template.git
31
+ $ syn --repo https://github.com/IceyWu/cloud-template.git --branch dev
32
+ $ syn --repo git@github.com:your/template.git --verbose
33
+
34
+ 支持的仓库格式:
35
+ • GitHub: https://github.com/owner/repo.git
36
+ • GitLab: https://gitlab.com/owner/repo.git
37
+ • Bitbucket: https://bitbucket.org/owner/repo.git
38
+ • SSH: git@github.com:owner/repo.git
39
+
40
+ 分支支持:
41
+ • 指定分支: --branch dev
42
+ • 不指定分支时会列出所有分支供选择
43
+ • 支持任意分支名称
44
+
45
+ 功能特性:
46
+ ✅ 智能合并 package.json
47
+ ✅ 支持 Vue/React/Angular 项目
48
+ ✅ 文件差异对比
49
+ ✅ 交互式确认更新
50
+ ✅ Git 备份保护
51
+ ✅ 配置文件保存
52
+ ✅ 多分支支持
53
+
54
+ 更多信息: https://github.com/IceyWu/template-syncer
55
+ `);
56
+
57
+ // 主要执行逻辑
58
+ async function main() {
59
+ try {
60
+ program.parse();
61
+ const options = program.opts(); // 显示启动信息
62
+ if (options.verbose) {
63
+ console.log('🔧 启动配置:');
64
+ if (options.repo) {
65
+ console.log(` 模板仓库: ${options.repo}`);
66
+ }
67
+ if (options.branch) {
68
+ console.log(` 指定分支: ${options.branch}`);
69
+ }
70
+ console.log(` 详细模式: 已启用`);
71
+ console.log('');
72
+ } // 创建同步器实例
73
+ const syncerOptions = {
74
+ ...options,
75
+ templateRepo: options.repo,
76
+ branch: options.branch
77
+ };
78
+ const syncer = new TemplateSyncer(syncerOptions);
79
+
80
+ if (options.init) {
81
+ await syncer.initConfig(); } else if (options.batch) {
82
+ // 高级批量操作模式
83
+ try {
84
+ await syncer.batchProcess();
85
+ } finally {
86
+ // 清理临时文件
87
+ syncer.cleanup();
88
+ }
89
+ } else if (options.preview) {
90
+ // 预览模式
91
+ try {
92
+ await syncer.getTemplateRepo();
93
+ await syncer.cloneTemplate();
94
+ const templateFiles = await syncer.scanTemplateFiles();
95
+ const currentFiles = await syncer.scanCurrentFiles();
96
+ const changedFiles = await syncer.compareFiles(templateFiles, currentFiles);
97
+ await syncer.previewAllDifferences(changedFiles);
98
+ } finally {
99
+ // 清理临时文件
100
+ syncer.cleanup();
101
+ }
102
+ } else if (options.smart) {
103
+ // 智能同步模式
104
+ try {
105
+ await syncer.intelligentSync();
106
+ } finally {
107
+ // 清理临时文件
108
+ syncer.cleanup();
109
+ }
110
+ } else {
111
+ // 默认交互式同步
112
+ await syncer.sync();
113
+ }
114
+ } catch (error) {
115
+ console.error('❌ 程序执行失败:', error.message);
116
+ if (program.opts().verbose) {
117
+ console.error(error.stack);
118
+ }
119
+ process.exit(1);
120
+ }
121
+ }
122
+
123
+ // 处理未捕获的异常
124
+ process.on('uncaughtException', (error) => {
125
+ console.error('💥 未捕获的异常:', error.message);
126
+ process.exit(1);
127
+ });
128
+
129
+ process.on('unhandledRejection', (reason, promise) => {
130
+ console.error('💥 未处理的 Promise 拒绝:', reason);
131
+ process.exit(1);
132
+ });
133
+
134
+ // 启动程序
135
+ main();
package/lib/index.d.ts ADDED
@@ -0,0 +1,117 @@
1
+ import { FileConfig, TemplateSyncerOptions, BatchResults } from './types';
2
+ /**
3
+ * 智能模板同步工具
4
+ * 用于将项目与模板仓库保持同步
5
+ */
6
+ export declare class TemplateSyncer {
7
+ private tempDir;
8
+ private templateRepo;
9
+ private branch;
10
+ private configFile;
11
+ private changes;
12
+ private verbose;
13
+ private ignorePatterns;
14
+ private fileTypeConfig;
15
+ private specialFiles;
16
+ constructor(options?: TemplateSyncerOptions);
17
+ /**
18
+ * 创建 Git 备份
19
+ */
20
+ createBackup(): void;
21
+ /**
22
+ * 克隆模板仓库
23
+ */
24
+ cloneTemplate(): Promise<void>; /**
25
+ * 获取或设置模板仓库
26
+ */
27
+ getTemplateRepo(): Promise<string>;
28
+ /**
29
+ * 测试仓库连接
30
+ */
31
+ testRepository(repo: string): Promise<boolean>;
32
+ /**
33
+ * 扫描模板文件
34
+ */
35
+ scanTemplateFiles(): Promise<FileConfig[]>;
36
+ /**
37
+ * 扫描当前项目文件
38
+ */
39
+ scanCurrentFiles(): Promise<FileConfig[]>;
40
+ /**
41
+ * 分析文件类型和特征
42
+ */
43
+ private analyzeFile;
44
+ /**
45
+ * 对比文件并生成变更列表
46
+ */
47
+ compareFiles(templateFiles: FileConfig[], currentFiles: FileConfig[]): Promise<FileConfig[]>;
48
+ /**
49
+ * 交互式选择要应用的变更
50
+ */
51
+ selectChangesToApply(changes: FileConfig[]): Promise<FileConfig[]>;
52
+ /**
53
+ * 按分类选择文件
54
+ */
55
+ private selectByCategory;
56
+ /**
57
+ * 逐一选择文件
58
+ */
59
+ private selectIndividually;
60
+ /**
61
+ * 批量更新文件
62
+ */
63
+ batchUpdateFiles(files: FileConfig[]): Promise<BatchResults>;
64
+ /**
65
+ * 更新单个文件
66
+ */
67
+ private updateSingleFile;
68
+ /**
69
+ * 智能合并 package.json
70
+ */
71
+ private mergePackageJson;
72
+ /**
73
+ * 显示操作摘要
74
+ */
75
+ showSummary(results: BatchResults): void;
76
+ /**
77
+ * 清理临时文件
78
+ */
79
+ cleanup(): void;
80
+ /**
81
+ * 加载配置文件
82
+ */
83
+ private loadConfig;
84
+ /**
85
+ * 保存配置文件
86
+ */
87
+ private saveConfig;
88
+ /**
89
+ * 主同步流程
90
+ */
91
+ sync(options?: TemplateSyncerOptions): Promise<void>;
92
+ /**
93
+ * 初始化配置向导
94
+ */
95
+ initConfig(): Promise<void>;
96
+ /**
97
+ * 批量处理模式
98
+ */
99
+ batchProcess(): Promise<void>;
100
+ /**
101
+ * 生成智能推荐
102
+ */
103
+ private generateRecommendations;
104
+ /**
105
+ * 预览所有差异
106
+ */
107
+ previewAllDifferences(files: FileConfig[]): Promise<void>;
108
+ /**
109
+ * 智能同步模式
110
+ */
111
+ intelligentSync(): Promise<void>;
112
+ /**
113
+ * 选择分支
114
+ */
115
+ private selectBranch;
116
+ }
117
+ //# sour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