Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"lint:write": "biome lint --write",
"format": "biome check --write --unsafe && prettier --write \"**/*.{md,yml,yaml,css}\"",
"format:check": "biome check && prettier --check \"**/*.{md,yml,yaml,css}\"",
"postinstall": "node dist/postinstall.js || true",
"test": "vitest run",
"test:watch": "vitest"
},
Expand Down
188 changes: 188 additions & 0 deletions src/__tests__/postinstall.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { describe, expect, it, vi } from 'vitest'
import type { SkillInstaller } from '../lib/skills/types.js'

vi.mock('../lib/skills/index.js', () => ({
skillInstallers: {} as Record<string, SkillInstaller>,
}))

import { skillInstallers } from '../lib/skills/index.js'
import { updateAllInstalledSkills } from '../lib/skills/update-installed.js'

function mockInstaller(overrides: Partial<SkillInstaller> = {}): SkillInstaller {
return {
name: 'test',
description: 'test',
getInstallPath: vi.fn(() => '/test/path'),
generateContent: vi.fn(() => 'content'),
isInstalled: vi.fn(async () => false),
install: vi.fn(async () => {}),
update: vi.fn(async () => {}),
uninstall: vi.fn(async () => {}),
...overrides,
}
}

function setInstaller(name: string, installer: SkillInstaller) {
;(skillInstallers as Record<string, SkillInstaller>)[name] = installer
}

function removeInstaller(name: string) {
const installers = skillInstallers as Record<string, SkillInstaller>
delete installers[name]
}

function getInstaller(name: string): SkillInstaller {
return (skillInstallers as Record<string, SkillInstaller>)[name]
}

function clearInstallers() {
const installers = skillInstallers as Record<string, SkillInstaller>
for (const key of Object.keys(installers)) {
delete installers[key]
}
}

describe('updateAllInstalledSkills', () => {
it('updates all installed skills', async () => {
setInstaller(
'agent-a',
mockInstaller({
name: 'agent-a',
isInstalled: vi.fn(async () => true),
}),
)
setInstaller(
'agent-b',
mockInstaller({
name: 'agent-b',
isInstalled: vi.fn(async () => true),
}),
)

const result = await updateAllInstalledSkills(false)

expect(result.updated).toEqual(['agent-a', 'agent-b'])
expect(result.skipped).toEqual([])
expect(result.errors).toEqual([])
expect(getInstaller('agent-a').update).toHaveBeenCalledWith(false)
expect(getInstaller('agent-b').update).toHaveBeenCalledWith(false)

removeInstaller('agent-a')
removeInstaller('agent-b')
})

it('skips agents that are not installed', async () => {
setInstaller(
'agent-installed',
mockInstaller({
name: 'agent-installed',
isInstalled: vi.fn(async () => true),
}),
)
setInstaller(
'agent-missing',
mockInstaller({
name: 'agent-missing',
isInstalled: vi.fn(async () => false),
}),
)

const result = await updateAllInstalledSkills(false)

expect(result.updated).toEqual(['agent-installed'])
expect(result.skipped).toEqual(['agent-missing'])
expect(result.errors).toEqual([])
expect(getInstaller('agent-installed').update).toHaveBeenCalledWith(false)
expect(getInstaller('agent-missing').update).not.toHaveBeenCalled()

removeInstaller('agent-installed')
removeInstaller('agent-missing')
})

it('continues updating remaining agents if one fails', async () => {
setInstaller(
'agent-failing',
mockInstaller({
name: 'agent-failing',
isInstalled: vi.fn(async () => true),
update: vi.fn(async () => {
throw new Error('update failed')
}),
}),
)
setInstaller(
'agent-working',
mockInstaller({
name: 'agent-working',
isInstalled: vi.fn(async () => true),
}),
)

const result = await updateAllInstalledSkills(false)

expect(result.errors).toEqual(['agent-failing'])
expect(result.updated).toEqual(['agent-working'])
expect(getInstaller('agent-working').update).toHaveBeenCalledWith(false)

removeInstaller('agent-failing')
removeInstaller('agent-working')
})

it('never throws even when all operations fail', async () => {
setInstaller(
'fail-1',
mockInstaller({
name: 'fail-1',
isInstalled: vi.fn(async () => {
throw new Error('check failed')
}),
}),
)
setInstaller(
'fail-2',
mockInstaller({
name: 'fail-2',
isInstalled: vi.fn(async () => true),
update: vi.fn(async () => {
throw new Error('update failed')
}),
}),
)

const result = await updateAllInstalledSkills(false)

expect(result.errors).toEqual(['fail-1', 'fail-2'])
expect(result.updated).toEqual([])
expect(result.skipped).toEqual([])

removeInstaller('fail-1')
removeInstaller('fail-2')
})

it('returns correct result when no agents exist', async () => {
clearInstallers()

const result = await updateAllInstalledSkills(false)

expect(result.updated).toEqual([])
expect(result.skipped).toEqual([])
expect(result.errors).toEqual([])
})

it('passes local flag through to isInstalled and update', async () => {
setInstaller(
'local-agent',
mockInstaller({
name: 'local-agent',
isInstalled: vi.fn(async () => true),
}),
)

await updateAllInstalledSkills(true)

expect(getInstaller('local-agent').isInstalled).toHaveBeenCalledWith(true)
expect(getInstaller('local-agent').update).toHaveBeenCalledWith(true)

removeInstaller('local-agent')
})
})
35 changes: 35 additions & 0 deletions src/__tests__/skill.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ vi.mock('chalk', () => ({
},
}))

