@quicktvui/ai 1.0.0 → 1.0.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/postinstall.js +65 -16
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quicktvui/ai",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "QuickTVUI AI 开发规范与脚手架注入工具",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/postinstall.js CHANGED
@@ -11,7 +11,7 @@ if (!projectRoot || !fs.existsSync(projectRoot)) {
11
11
  }
12
12
 
13
13
  // 避免在包自身的开发环境中循环复制
14
- if (projectRoot === __dirname) {
14
+ if (projectRoot === __dirname || projectRoot === path.join(__dirname, '..', '..')) {
15
15
  process.exit(0);
16
16
  }
17
17
 
@@ -19,6 +19,9 @@ console.log("\n🚀 [@quicktvui/ai] 开始为您注入 QuickTVUI 专属 AI 规
19
19
  console.log(`📂 目标目录: ${projectRoot}`);
20
20
 
21
21
  const rulesDir = path.join(__dirname, "rules");
22
+ const backupBaseDir = path.join(projectRoot, ".quicktvui-ai-backup");
23
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
24
+ const currentBackupDir = path.join(backupBaseDir, `backup_${timestamp}`);
22
25
 
23
26
  function copyDirectorySync(src, dest) {
24
27
  if (!fs.existsSync(dest)) fs.mkdirSync(dest, { recursive: true });
@@ -35,27 +38,73 @@ function copyDirectorySync(src, dest) {
35
38
  }
36
39
 
37
40
  try {
38
- // 1. 拷贝规则
41
+ const rootItems = fs.readdirSync(rulesDir);
42
+ let backupNeeded = false;
43
+
44
+ // 1. 检查并备份现有配置
45
+ rootItems.forEach((item) => {
46
+ const targetPath = path.join(projectRoot, item);
47
+ if (fs.existsSync(targetPath)) {
48
+ if (!backupNeeded) {
49
+ if (!fs.existsSync(currentBackupDir)) fs.mkdirSync(currentBackupDir, { recursive: true });
50
+ backupNeeded = true;
51
+ }
52
+ const backupPath = path.join(currentBackupDir, item);
53
+ if (fs.lstatSync(targetPath).isDirectory()) {
54
+ copyDirectorySync(targetPath, backupPath);
55
+ } else {
56
+ fs.copyFileSync(targetPath, backupPath);
57
+ }
58
+ }
59
+ });
60
+
61
+ if (backupNeeded) {
62
+ console.log(`📦 已检测到现有 AI 配置文件,已备份至: .quicktvui-ai-backup/backup_${timestamp}`);
63
+ }
64
+
65
+ // 2. 注入新规则
39
66
  copyDirectorySync(rulesDir, projectRoot);
40
67
  console.log("🎉 [@quicktvui/ai] AI 规范注入成功!");
41
68
 
42
- // 2. 尝试 Git Add
43
- const gitDir = path.join(projectRoot, ".git");
44
- if (fs.existsSync(gitDir)) {
45
- console.log("📦 正在将配置文件添加到 Git 暂存区...");
46
- const rootItems = fs.readdirSync(rulesDir);
47
- rootItems.forEach((item) => {
48
- try {
49
- execSync(`git add "${item}"`, { cwd: projectRoot, stdio: "ignore" });
50
- } catch (e) {}
51
- });
52
- console.log("✅ 自动 git add 完成。");
53
- } else {
54
- console.log("ℹ️ 未检测到 Git 仓库,跳过自动 git add。");
69
+ // 3. 更新 .gitignore
70
+ const gitignorePath = path.join(projectRoot, ".gitignore");
71
+ let gitignoreContent = "";
72
+ if (fs.existsSync(gitignorePath)) {
73
+ gitignoreContent = fs.readFileSync(gitignorePath, "utf8");
55
74
  }
56
- } catch (err) {
75
+
76
+ const itemsToIgnore = [
77
+ ".quicktvui-ai-backup",
78
+ ...rootItems.map(item => item === ".github" ? ".github/copilot-instructions.md" : item)
79
+ ];
80
+
81
+ let updatedGitignore = gitignoreContent;
82
+ let hasChanges = false;
83
+
84
+ const sectionHeader = "# QuickTVUI AI Rules";
85
+ if (!updatedGitignore.includes(sectionHeader)) {
86
+ updatedGitignore += (updatedGitignore.length > 0 && !updatedGitignore.endsWith("\n") ? "\n" : "") + `\n${sectionHeader}\n`;
87
+ hasChanges = true;
88
+ }
89
+
90
+ itemsToIgnore.forEach((item) => {
91
+ const regex = new RegExp(`^${item.replace(/\./g, "\\.")}$`, "m");
92
+ if (!regex.test(updatedGitignore)) {
93
+ if (!updatedGitignore.endsWith("\n")) updatedGitignore += "\n";
94
+ updatedGitignore += `${item}\n`;
95
+ hasChanges = true;
96
+ }
97
+ });
98
+
99
+ if (hasChanges) {
100
+ fs.writeFileSync(gitignorePath, updatedGitignore);
101
+ console.log("✅ 已更新 .gitignore,排除 AI 配置文件。");
102
+ }
103
+ } catch (err) {
104
+
57
105
  console.error(`❌ 注入失败: ${err.message}`);
58
106
  }
107
+
59
108
  console.log(
60
109
  "\n🤖 您的项目现已全面兼容 Cursor, Windsurf, Cline 等 AI 助手!开发愉快!\n",
61
110
  );