Skip to content

gunshi / definition / lazy

Function: lazy()

Define a lazy command with or without definition.

Param

A command loader function that returns a command definition

Param

An optional command definition

Call Signature

ts
function lazy<A>(loader): LazyCommand<{
  args: A;
  extensions: {
  };
}, {
}>;

Define a lazy command.

Type Parameters

Type ParameterDescription
A extends ArgsAn Args type

Parameters

ParameterTypeDescription
loaderCommandLoader<{ args: A; extensions: { }; }>A command loader

Returns

LazyCommand<{ args: A; extensions: { }; }, { }>

A lazy command with loader

Example

ts
// load command with dynamic importing
const test = lazy(() => import('./commands/test'))

Call Signature

ts
function lazy<G, A, D>(loader, definition): LazyCommand<{
  args: A;
  extensions: {
  };
}, D>;

Define a lazy command with definition.

Type Parameters

Type ParameterDefault typeDescription
G extends GunshiParamsConstraintDefaultGunshiParams-
A extends ArgsExtractArgs<G>An Args type
D extends Partial<Command<{ args: A; extensions: { }; }>>Partial<Command<{ args: A; extensions: { }; }>>A partial Command definition type

Parameters

ParameterTypeDescription
loaderCommandLoader<{ args: A; extensions: { }; }>A command loader function that returns a command definition
definitionDAn optional command definition

Returns

LazyCommand<{ args: A; extensions: { }; }, D>

A lazy command that can be executed later

Example

ts
// define command without command runner
const testDefinition = define({
  name: 'test',
  description: 'Test command',
  args: {
    debug: {
      type: 'boolean',
      description: 'Enable debug mode',
      default: false
    }
  },
})

// define load command with command runner and defined command
const test = lazy((): CommandRunner<{ args: typeof testDefinition.args; extensions: {} }> => {
  return ctx => {
    if (ctx.values.debug) {
      console.debug('Debug mode is enabled');
    }
  }
}, testDefinition)

Released under the MIT License.