vi.mock('../lib/skills/update-installed.js', () => ({
updateAllInstalledSkills: vi.fn(),
}))

import { registerSkillCommand } from '../commands/skill.js'
import { createInstaller } from '../lib/skills/create-installer.js'
import { getInstaller, listAgents, skillInstallers } from '../lib/skills/index.js'
import { updateAllInstalledSkills } from '../lib/skills/update-installed.js'

function createProgram() {
const program = new Command()
Expand Down Expand Up @@ -78,6 +83,36 @@ describe('skill command', () => {
program.parseAsync(['node', 'td', 'skill', 'update', 'unknown-agent']),
).rejects.toThrow('Unknown agent: unknown-agent')
})

it('updates all installed agents when "all" is passed', async () => {
vi.mocked(updateAllInstalledSkills).mockResolvedValue({
updated: ['claude-code', 'cursor'],
skipped: ['codex'],
errors: [],
})

const program = createProgram()
await program.parseAsync(['node', 'td', 'skill', 'update', 'all'])

expect(updateAllInstalledSkills).toHaveBeenCalledWith(false)
expect(consoleSpy).toHaveBeenCalledWith('✓', 'Updated claude-code skill')
expect(consoleSpy).toHaveBeenCalledWith('✓', 'Updated cursor skill')
expect(consoleSpy).toHaveBeenCalledWith(' Skipped codex (not installed)')
})

it('shows message when no agents are installed for "all"', async () => {
vi.mocked(updateAllInstalledSkills).mockResolvedValue({
updated: [],
skipped: ['claude-code', 'codex', 'cursor'],
errors: [],
})

const program = createProgram()
await program.parseAsync(['node', 'td', 'skill', 'update', 'all'])

expect(updateAllInstalledSkills).toHaveBeenCalledWith(false)
expect(consoleSpy).toHaveBeenCalledWith('No installed skills found to update.')
})
})

describe('uninstall subcommand', () => {
Expand Down
25 changes: 25 additions & 0 deletions src/commands/skill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chalk from 'chalk'
import { Command } from 'commander'
import { formatError } from '../lib/output.js'
import { getInstaller, listAgents, skillInstallers } from '../lib/skills/index.js'
import { updateAllInstalledSkills } from '../lib/skills/update-installed.js'

interface InstallOptions {
local?: boolean
Expand Down Expand Up @@ -37,6 +38,27 @@ async function installSkill(agent: string, options: InstallOptions): Promise<voi
console.log(chalk.dim(filepath))
}

async function updateAllSkills(options: UpdateOptions): Promise<void> {
const local = options.local ?? false
const result = await updateAllInstalledSkills(local)

for (const name of result.updated) {
console.log(chalk.green('✓'), `Updated ${name} skill`)
}

for (const name of result.skipped) {
console.log(chalk.dim(` Skipped ${name} (not installed)`))
}

for (const name of result.errors) {
console.log(chalk.red('✗'), `Failed to update ${name}`)
}

if (result.updated.length === 0 && result.errors.length === 0) {
console.log(chalk.dim('No installed skills found to update.'))
}
}

async function updateSkill(agent: string, options: UpdateOptions): Promise<void> {
const installer = getInstaller(agent)
if (!installer) {
Expand Down Expand Up @@ -132,6 +154,9 @@ export function registerSkillCommand(program: Command): void {
updateCmd.help()
return
}
if (agent === 'all') {
return updateAllSkills(options)
}
return updateSkill(agent, options)
})

Expand Down
29 changes: 29 additions & 0 deletions src/lib/skills/update-installed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { skillInstallers } from './index.js'

export interface UpdateAllResult {
updated: string[]
skipped: string[]
errors: string[]
}

export async function updateAllInstalledSkills(local: boolean): Promise<UpdateAllResult> {
const updated: string[] = []
const skipped: string[] = []
const errors: string[] = []

for (const [name, installer] of Object.entries(skillInstallers)) {
try {
const isInstalled = await installer.isInstalled(local)
if (isInstalled) {
await installer.update(local)
updated.push(name)
} else {
skipped.push(name)
}
} catch {
errors.push(name)
}
}

return { updated, skipped, errors }
}
3 changes: 3 additions & 0 deletions src/postinstall.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { updateAllInstalledSkills } from './lib/skills/update-installed.js'

updateAllInstalledSkills(false).catch(() => {})