Nest: A way to create own console commands

Created on 15 Dec 2017  路  18Comments  路  Source: nestjs/nest

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Doesn't have

Expected behavior

For example nest run create-user --username "John Doe" --email "[email protected]" creates new user in db

What is the motivation / use case for changing the behavior?

It is convenient to have this to create new data, change settings etc.
Also it will be nice if we can send commands to server instance. In case we use websockets we can update page info instantly.

Frameworks examples:
https://laravel.com/docs/5.5/artisan#writing-commands
https://symfony.com/doc/current/console.html
Not sure about this https://projects.spring.io/spring-shell/ but..

question 馃檶

Most helpful comment

I thought about something like that:

@Module({console: [UserCommands]}) class aModule {}
//...
@Commands()
class UserCommands {

  @Command('create-user')
  createUser(@arg('name') name: string, @arg('email') email: string) {
    // Do your stuff
  }
}

All 18 comments

Hi @FriOne,
Hmm. I think the NestApplicationContext instance should fit your requirements. I'll update the docs as soon as possible and let you know when it's available already so you can verify whether it helps somehow. 馃檪

You could also just cUrl against your own API, or mock requests with Postman.

@wbhob yeah, but it would be nice to have "from the box" and not to write long commands to use. Also API is available from outside but commands not always should be.
@kamilmysliwiec thanks!

I thought about something like that:

@Module({console: [UserCommands]}) class aModule {}
//...
@Commands()
class UserCommands {

  @Command('create-user')
  createUser(@arg('name') name: string, @arg('email') email: string) {
    // Do your stuff
  }
}

We're actually working on a CLI framework like this at github.com/orbital-js/orbital. Work in progress, but that's the general API. This isn't really functionality that would/should be in Nest because it's just for debugging.

Well, I just want to have possibility to use nest components in a CLI command.

I tried using the NestApplicationContext (for the same purpose of utilizing it in a CLI framework). However, I can't get an instance of my components (it just returns null). I've tried the following:

import { NestFactory } from "@nestjs/core";
import { ApplicationModule } from "./app";
import { FooComponent } from "./modules/foo/foo.component";

NestFactory.createApplicationContext(ApplicationModule)
  .then(cxt => {
    const foo = cxt.get<FooComponent>(FooComponent);
    console.log(foo); // null
  })
  .catch(err => console.error(err));

// also tried it w/ a provider and a token
NestFactory.createApplicationContext(ApplicationModule)
  .then(cxt => {
    const foo = cxt.get<FooComponent>("FooToken");
    console.log(foo); // null
  })
  .catch(err => console.error(err));

Should this be working or am I missing a step?

EDIT

If I first select the module I want to use then get the component from it, it works:

import { NestFactory } from "@nestjs/core";
import { FooModule } from "../../src/modules/foo/foo.module";
import { FooComponent } from "../../src/modules/foo/foo.component";
import { ApplicationModule } from "../../src/app.module";

(async () => {
  const context = await NestFactory.createApplicationContext(ApplicationModule);
  const module = await context.select<FooModule>(FooModule);
  const foo = module.get<FooComponent>(FooComponent);

  console.log(foo.sayHi());
})();

nice ;)

Hey,
Here's a small overview https://docs.nestjs.com/execution-context
@chrissm79 you need to go through entire modules hierarchy

@kamilmysliwiec Nice! Guess the docs site has issue with angular service worker. Docs are not updating, need to unregister it by hand.

I thought about something like that:

@Module({console: [UserCommands]}) class aModule {}
//...
@Commands()
class UserCommands {

  @Command('create-user')
  createUser(@arg('name') name: string, @arg('email') email: string) {
    // Do your stuff
  }
}

Is there any work/plan for such way? whether to expect it?

Currently, what the best way to create a console app with several commands/arguments? Where get the arguments?

Thank you!

Have you checked this module https://www.npmjs.com/package/nestjs-command @aleksanderd?

Can't make the lib work :/
I'd be happy to implement @aleksanderd idea if it is ok with you @kamilmysliwiec :)

@Samox: I have reimplemented the referenced library using ts-command-line as the backbone over yargs but run into a broader issue of being completely unable to use the invoked commands for their most obvious purpose: overriding configuration before modules are initialized. Does anyone have a proposed solution for this? Some way to defer loading of modules until the action has had a chance to resolve?

I made an other one. Using commander
https://github.com/Pop-Code/nestjs-console

Hey,
Here's a small overview https://docs.nestjs.com/execution-context
@chrissm79 you need to go through entire modules hierarchy

In case anyone is looking, this link has changed to: https://docs.nestjs.com/application-context

@Rmannn your console cli module is awesome! So easy, so nice, I'm going to be using nest even more now-

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rafal-rudnicki picture rafal-rudnicki  路  3Comments

VRspace4 picture VRspace4  路  3Comments

anyx picture anyx  路  3Comments

cdiaz picture cdiaz  路  3Comments

menme95 picture menme95  路  3Comments