Skip to content

feat(commands)!: defineCommand migration, lazy registration, in-process dispatch and command-owned key shortcuts - #6153

Draft
edusperoni wants to merge 19 commits into
mainfrom
feat/define-command-migration
Draft

edusperoni wants to merge 19 commits into
mainfrom
feat/define-command-migration

Conversation

@edusperoni

Copy link
Copy Markdown
Collaborator

Summary

Modernizes how the CLI defines, registers and dispatches its own commands, and turns the ns start key handling into a shortcut system that any command can own.

Commands

  • defineCommand for the CLI's own commands. Every built-in command is now a declarative definition: options and arguments are declared on it and validated before run, setup runs ahead of argument enforcement, and unknown options are tolerated rather than skipping validation. Class-based surfaces the migration left without callers are deprecated, not removed, since extensions may import them.
  • Lazy, typed registration. registerCommand has one shape and registers in one call, off the global binding and into the loading context's injector. Built-in commands load on first use. getInjector is getRootInjector.
  • In-process dispatch. A command can run another command in process without exiting on failure. DeferredCommandResult is a discriminated union, and the command-name types an extension needs are exported from the contracts.

Key shortcuts

  • Declarative table with a generic engine. ns start's keys become a table over a caller-supplied context; the key-command surface leaves the injector facade.
  • Registry contract. KeyShortcutRegistry is a contract with disposable registrations. Help and dispatch resolve through it, so an entry registered at runtime takes effect immediately. A defineCommand may declare the keys it answers to.
  • Restart ladder. r restarts the app of the running session without preparing, building or syncing. R prepares again first and rebuilds the native app only if the change scan says so. B always rebuilds it. ns start forwards all three to its children.
  • Hint reprint. The › press ? to list shortcuts line is repeated once a burst of syncs settles instead of scrolling away.

Fixes found along the way

  • The bundler child was never actually torn down on a restart: stop sent SIGINT without waiting, and the old child's exit handler then evicted the new child's registry entry, so each restart leaked a webpack process. Stop now awaits exit (SIGKILL fallback) and evictions are identity-checked.
  • A restart re-resolved every device on the platform, silently pulling devices the user had not picked into the session.
  • ns start surfaces failures from its spawned run children.
  • NS_NO_OPEN keeps the CLI from launching a browser where nobody is watching.

Gating

Standalone-command shortcuts (ns run, ns debug) are behind NS_COMMAND_SHORTCUTS=1 and default off. With the flag unset, ns start, its children, and the standalone commands behave as before.

Breaking change

ctx.arguments is now ctx.params on the command context (commit feat(commands)!).

Testing

npm test: 124 files, 2019 passed, 9 skipped.

Not in this PR

  • Explicit child mode for ns start children instead of TTY sniffing; children advertising their shortcuts over IPC.
  • A pinned footer for the shortcut hint.
  • Busy feedback while a key's action is still running (keys are currently dropped silently).

Options and arguments are declared on the definition and validated
before run. Setup runs ahead of argument enforcement so a definition can
derive its arguments; a redeclared CLI option keeps whatever it leaves
unspecified; unknown options are tolerated instead of skipping
validation; objectOption covers --env.* style values; a missing required
argument keeps the command's preamble in the error.
Platform validation, dynamic delegation, native-add and widget, test,
create/install/post-install-cli/help, enforced-parameter, device,
self-contained, platform, plugin and hooks, open|*, and the rest.
Class-based surfaces the migration left without callers are deprecated
rather than removed, since extensions may import them.
… type

registerCommand takes one shape and registers in one call, off the
global binding and into the loading context's injector; built-in
commands load on first use through a shared helper; getInjector becomes
getRootInjector. Package-manager commands register from their real
path, dev-post-install is reachable again, and a mistyped subcommand
shows help in the terminal.
A command can run another in process without exiting on failure; a
definition's setup state is scoped to one invocation;
DeferredCommandResult is a discriminated union; the command-name types
an extension needs are exported from the contracts.

