-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Expand file tree
/
Copy pathtemplate.ts
More file actions
executable file
·244 lines (210 loc) · 6.82 KB
/
Copy pathtemplate.ts
File metadata and controls
executable file
·244 lines (210 loc) · 6.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import { readFile, mkdir, writeFile, cp, access, readdir } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { homedir } from 'node:os';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
// After bun build: dist/index.js -> ../assets = cli/assets ✓
const ASSETS_DIR = join(__dirname, '..', 'assets');
export interface PlatformConfig {
platform: string;
displayName: string;
installType: 'full' | 'reference';
folderStructure: {
root: string;
skillPath: string;
filename: string;
};
scriptPath: string;
frontmatter: Record<string, string> | null;
sections: {
quickReference: boolean;
};
title: string;
description: string;
skillOrWorkflow: string;
}
// Map AIType to platform config file name
const AI_TO_PLATFORM: Record<string, string> = {
claude: 'claude',
cursor: 'cursor',
windsurf: 'windsurf',
antigravity: 'agent',
copilot: 'copilot',
'copilot-cli': 'copilot-cli',
kiro: 'kiro',
opencode: 'opencode',
roocode: 'roocode',
codex: 'codex',
qoder: 'qoder',
gemini: 'gemini',
trae: 'trae',
continue: 'continue',
codebuddy: 'codebuddy',
droid: 'droid',
kilocode: 'kilocode',
warp: 'warp',
augment: 'augment',
};
async function exists(path: string): Promise<boolean> {
try {
await access(path);
return true;
} catch {
return false;
}
}
/**
* Load platform configuration from JSON file
*/
export async function loadPlatformConfig(aiType: string): Promise<PlatformConfig> {
const platformName = AI_TO_PLATFORM[aiType];
if (!platformName) {
throw new Error(`Unknown AI type: ${aiType}`);
}
const configPath = join(ASSETS_DIR, 'templates', 'platforms', `${platformName}.json`);
const content = await readFile(configPath, 'utf-8');
return JSON.parse(content) as PlatformConfig;
}
/**
* Load all available platform configs
*/
export async function loadAllPlatformConfigs(): Promise<Map<string, PlatformConfig>> {
const configs = new Map<string, PlatformConfig>();
for (const [aiType, platformName] of Object.entries(AI_TO_PLATFORM)) {
try {
const config = await loadPlatformConfig(aiType);
configs.set(aiType, config);
} catch {
// Skip if config doesn't exist
}
}
return configs;
}
/**
* Load a template file
*/
async function loadTemplate(templateName: string): Promise<string> {
const templatePath = join(ASSETS_DIR, 'templates', templateName);
return readFile(templatePath, 'utf-8');
}
/**
* Render frontmatter section
*/
function renderFrontmatter(frontmatter: Record<string, string> | null): string {
if (!frontmatter) return '';
const lines = ['---'];
for (const [key, value] of Object.entries(frontmatter)) {
// Quote values that contain special characters
if (value.includes(':') || value.includes('"') || value.includes('\n')) {
lines.push(`${key}: "${value.replace(/"/g, '\\"')}"`);
} else {
lines.push(`${key}: ${value}`);
}
}
lines.push('---', '');
return lines.join('\n');
}
/**
* Render skill file content from template
* When isGlobal=true, rewrites script paths to use ~/{root}/ prefix
*/
export async function renderSkillFile(config: PlatformConfig, isGlobal = false): Promise<string> {
// Load base template
let content = await loadTemplate('base/skill-content.md');
// Load quick reference if needed
let quickReferenceContent = '';
if (config.sections.quickReference) {
quickReferenceContent = await loadTemplate('base/quick-reference.md');
}
// Build the final content
const frontmatter = renderFrontmatter(config.frontmatter);
// Replace placeholders
// Add newline before quick reference content if it exists
const quickRefWithNewline = quickReferenceContent ? '\n' + quickReferenceContent : '';
content = content
.replace(/\{\{TITLE\}\}/g, config.title)
.replace(/\{\{DESCRIPTION\}\}/g, config.description)
.replace(/\{\{SCRIPT_PATH\}\}/g, config.scriptPath)
.replace(/\{\{SKILL_OR_WORKFLOW\}\}/g, config.skillOrWorkflow)
.replace(/\{\{QUICK_REFERENCE\}\}/g, quickRefWithNewline);
// For global install, rewrite relative script paths to absolute ~/root/ paths
if (isGlobal) {
const globalPrefix = `~/${config.folderStructure.root}/`;
content = content.replace(
/python3 skills\//g,
`python3 ${globalPrefix}skills/`
);
}
return frontmatter + content;
}
/**
* Copy data and scripts to target directory
*/
async function copyDataAndScripts(targetSkillDir: string): Promise<void> {
const dataSource = join(ASSETS_DIR, 'data');
const scriptsSource = join(ASSETS_DIR, 'scripts');
const dataTarget = join(targetSkillDir, 'data');
const scriptsTarget = join(targetSkillDir, 'scripts');
// Copy data
if (await exists(dataSource)) {
await mkdir(dataTarget, { recursive: true });
await cp(dataSource, dataTarget, { recursive: true });
}
// Copy scripts
if (await exists(scriptsSource)) {
await mkdir(scriptsTarget, { recursive: true });
await cp(scriptsSource, scriptsTarget, { recursive: true });
}
}
/**
* Generate platform files for a specific AI type
* All platforms use self-contained installation with data and scripts
* When isGlobal=true, installs to ~/home directory with absolute script paths
*/
export async function generatePlatformFiles(
targetDir: string,
aiType: string,
isGlobal = false
): Promise<string[]> {
const config = await loadPlatformConfig(aiType);
const createdFolders: string[] = [];
// For global install, target the user's home directory
const effectiveDir = isGlobal ? homedir() : targetDir;
// Determine full skill directory path
const skillDir = join(
effectiveDir,
config.folderStructure.root,
config.folderStructure.skillPath
);
// Create directory structure
await mkdir(skillDir, { recursive: true });
// Render and write skill file (pass isGlobal to adjust paths)
const skillContent = await renderSkillFile(config, isGlobal);
const skillFilePath = join(skillDir, config.folderStructure.filename);
await writeFile(skillFilePath, skillContent, 'utf-8');
createdFolders.push(config.folderStructure.root);
// Copy data and scripts into the skill directory (self-contained)
await copyDataAndScripts(skillDir);
return createdFolders;
}
/**
* Generate files for all AI types
*/
export async function generateAllPlatformFiles(targetDir: string, isGlobal = false): Promise<string[]> {
const allFolders = new Set<string>();
for (const aiType of Object.keys(AI_TO_PLATFORM)) {
try {
const folders = await generatePlatformFiles(targetDir, aiType, isGlobal);
folders.forEach(f => allFolders.add(f));
} catch {
// Skip if generation fails for a platform
}
}
return Array.from(allFolders);
}
/**
* Get list of supported AI types
*/
export function getSupportedAITypes(): string[] {
return Object.keys(AI_TO_PLATFORM);
}