fix(slashcommand): deduplicate opencode command aliases
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -204,4 +204,35 @@ Use ancestor command.
|
|||||||
expect(ancestorCommand?.scope).toBe("opencode-project")
|
expect(ancestorCommand?.scope).toBe("opencode-project")
|
||||||
expect(ancestorCommand?.content).toContain("Use ancestor command.")
|
expect(ancestorCommand?.content).toContain("Use ancestor command.")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("deduplicates same-named opencode commands while keeping the higher-priority alias", () => {
|
||||||
|
const commandsRoot = join(projectDir, ".opencode")
|
||||||
|
const singularDir = join(commandsRoot, "command")
|
||||||
|
const pluralDir = join(commandsRoot, "commands")
|
||||||
|
|
||||||
|
mkdirSync(singularDir, { recursive: true })
|
||||||
|
mkdirSync(pluralDir, { recursive: true })
|
||||||
|
writeFileSync(
|
||||||
|
join(singularDir, "duplicate.md"),
|
||||||
|
`---
|
||||||
|
description: Singular duplicate command
|
||||||
|
---
|
||||||
|
Use singular command.
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
writeFileSync(
|
||||||
|
join(pluralDir, "duplicate.md"),
|
||||||
|
`---
|
||||||
|
description: Plural duplicate command
|
||||||
|
---
|
||||||
|
Use plural command.
|
||||||
|
`,
|
||||||
|
)
|
||||||
|
|
||||||
|
const commands = discoverCommandsSync(projectDir)
|
||||||
|
const duplicates = commands.filter((command) => command.name === "duplicate")
|
||||||
|
|
||||||
|
expect(duplicates).toHaveLength(1)
|
||||||
|
expect(duplicates[0]?.content).toContain("Use plural command.")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -76,6 +76,22 @@ function discoverPluginCommands(options?: CommandDiscoveryOptions): CommandInfo[
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deduplicateCommandInfosByName(commands: CommandInfo[]): CommandInfo[] {
|
||||||
|
const seen = new Set<string>()
|
||||||
|
const deduplicatedCommands: CommandInfo[] = []
|
||||||
|
|
||||||
|
for (const command of commands) {
|
||||||
|
if (seen.has(command.name)) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
seen.add(command.name)
|
||||||
|
deduplicatedCommands.push(command)
|
||||||
|
}
|
||||||
|
|
||||||
|
return deduplicatedCommands
|
||||||
|
}
|
||||||
|
|
||||||
export function discoverCommandsSync(
|
export function discoverCommandsSync(
|
||||||
directory?: string,
|
directory?: string,
|
||||||
options?: CommandDiscoveryOptions,
|
options?: CommandDiscoveryOptions,
|
||||||
@@ -110,12 +126,12 @@ export function discoverCommandsSync(
|
|||||||
scope: "builtin",
|
scope: "builtin",
|
||||||
}))
|
}))
|
||||||
|
|
||||||
return [
|
return deduplicateCommandInfosByName([
|
||||||
...projectCommands,
|
...projectCommands,
|
||||||
...userCommands,
|
...userCommands,
|
||||||
...opencodeProjectCommands,
|
...opencodeProjectCommands,
|
||||||
...opencodeGlobalCommands,
|
...opencodeGlobalCommands,
|
||||||
...builtinCommands,
|
...builtinCommands,
|
||||||
...pluginCommands,
|
...pluginCommands,
|
||||||
]
|
])
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user