tools-cc 1.0.3 → 1.0.5

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.
@@ -1,87 +1,161 @@
1
1
  import fs from 'fs-extra';
2
2
  import path from 'path';
3
- import { ProjectConfig } from '../types';
3
+ import { ProjectConfig, LegacyProjectConfig, normalizeProjectConfig, SourceSelection, ExportConfig } from '../types';
4
4
  import { loadManifest } from './manifest';
5
5
  import { getToolsccDir, getProjectConfigPath } from '../utils/path';
6
6
 
7
+ /**
8
+ * 默认选择配置 - 导入所有内容
9
+ */
10
+ const DEFAULT_SELECTION: SourceSelection = {
11
+ skills: ['*'],
12
+ commands: ['*'],
13
+ agents: ['*']
14
+ };
15
+
7
16
  export async function initProject(projectDir: string): Promise<void> {
8
17
  const toolsccDir = getToolsccDir(projectDir);
9
18
  const configFile = getProjectConfigPath(projectDir);
10
-
19
+
11
20
  // Create .toolscc directory structure
12
21
  await fs.ensureDir(path.join(toolsccDir, 'skills'));
13
22
  await fs.ensureDir(path.join(toolsccDir, 'commands'));
14
23
  await fs.ensureDir(path.join(toolsccDir, 'agents'));
15
-
24
+
16
25
  // Create project config if not exists
17
26
  if (!(await fs.pathExists(configFile))) {
18
27
  const config: ProjectConfig = {
19
- sources: [],
28
+ sources: {},
20
29
  links: []
21
30
  };
22
31
  await fs.writeJson(configFile, config, { spaces: 2 });
23
32
  }
24
33
  }
25
34
 
