template-syncer 1.0.1 → 1.0.2

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/README.md CHANGED
@@ -5,10 +5,13 @@
5
5
  ## ✨ 特性
6
6
 
7
7
  - 🚀 **智能同步** - 自动检测并同步模板更新
8
- - 📦 **智能合并** - 特别针对 `package.json` 的智能合并策略
8
+ - **自动扫描** - 智能扫描当前目录的所有可同步文件
9
+ - ✅ **批量选择** - 支持全选/反选,批量处理文件
10
+ - 🔍 **智能对比** - 先对比差异,再选择性更新
11
+ - �📦 **智能合并** - 特别针对 `package.json` 的智能合并策略
9
12
  - 🔄 **差异对比** - 使用 Git diff 显示文件变更
10
13
  - 💾 **安全备份** - 操作前自动创建 Git 备份
11
- - 🎯 **交互式操作** - 每个变更都需要用户确认
14
+ - 🎯 **两阶段确认** - 先选择文件,再选择更新项
12
15
 
13
16
  ## 📦 安装
14
17
 
@@ -22,19 +25,37 @@ npm install -g template-syncer
22
25
 
23
26
  ```bash
24
27
  # 交互式同步(会询问模板仓库)
25
- template-sync
28
+ syn
26
29
 
27
30
  # 指定模板仓库
28
- template-sync --repo https://github.com/antfu/vitesse-lite.git
31
+ syn --repo https://github.com/antfu/vitesse-lite.git
29
32
 
30
33
  # 详细模式
31
- template-sync --verbose
34
+ syn --verbose
32
35
  ```
33
36
 
37
+ ### 同步流程
38
+
39
+ 1. **文件扫描** - 自动扫描当前目录下的所有支持文件
40
+ 2. **文件选择** - 默认全选,可以自定义选择要检查的文件
41
+ 3. **差异对比** - 与模板进行对比,找出有变化的文件
42
+ 4. **变更选择** - 选择要更新的文件,可以预览差异
43
+ 5. **批量更新** - 一次性更新所有选中的文件
44
+
45
+ ### 支持的文件类型
46
+
47
+ - **配置文件**: `.json`, `.yml`, `.yaml`, `.xml`
48
+ - **代码文件**: `.js`, `.ts`, `.jsx`, `.tsx`
49
+ - **样式文件**: `.css`, `.scss`, `.less`
50
+ - **文档文件**: `.md`, `.txt`
51
+ - **配置文件**: `.gitignore`, `.npmrc`, `.eslintrc`, `.prettierrc` 等
52
+ - **容器文件**: `Dockerfile`, `.dockerignore`
53
+ - **构建文件**: `Makefile`, 各种配置文件
54
+
34
55
  ### 初始化配置
35
56
 
36
57
  ```bash
37
- template-sync --init
58
+ syn --init
38
59
  ```
39
60
 
40
61
  ## 🧪 测试
@@ -46,8 +67,6 @@ npm test
46
67
  # 然后在测试项目中修改文件,运行同步命令查看效果
