@@ -482,8 +482,8 @@ Sharing is either of two things, and neither of them is a bag:
482482 }
483483 ```
484484
485- - ** A whole command's precondition** — ` canExecuteCommand(name, args) ` , which
486- asks that command itself; see [ Asking another
485+ - ** A whole command's precondition** — ` CommandsService. canExecuteCommand` ,
486+ which asks that command itself; see [ Asking another
487487 command] ( #asking-another-command ) .
488488
489489### ` setup ` , when a command has one
@@ -846,21 +846,23 @@ Running a command in process
846846----------------------------
847847
848848The ` CommandsService ` contract dispatches a registered command from inside the
849- process that is already running. A class command injects it like any other
850- service; an inline handler or a key shortcut may use the ` runCommand `
851- convenience, which only resolves the contract from the current context:
849+ process that is already running. It is a service like any other, so it follows
850+ the rule every service does: ` inject() ` before the first ` await ` , the
851+ injector after it, and a key shortcut's action reaches it through the
852+ injector its context carries:
852853
853854``` ts
854- import { CommandsService } from " ../common/contracts/commands-service" ;
855- import { runCommand } from " ../common/services/command-definition-adapter" ;
855+ import { CommandsService } from " nativescript/contracts" ;
856856
857857// in a class command
858858private $commandsService = inject (CommandsService );
859859await this .$commandsService .runCommand (" autocomplete" );
860860
861- // in an inline handler or a shortcut action
862- await runCommand (" open|ios" );
863- await runCommand (" install" , [" lodash" ]);
861+ // in an inline handler, after the first await
862+ await ctx .injector .get (CommandsService ).runCommand (" install" , [" lodash" ]);
863+
864+ // in a shortcut action
865+ action : (ctx ) => ctx .injector .get (CommandsService ).runCommand (" open|ios" ),
864866```
865867
866868The command gets what a typed command line gives it, in the same order: its
@@ -887,10 +889,11 @@ declarations into it rewrites the values the host process is still running on
887889— ` open|ios ` declares ` watch: false ` , which would otherwise leave an ` ns start `
888890out of watch mode for the rest of its life.
889891
890- Which injector ` runCommand ` dispatches through follows the rule
891- ` registerCommand ` does: the injector of the current injection context, and the
892- CLI's own outside one. The pipeline itself lives on the contract, so a plugin
893- that holds an injector can call ` CommandsService.runCommand ` directly.
892+ There is deliberately no free ` runCommand() ` function: one that silently fell
893+ back to the CLI's root injector outside an injection context would dispatch
894+ through the wrong scope from exactly the places — after an ` await ` , inside a
895+ stdin handler — where the mistake is hardest to notice. The injector you hold
896+ is the one to dispatch through.
894897
895898### Asking another command
896899
@@ -903,15 +906,21 @@ given, whether or not it is registered, so `runCommand(prepareCommandDefinition)
903906runs exactly what you hold and cannot go stale the way a string can. Its first
904907name still identifies it for hooks and reporting.
905908
906- ` CommandsService.canExecuteCommand(command, args) ` — or the
907- ` canExecuteCommand ` convenience — asks a registered command whether it * could*
908- run, without running it:
909+ ` CommandsService.canExecuteCommand(command, args) ` asks a registered command
910+ whether it * could* run, without running it:
909911
910912``` ts
911- import { canExecuteCommand } from " ../common/services/command-definition-adapter" ;
913+ import { CommandsService } from " nativescript/contracts" ;
914+
915+ private $commandsService = inject (CommandsService );
912916
913917async canExecute (): Promise < boolean > {
914- if (!(await canExecuteCommand (" prepare" , [this .args [0 ]]))) {
918+ if (
919+ !(await this.$commandsService.canExecuteCommand(
920+ prepareCommandDefinition ,
921+ [this .args [0 ]],
922+ ))
923+ ) {
915924 return false ;
916925 }
917926
@@ -931,10 +940,10 @@ Pass only the arguments the child's own `arguments` policy accepts. The child
931940enforces that policy before its ` canExecute ` , so forwarding a caller's whole
932941argument list to a child that declares fewer is a rejection, not a wider check.
933942
934- ` canExecuteCommand ` is a thin call onto ` CommandsService.canExecuteCommand `
935- (which the deprecated ` canExecuteCommandInProcess ` also calls), and follows
936- ` runCommand ` in everything else: the same injector rule, the same option priming and
937- restoration .
943+ ` canExecuteCommand ` follows ` runCommand ` in everything else: the same option
944+ priming and restoration, the same routing of a parent name to its subcommand.
945+ The deprecated ` canExecuteCommandInProcess ` and ` executeCommandInProcess `
946+ call the two methods with a name .
938947
939948### Key shortcuts
940949
@@ -947,7 +956,7 @@ an `action` that runs it:
947956 key : " I" ,
948957 description : " Open project in Xcode" ,
949958 when : onPlatform (" iOS" ),
950- action : () => runCommand (" open|ios" ),
959+ action : (ctx ) => ctx . injector . get ( CommandsService ). runCommand (" open|ios" ),
951960}
952961```
953962
0 commit comments