35
+ /**
36
+ * 读取项目配置,自动处理新旧格式
37
+ */
38
+ async function readProjectConfig(configFile: string): Promise<ProjectConfig> {
39
+ const rawConfig = await fs.readJson(configFile);
40
+ return normalizeProjectConfig(rawConfig);
41
+ }
42
+
43
+ /**
44
+ * 获取源名称列表(兼容新旧格式)
45
+ */
46
+ function getSourceNames(config: ProjectConfig): string[] {
47
+ return Object.keys(config.sources);
48
+ }
49
+
50
+ /**
51
+ * 检查是否应该复制某项(根据选择配置)
52
+ */
53
+ function shouldInclude(itemName: string, selection: string[]): boolean {
54
+ // 如果选择包含通配符,包含所有项
55
+ if (selection.includes('*')) {
56
+ return true;
57
+ }
58
+ // 否则检查是否在选择列表中
59
+ return selection.includes(itemName);
60
+ }
61
+
26
62
  export async function useSource(
27
63
  sourceName: string,
28
64
  sourceDir: string,
29
- projectDir: string
65
+ projectDir: string,
66
+ selection?: SourceSelection
30
67
  ): Promise<void> {
31
68
  // Input validation
32
69
  if (!sourceName || !sourceName.trim()) {
33
70
  throw new Error('Source name is required');
34
71
  }
35
-
72
+
36
73
  // Check source directory existence
37
74
  if (!(await fs.pathExists(sourceDir))) {
38
75
  throw new Error(`Source directory does not exist: ${sourceDir}`);
39
76
  }
40
-
77
+
41
78
  const toolsccDir = getToolsccDir(projectDir);
42
79
  const manifest = await loadManifest(sourceDir);
43
-
80
+
44
81
  // Ensure project is initialized
45
82
  await initProject(projectDir);
46
-
83
+
84
+ // 使用传入的选择配置或默认配置
85
+ const effectiveSelection: SourceSelection = selection ?? DEFAULT_SELECTION;
86
+
47
87
  // Copy/link skills (flattened with prefix)
48
88
  const sourceSkillsDir = path.join(sourceDir, 'skills');
49
89
  if (await fs.pathExists(sourceSkillsDir)) {
50
90
  const skills = await fs.readdir(sourceSkillsDir);
51
91
  for (const skill of skills) {
92
+ // 检查是否应该包含此 skill
93
+ if (!shouldInclude(skill, effectiveSelection.skills)) {
94
+ continue;
95
+ }
96
+
52
97
  const srcPath = path.join(sourceSkillsDir, skill);
53
- const destPath = path.join(toolsccDir, 'skills', `${sourceName}-${skill}`);
54
-
98
+ const name = `${sourceName}` == `${skill}` ? skill : `${sourceName}-${skill}`;
99
+ const destPath = path.join(toolsccDir, 'skills', name);
100
+
55
101
  // Remove existing if exists
56
102
  await fs.remove(destPath);
57
-
103
+
58
104
  // Copy directory
59
105
  await fs.copy(srcPath, destPath);
60
106
  }
61
107
  }
62
-
108
+
63
109
  // Copy commands (in subdirectory by source name)
64
110
  const sourceCommandsDir = path.join(sourceDir, 'commands');
65
111
  if (await fs.pathExists(sourceCommandsDir)) {
66
- const destDir = path.join(toolsccDir, 'commands', sourceName);
67
- await fs.remove(destDir);
68
- await fs.copy(sourceCommandsDir, destDir);
112
+ // 检查是否有选择 commands
113
+ if (effectiveSelection.commands.includes('*')) {
114
+ // 复制所有 commands
115
+ const destDir = path.join(toolsccDir, 'commands', sourceName);
116
+ await fs.remove(destDir);
117
+ await fs.copy(sourceCommandsDir, destDir);
118
+ } else if (effectiveSelection.commands.length > 0) {
119
+ // 只复制选中的 commands
120
+ const destDir = path.join(toolsccDir, 'commands', sourceName);
121
+ await fs.ensureDir(destDir);
122
+
123
+ for (const cmdName of effectiveSelection.commands) {
124
+ const srcFile = path.join(sourceCommandsDir, `${cmdName}.md`);
125
+ if (await fs.pathExists(srcFile)) {
126
+ await fs.copy(srcFile, path.join(destDir, `${cmdName}.md`));
127
+ }
128
+ }
129
+ }
69
130
  }
70
-
131
+
71
132
  // Copy agents (in subdirectory by source name)
72
133
  const sourceAgentsDir = path.join(sourceDir, 'agents');
73
134
  if (await fs.pathExists(sourceAgentsDir)) {
74
- const destDir = path.join(toolsccDir, 'agents', sourceName);
75
- await fs.remove(destDir);
76
- await fs.copy(sourceAgentsDir, destDir);
135
+ // 检查是否有选择 agents
136
+ if (effectiveSelection.agents.includes('*')) {
137
+ // 复制所有 agents
138
+ const destDir = path.join(toolsccDir, 'agents', sourceName);
139
+ await fs.remove(destDir);
140
+ await fs.copy(sourceAgentsDir, destDir);
141
+ } else if (effectiveSelection.agents.length > 0) {
142
+ // 只复制选中的 agents
143
+ const destDir = path.join(toolsccDir, 'agents', sourceName);
144
+ await fs.ensureDir(destDir);
145
+
146
+ for (const agentName of effectiveSelection.agents) {
147
+ const srcFile = path.join(sourceAgentsDir, `${agentName}.md`);
148
+ if (await fs.pathExists(srcFile)) {
149
+ await fs.copy(srcFile, path.join(destDir, `${agentName}.md`));
150
+ }
151
+ }
152
+ }
77
153
  }
78
-
79
- // Update project config
154
+
155
+ // Update project config - 保存实际使用的选择配置
80
156
  const configFile = getProjectConfigPath(projectDir);
81
- const config: ProjectConfig = await fs.readJson(configFile);
82
- if (!config.sources.includes(sourceName)) {
83
- config.sources.push(sourceName);
84
- }
157
+ const config = await readProjectConfig(configFile);
158
+ config.sources[sourceName] = effectiveSelection;
85
159
  await fs.writeJson(configFile, config, { spaces: 2 });
86
160
  }
87
161
 
@@ -90,10 +164,10 @@ export async function unuseSource(sourceName: string, projectDir: string): Promi
90
164
  if (!sourceName || !sourceName.trim()) {
91
165
  throw new Error('Source name is required');
92
166
  }
93
-
167
+
94
168
  const toolsccDir = getToolsccDir(projectDir);
95
169
  const configFile = getProjectConfigPath(projectDir);
96
-
170
+
97
171
  // Remove skills with prefix