47
68
  ```
48
69
 
49
- ## 📝 文件处理类型
50
-
51
70
  - **merge** - 智能合并(主要用于 `package.json`)
52
71
  - **diff** - 显示差异并让用户选择是否更新
53
72
  - **overwrite** - 直接覆盖
package/bin/syn.js ADDED
@@ -0,0 +1,125 @@
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('-v, --verbose', '显示详细输出信息')
15
+ .option('-i, --init', '初始化配置向导')
16
+ .option('-b, --batch', '高级批量操作模式')
17
+ .option('-p, --preview', '预览所有差异(不执行更新)')
18
+ .option('-s, --smart', '智能同步模式(自动推荐)')
19
+ .helpOption('-h, --help', '显示帮助信息');
20
+
21
+ program.addHelpText('after', `
22
+
23
+ 示例:
24
+ $ syn # 交互式同步
25
+ $ syn --init # 初始化配置
26
+ $ syn --batch # 高级批量操作
27
+ $ syn --preview # 预览所有差异
28
+ $ syn --smart # 智能推荐模式
29
+ $ syn --repo https://github.com/antfu/vitesse-lite.git
30
+ $ syn --repo git@github.com:your/template.git --verbose
31
+
32
+ 支持的仓库格式:
33
+ • GitHub: https://github.com/owner/repo.git
34
+ • GitLab: https://gitlab.com/owner/repo.git
35
+ • Bitbucket: https://bitbucket.org/owner/repo.git
36
+ • SSH: git@github.com:owner/repo.git
37
+
38
+ 功能特性:
39
+ ✅ 智能合并 package.json
40
+ ✅ 支持 Vue/React/Angular 项目
41
+ ✅ 文件差异对比
42
+ ✅ 交互式确认更新
43
+ ✅ Git 备份保护
44
+ ✅ 配置文件保存
45
+
46
+ 更多信息: https://github.com/IceyWu/template-syncer
47
+ `);
48
+
49
+ // 主要执行逻辑
50
+ async function main() {
51
+ try {
52
+ program.parse();
53
+ const options = program.opts();
54
+
55
+ // 显示启动信息
56
+ if (options.verbose) {
57
+ console.log('🔧 启动配置:');
58
+ if (options.repo) {
59
+ console.log(` 模板仓库: ${options.repo}`);
60
+ }
61
+ console.log(` 详细模式: 已启用`);
62
+ console.log('');
63
+ } // 创建同步器实例
64
+ const syncerOptions = {
65
+ ...options,
66
+ templateRepo: options.repo
67
+ };
68
+ const syncer = new TemplateSyncer(syncerOptions);
69
+
70
+ if (options.init) {
71
+ await syncer.initConfig(); } else if (options.batch) {
72
+ // 高级批量操作模式
73
+ try {
74
+ await syncer.batchProcess();
75
+ } finally {
76
+ // 清理临时文件
77
+ syncer.cleanup();
78
+ }
79
+ } else if (options.preview) {
80
+ // 预览模式
81
+ try {
82
+ await syncer.getTemplateRepo();
83
+ await syncer.cloneTemplate();
84
+ const templateFiles = await syncer.scanTemplateFiles();
85
+ const currentFiles = await syncer.scanCurrentFiles();
86
+ const changedFiles = await syncer.compareFiles(templateFiles, currentFiles);
87
+ await syncer.previewAllDifferences(changedFiles);
88
+ } finally {
89
+ // 清理临时文件
90
+ syncer.cleanup();
91
+ }
92
+ } else if (options.smart) {
93
+ // 智能同步模式
94
+ try {
95
+ await syncer.intelligentSync();
96
+ } finally {
97
+ // 清理临时文件
98
+ syncer.cleanup();
99
+ }
100
+ } else {
101
+ // 默认交互式同步
102
+ await syncer.sync();
103
+ }
104
+ } catch (error) {
105
+ console.error('❌ 程序执行失败:', error.message);
106
+ if (program.opts().verbose) {
107
+ console.error(error.stack);
108
+ }
109
+ process.exit(1);
110
+ }
111
+ }
112
+
113
+ // 处理未捕获的异常
114
+ process.on('uncaughtException', (error) => {
115
+ console.error('💥 未捕获的异常:', error.message);
116
+ process.exit(1);
117
+ });
118
+
119
+ process.on('unhandledRejection', (reason, promise) => {
120
+ console.error('💥 未处理的 Promise 拒绝:', reason);
121
+ process.exit(1);
122
+ });
123
+
124
+ // 启动程序
125
+ main();
package/lib/index.d.ts ADDED
@@ -0,0 +1,112 @@
1
+ import { FileConfig, TemplateSyncerOptions, BatchResults } from './types';
2
+ /**
3
+ * 智能模板同步工具
4
+ * 用于将项目与模板仓库保持同步
5
+ */
6
+ export declare class TemplateSyncer {
7
+ private tempDir;
8
+ private templateRepo;
9
+ private configFile;
10
+ private changes;
11
+ private verbose;
12
+ private ignorePatterns;
13
+ private fileTypeConfig;
14
+ private specialFiles;
15
+ constructor(options?: TemplateSyncerOptions);
16
+ /**
17
+ * 创建 Git 备份
18
+ */
19
+ createBackup(): void;
20
+ /**
21
+ * 克隆模板仓库
22
+ */
23
+ cloneTemplate(): Promise<void>; /**
24
+ * 获取或设置模板仓库
25
+ */
26
+ getTemplateRepo(): Promise<string>;
27
+ /**
28
+ * 测试仓库连接
29
+ */
30
+ testRepository(repo: string): Promise<boolean>;
31
+ /**
32
+ * 扫描模板文件
33
+ */
34
+ scanTemplateFiles(): Promise<FileConfig[]>;
35
+ /**
36
+ * 扫描当前项目文件
37
+ */
38
+ scanCurrentFiles(): Promise<FileConfig[]>;
39
+ /**
40
+ * 分析文件类型和特征
41
+ */
42
+ private analyzeFile;
43
+ /**
44
+ * 对比文件并生成变更列表
45
+ */
46
+ compareFiles(templateFiles: FileConfig[], currentFiles: FileConfig[]): Promise<FileConfig[]>;
47
+ /**
48
+ * 交互式选择要应用的变更
49
+ */
50
+ selectChangesToApply(changes: FileConfig[]): Promise<FileConfig[]>;
51
+ /**
52
+ * 按分类选择文件
53
+ */
54
+ private selectByCategory;
55
+ /**
56
+ * 逐一选择文件
57
+ */
58
+ private selectIndividually;
59
+ /**
60
+ * 批量更新文件
61
+ */
62
+ batchUpdateFiles(files: FileConfig[]): Promise<BatchResults>;
63
+ /**
64
+ * 更新单个文件
65
+ */
66
+ private updateSingleFile;
67
+ /**
68
+ * 智能合并 package.json
69
+ */
70
+ private mergePackageJson;
71
+ /**
72
+ * 显示操作摘要
73
+ */
74
+ showSummary(results: BatchResults): void;
75
+ /**
76
+ * 清理临时文件
77
+ */
78
+ cleanup(): void;
79
+ /**
80
+ * 加载配置文件
81
+ */
82
+ private loadConfig;
83
+ /**
84
+ * 保存配置文件
85
+ */
86
+ private saveConfig;
87
+ /**
88
+ * 主同步流程
89
+ */
90
+ sync(options?: TemplateSyncerOptions): Promise<void>;
91
+ /**
92
+ * 初始化配置向导
93
+ */
94
+ initConfig(): Promise<void>;
95
+ /**
96
+ * 批量处理模式
97
+ */
98
+ batchProcess(): Promise<void>;
99
+ /**
100
+ * 生成智能推荐
101
+ */
102
+ private generateRecommendations;
103
+ /**
104
+ * 预览所有差异
105
+ */
106
+ previewAllDifferences(files: FileConfig[]): Promise<void>;
107
+ /**
108
+ * 智能同步模式
109
+ */
110
+ intelligentSync(): Promise<void>;
111
+ }
112
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAOA,OAAO,EACL,UAAU,EAEV,qBAAqB,EAGrB,YAAY,EAGb,MAAM,SAAS,CAAC;AAEjB;;;GAGG;AACH,qBAAa,cAAc;IACzB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,YAAY,CAAqB;IACzC,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,OAAO,CAAW;IAC1B,OAAO,CAAC,OAAO,CAAU;IACzB,OAAO,CAAC,cAAc,CAAW;IACjC,OAAO,CAAC,cAAc,CAAiC;IACvD,OAAO,CAAC,YAAY,CAAiC;gBAEzC,OAAO,GAAE,qBAA0B;IAwK/C;;OAEG;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