diff --git a/cli/src/utils/template.ts b/cli/src/utils/template.ts index 170effa..05ef879 100755 --- a/cli/src/utils/template.ts +++ b/cli/src/utils/template.ts @@ -260,6 +260,14 @@ export async function listBundledSubSkills(): Promise { return entries.filter(e => e.isDirectory()).map(e => e.name).sort(); } +async function* subSkillsToCopy(skillsParentDir: string, force: boolean): AsyncGenerator { + for (const name of await listBundledSubSkills()) { + if (force || !(await exists(join(skillsParentDir, name)))) { + yield name; + } + } +} + /** * Paths of an earlier layout of aiType that exist under baseDir: the old skill * file, the orchestrator folder and the bundled sub-skills left next to it. @@ -302,11 +310,8 @@ export async function removeLegacyInstall(baseDir: string, aiType: string): Prom */ async function copySubSkills(skillsParentDir: string, force: boolean): Promise { const skillsSource = join(ASSETS_DIR, 'skills'); - if (!(await exists(skillsSource))) return; - - for (const name of await listBundledSubSkills()) { + for await (const name of subSkillsToCopy(skillsParentDir, force)) { const target = join(skillsParentDir, name); - if (await exists(target) && !force) continue; await mkdir(target, { recursive: true }); await cp(join(skillsSource, name), target, { recursive: true }); } @@ -430,7 +435,10 @@ export async function planPlatformInstallActions( actions.push(`Would write: ${skillFilePath}`); actions.push(`Would copy data + scripts: ${dataDir}`); - const subSkills = await listBundledSubSkills(); + const subSkills: string[] = []; + for await (const name of subSkillsToCopy(skillsParentDir, force)) { + subSkills.push(name); + } if (subSkills.length > 0) { actions.push( `Would copy ${subSkills.length} sub-skills (${subSkills.join(', ')}): ${skillsParentDir}` diff --git a/cli/tests/e2e/dry-run.spec.ts b/cli/tests/e2e/dry-run.spec.ts index 3128e1a..b432305 100644 --- a/cli/tests/e2e/dry-run.spec.ts +++ b/cli/tests/e2e/dry-run.spec.ts @@ -55,6 +55,59 @@ test('dry-run skips all install actions for an existing skill unless forced', as expect(await readdir(skillDir)).toEqual(['SKILL.md']); }); +for (const force of [false, true]) { + test(`dry-run matches the sub-skills copied ${force ? 'with --force' : 'without --force'}`, async ({}, testInfo) => { + const scratch = testInfo.outputPath('project'); + const skillsDir = join(scratch, '.opencode', 'skills'); + const brandDir = join(skillsDir, 'brand'); + const brandFile = join(brandDir, 'SKILL.md'); + const existingBrand = 'existing brand skill'; + await mkdir(brandDir, { recursive: true }); + await writeFile(brandFile, existingBrand); + + const bundledSkills = await listBundledSubSkills(); + const copiedSkills = force ? bundledSkills : bundledSkills.filter(name => name !== 'brand'); + const actions = await planPlatformInstallActions(scratch, 'opencode', false, force); + expect(actions).toContain( + `Would copy ${copiedSkills.length} sub-skills (${copiedSkills.join(', ')}): ${skillsDir}` + ); + expect(await readdir(skillsDir)).toEqual(['brand']); + expect(await readFile(brandFile, 'utf-8')).toBe(existingBrand); + + await generatePlatformFiles(scratch, 'opencode', false, force); + for (const name of copiedSkills) { + const bundledFile = new URL(`../../assets/skills/${name}/SKILL.md`, import.meta.url); + const installedFile = join(skillsDir, name, 'SKILL.md'); + expect(await readFile(installedFile, 'utf-8')).toBe(await readFile(bundledFile, 'utf-8')); + } + if (!force) { + expect(await readFile(brandFile, 'utf-8')).toBe(existingBrand); + } + }); +} + +test('dry-run omits the sub-skill copy action when all target directories exist', async ({}, testInfo) => { + const scratch = testInfo.outputPath('project'); + const skillsDir = join(scratch, '.opencode', 'skills'); + const bundledSkills = await listBundledSubSkills(); + for (const name of bundledSkills) { + await mkdir(join(skillsDir, name), { recursive: true }); + } + + const actions = await planPlatformInstallActions(scratch, 'opencode'); + const orchestratorDir = join(skillsDir, 'ui-ux-pro-max'); + expect(actions).toEqual([ + `Would write: ${join(orchestratorDir, 'SKILL.md')}`, + `Would copy data + scripts: ${orchestratorDir}`, + ]); + expect((await readdir(skillsDir)).sort()).toEqual(bundledSkills); + + await generatePlatformFiles(scratch, 'opencode'); + for (const name of bundledSkills) { + expect(await readdir(join(skillsDir, name))).toEqual([]); + } +}); + test('dry-run plan supports the zcode platform layout', async () => { const scratch = await mkdtemp(join(tmpdir(), 'uipro-dry-run-zcode-'));