BREAKING CHANGE: ctx.arguments is now ctx.params on the command context.
ns start's key handling becomes a declarative table over a
caller-supplied context, with state on the context and capabilities on
the injector; the key-command surface leaves the injector facade.
Failures from the spawned run children surface in the parent, and
NS_NO_OPEN keeps the CLI from launching a browser where nobody is
watching.
KeyShortcutRegistry is a contract with disposable registrations; the
engine resolves help and dispatch through it, so an entry registered at
runtime takes effect immediately. The shared entries become builders, a
defineCommand may declare the keys it answers to, and ns run and ns
debug get their own tables behind NS_COMMAND_SHORTCUTS (default off).
Stopping a bundler only sent SIGINT and returned, so a restart could spawn
a replacement while the old watcher was still alive. The stale child's exit
then evicted the replacement's map entry, and a compilation finishing on it
still reached the prepare controller.

Await the child's exit (escalating to SIGKILL), detach its output and IPC
handlers, and key every eviction on process identity. The prepare
controller now keeps its compilation handler per platform, so stopping one
platform no longer leaves the other's listener attached.
r restarts the app of the running session without preparing, building
or syncing; R prepares again first and rebuilds the native app only if
needed; B always rebuilds it. The help hint is repeated once a burst of
syncs settles, and a restart stays on the devices the session was given
instead of every device attached to the platform.
@coderabbitai

coderabbitai Bot commented Sep 14, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

A command's services type is now read off its setup function with
ReturnType instead of being declared beside it and kept in sync by hand.
…oken

The adapter now builds a child injector per invocation providing
COMMAND_CONTEXT, and runs setup, canExecute, run, postRun and shortcuts
under it. Handler signatures are unchanged; the token is the way a
service or a field initializer reaches the context without threading it
through. The provider reads the stage's own context, and nothing outside
an invocation can resolve the token.
Command(meta) returns a base class whose static definition is a real
defineCommand result: the handlers become methods, the instance is the
setup result, and the adapter still only ever sees definitions. The
definition is a static getter, so it resolves the subclass it is read
through and caches on that constructor.

Registration, the name-literal check and the extension manifest path
take either form. COMMAND_CONTEXT is promoted to nativescript/contracts,
which is what the base class reads in its field initializer.
platform|clean, update and device|*list are written as Command()
classes, with their services as inject() fields and the constructor
doing the initializeProjectData() work setup did. The per-platform
device listings stay in the object form: they are generated from one
function, which is what that form is for.
canExecute opens the invocation with the context it builds; execute and
postCommandAction reuse it, so every stage, the class instance and
COMMAND_CONTEXT hold the same object.
canExecuteCommand(name, args) resolves a registered command and primes its
options exactly as runCommand does, then returns its own canExecute verdict.
The child builds its setup from its own services, so a command can reuse
another's precondition without importing its handlers.
The commands with real internal structure - state shared between canExecute
and run, values derived once per invocation, several private steps - are
classes now, with one inject() field per dependency and the handlers as
methods. Long handlers are split into private methods along the seams that
were already there.

EmbedCommand asks prepare whether it could run instead of importing its
canExecute, which is what canExecuteCommand exists for.
A simple command is now one defineCommand call with its handlers written
inline, where ctx is typed by inference: the exported setupX/runX/canExecuteX
functions and the IXServices and XCommandContext aliases nothing else read are
gone. Handlers inject what they use at their own top, before the first await.

No command hands another a bag of services any more. injectPlatformCommandServices
and the setupX bundles are deleted; the shared platform checks take the context
and resolve through ctx.injector. A setup that survives is side-effect only -
the eager initializeProjectData that has to land ahead of the arguments policy.
Says that a handler resolves its own dependencies at its top, that services
are never bundled or shared between commands, and that setup is optional sugar
for one command. Documents canExecuteCommand as the way to reuse another
command's precondition, and sharpens which authoring form fits which command.
CommandsService is the API a command or plugin runs or consults another
command through: runCommand and canExecuteCommand take the registered
name or the definition or class it was registered from. The free helpers
stay as convenience over it; the *InProcess methods are deprecated.
A name is looked up in the registry; a definition or Command() class is
built and run as the caller holds it, registered or not, its first name
serving only hooks and reporting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant