(outputRoot: string, bundle: PiBundle)
| 57 | } |
| 58 | |
| 59 | export async function writePiBundle(outputRoot: string, bundle: PiBundle): Promise<void> { |
| 60 | const pluginName = bundle.pluginName ? sanitizeCodexPathComponent(bundle.pluginName) : undefined |
| 61 | const paths = resolvePiPaths(outputRoot, pluginName) |
| 62 | const manifest = pluginName |
| 63 | ? await readInstallManifestWithLegacyFallback(paths, pluginName) |
| 64 | : null |
| 65 | const currentPrompts = bundle.prompts.map((prompt) => `${sanitizePathName(prompt.name)}.md`) |
| 66 | const currentSkills = [ |
| 67 | ...bundle.skillDirs.map((skill) => sanitizePathName(skill.name)), |
| 68 | ...bundle.generatedSkills.map((skill) => sanitizePathName(skill.name)), |
| 69 | ] |
| 70 | const currentAgents = bundle.agents.map((agent) => `${sanitizePathName(agent.name)}.md`) |
| 71 | const currentExtensions = bundle.extensions.map((extension) => extension.name) |
| 72 | |
| 73 | await ensureDir(paths.skillsDir) |
| 74 | await ensureDir(paths.promptsDir) |
| 75 | await ensureDir(paths.extensionsDir) |
| 76 | await ensureDir(paths.agentsDir) |
| 77 | |
| 78 | await cleanupStaleAgents(paths.skillsDir, null) |
| 79 | await cleanupRemovedPrompts(paths.promptsDir, manifest, currentPrompts) |
| 80 | await cleanupRemovedSkills(paths.skillsDir, manifest, currentSkills) |
| 81 | await cleanupRemovedAgents(paths.agentsDir, manifest, currentAgents) |
| 82 | await cleanupRemovedExtensions(paths.extensionsDir, manifest, currentExtensions) |
| 83 | |
| 84 | for (const prompt of bundle.prompts) { |
| 85 | await writeText(path.join(paths.promptsDir, `${sanitizePathName(prompt.name)}.md`), prompt.content + "\n") |
| 86 | } |
| 87 | |
| 88 | for (const skill of bundle.skillDirs) { |
| 89 | const skillName = sanitizePathName(skill.name) |
| 90 | const targetDir = path.join(paths.skillsDir, skillName) |
| 91 | await cleanupCurrentManagedSkillDir(targetDir, manifest, skillName) |
| 92 | await copySkillDir(skill.sourceDir, targetDir, transformContentForPi) |
| 93 | } |
| 94 | |
| 95 | for (const skill of bundle.generatedSkills) { |
| 96 | const skillName = sanitizePathName(skill.name) |
| 97 | const targetDir = path.join(paths.skillsDir, skillName) |
| 98 | await cleanupCurrentManagedSkillDir(targetDir, manifest, skillName) |
| 99 | await writeText(path.join(targetDir, "SKILL.md"), skill.content + "\n") |
| 100 | } |
| 101 | |
| 102 | for (const agent of bundle.agents) { |
| 103 | const agentFileName = `${sanitizePathName(agent.name)}.md` |
| 104 | const targetPath = path.join(paths.agentsDir, agentFileName) |
| 105 | await cleanupCurrentManagedAgentFile(targetPath, manifest, agentFileName) |
| 106 | await writeText(targetPath, agent.content + "\n") |
| 107 | } |
| 108 | |
| 109 | for (const extension of bundle.extensions) { |
| 110 | await writeText(path.join(paths.extensionsDir, extension.name), extension.content + "\n") |
| 111 | } |
| 112 | |
| 113 | if (bundle.mcporterConfig) { |
| 114 | const backupPath = await backupFile(paths.mcporterConfigPath) |
| 115 | if (backupPath) { |
| 116 | console.log(`Backed up existing MCPorter config to ${backupPath}`) |
no test coverage detected