Add alias and forget commands for user-defined package aliases#23
Add alias and forget commands for user-defined package aliases#23TitasGailius wants to merge 2 commits into
Conversation
| name: 'forget', | ||
| description: 'Remove a user-defined alias', | ||
| )] | ||
| class ForgetCommand extends Command |
There was a problem hiding this comment.
I think here we should display not only the alias, but also what the alias stands for, because users can even have different aliases for the same package. for example pest3 and pest4 for the versions.
There was a problem hiding this comment.
This is a great idea. Now it displays the full package name under the highlighted alias.
|
|
||
| $json = json_decode((string) file_get_contents($file), true); | ||
|
|
||
| return new self(array_map( |
There was a problem hiding this comment.
A malformed ~/.cpx/aliases.json (invalid JSON, non-array root, or one bad value) throws and breaks every cpx command, including cpx forget, which is how a user would fix it. Let's mirror the is_array() guard already used in Application::resolveVersion(), bail to an empty state otherwise, and skip individual unparseable entries.
| return $this; | ||
| } | ||
|
|
||
| public function save(): void |
There was a problem hiding this comment.
I think this could take advantage of some things I created for this PR:
#22, check the Filesystem class
| $this->addArgument('name', InputArgument::OPTIONAL, 'The alias name to run the package as, e.g. "cpx <name>"'); | ||
| } | ||
|
|
||
| protected function execute(InputInterface $input, OutputInterface $output): int |
There was a problem hiding this comment.
Should we display a warning/confirmation when user is overriding an existing alias?
| try { | ||
| $package = $this->resolvePackage($input); | ||
| $name = $this->resolveName($input, $package); | ||
| } catch (InvalidArgumentException|NonInteractiveValidationException $e) { |
There was a problem hiding this comment.
The two commands handle errors differently: AliasCommand catches InvalidArgumentException|NonInteractiveValidationException while ForgetCommand catches only the latter, and package vs name validation take two different shapes here. Let's align the catch sets and route both args through a validate*() method so the pattern reads the same.
|
This looks like it covers the primary use case when a binary matches the package/vendor name clearly, do you think there's appetite for another question/parameter for packages that have multiple binaries when it isn't entirely clear? For example:
|
Creating aliases
Added
cpx alias [package] [name]to let users create their own shortcut for a package (e.g.cpx alias laravel/pint pint), prompting interactively via Laravel Prompts for whichever argument is omitted.Listing aliases
cpx aliasesnow also lists user-defined aliases under a "Your aliases" section alongside the built-in ones.Deleting aliases
Added
cpx forget [name]to remove a saved alias, with a select prompt listing existing aliases when the name is omitted.Other
~/.cpx/aliases.jsonand take priority over the built-in aliases at dispatch time, so a user can override e.g. pint to point elsewhere.