98
172
  const skillsDir = path.join(toolsccDir, 'skills');
99
173
  if (await fs.pathExists(skillsDir)) {
@@ -104,33 +178,100 @@ export async function unuseSource(sourceName: string, projectDir: string): Promi
104
178
  }
105
179
  }
106
180
  }
107
-
181
+
108
182
  // Remove commands subdirectory
109
183
  await fs.remove(path.join(toolsccDir, 'commands', sourceName));
110
-
184
+
111
185
  // Remove agents subdirectory
112
186
  await fs.remove(path.join(toolsccDir, 'agents', sourceName));
113
-
187
+
114
188
  // Update project config with error handling
115
189
  let config: ProjectConfig;
116
190
  try {
117
- config = await fs.readJson(configFile);
191
+ config = await readProjectConfig(configFile);
118
192
  } catch (error) {
119
193
  // If config file doesn't exist or is invalid, nothing to update
120
194
  return;
121
195
  }
122
-
123
- config.sources = config.sources.filter(s => s !== sourceName);
196
+
197
+ delete config.sources[sourceName];
124
198
  await fs.writeJson(configFile, config, { spaces: 2 });
125
199
  }
126
200
 
127
201
  export async function listUsedSources(projectDir: string): Promise<string[]> {
128
202
  const configFile = getProjectConfigPath(projectDir);
129
-
203
+
130
204
  if (!(await fs.pathExists(configFile))) {
131
205
  return [];
132
206
  }
133
-
134
- const config: ProjectConfig = await fs.readJson(configFile);
135
- return config.sources;
207
+
208
+ const config = await readProjectConfig(configFile);
209
+ return getSourceNames(config);
210
+ }
211
+
212
+ /**
213
+ * 导出项目配置到 JSON 文件
214
+ */
215
+ export async function exportProjectConfig(
216
+ projectDir: string,
217
+ outputPath: string
218
+ ): Promise<void> {
219
+ const configFile = getProjectConfigPath(projectDir);
220
+
221
+ if (!(await fs.pathExists(configFile))) {
222
+ throw new Error('Project not initialized. Use `tools-cc use <source>` to get started.');
223
+ }
224
+
225
+ const config = await readProjectConfig(configFile);
226
+
227
+ const exportConfig: ExportConfig = {
228
+ version: '1.0',
229
+ type: 'project',
230
+ config,
231
+ exportedAt: new Date().toISOString()
232
+ };
233
+
234
+ await fs.writeJson(outputPath, exportConfig, { spaces: 2 });
136
235
  }
236
+
237
+ /**
238
+ * 从 JSON 文件导入项目配置
239
+ */
240
+ export async function importProjectConfig(
241
+ configPath: string,
242
+ projectDir: string,
243
+ resolveSourcePath: (sourceName: string) => Promise<string>
244
+ ): Promise<void> {
245
+ if (!(await fs.pathExists(configPath))) {
246
+ throw new Error(`Config file not found: ${configPath}`);
247
+ }
248
+
249
+ const exportConfig: ExportConfig = await fs.readJson(configPath);
250
+
251
+ // Validate version
252
+ if (exportConfig.version !== '1.0') {
253
+ throw new Error(`Unsupported config version: ${exportConfig.version}`);
254
+ }
255
+
256
+ // Validate type
257
+ if (exportConfig.type !== 'project') {
258
+ throw new Error(`Invalid config type: ${exportConfig.type}. Expected 'project'.`);
259
+ }
260
+
261
+ // Initialize project
262
+ await initProject(projectDir);
263
+
264
+ // Apply each source
265
+ for (const [sourceName, selection] of Object.entries(exportConfig.config.sources)) {
266
+ const sourceDir = await resolveSourcePath(sourceName);
267
+ await useSource(sourceName, sourceDir, projectDir, selection);
268
+ }
269
+
270
+ // Update links if present
271
+ if (exportConfig.config.links && exportConfig.config.links.length > 0) {
272
+ const configFile = getProjectConfigPath(projectDir);
273
+ const config = await readProjectConfig(configFile);
274
+ config.links = exportConfig.config.links;
275
+ await fs.writeJson(configFile, config, { spaces: 2 });
276
+ }
277
+ }