Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a new NestJS through a shcematic doesn't put it in the right folder #1658

Closed
3 of 4 tasks
Maryannah opened this issue Jul 30, 2019 · 16 comments
Closed
3 of 4 tasks
Assignees

Comments

@Maryannah
Copy link

Prerequisites

Please answer the following questions for yourself before submitting an issue.
YOU MAY DELETE THE PREREQUISITES SECTION.

  • I am running the latest version (no, 8.3.0, since the latest one doesn't work for me)
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed
  • I'm reporting the issue to the correct repository (not related to Angular, AngularCLI or any dependency)

Expected Behavior

I am using the Angular console to create a new NestJs controller. The outputted command is

ng generate @nestjs/schematics:controller mycontroller

I expect the controller to be placed inside my Nest application (apps/api)

Current Behavior

The output of the console is

CREATE src/mycontroller/mycontroller.controller.spec.ts (479 bytes)
CREATE src/mycontroller/mycontroller.controller.ts (97 bytes)

And indeed, the controller is put at the root of the projet, under a new folder src, not under apps/api.

Steps to Reproduce

  1. Create a Nx project
  2. Add a Nest projet
  3. Create a Nest controller as specified above

Context

  • version of Nx used = 8.3.0
  • version of Angular CLI used = 8.1.1
  • angular.json configuration = untouched
  • version of Angular DevKit used = 8.1.1
  • 3rd-party libraries and their versions = non installed

Other

I have tried giving a path as the optional parameter, to no success. (still in src)
I have tried giving a source root, to no success (still in src)

@johannesschobel
Copy link
Contributor

Dear @Maryannah ,

that is most likely, because nrwl/nx does not ship with a nest-cli.json file in the root folder. This file, in turn, is used by nestjs to determine the source folder to put the generated files into..

In order to fix your issue, simply create a nest-cli.json file in the root folder and add the following content:

{
  "language": "ts",
  "collection": "@nestjs/schematics",
  "sourceRoot": "apps/api/src"
}

Please adapt the sourceRoot to your specific folder-structure..
That should solve your issues...

@vsavkin or @FrozenPandaz : Maybe we can add this nest-cli.json file to the nest schematics? What do you think about this?

All the best

@METACEO
Copy link

METACEO commented Aug 11, 2019

I was about to submit a similar issue until I came across this issue by @Maryannah - I would like to extend the discussion around this issue to accommodate or consider multiple Nest apps.

In my original case, I am using the Angular Console via VSCode and when I click on "Projects", find my Nest application and click its "Generate" button, any @nestjs/schematics I go to generate (i.e. controller, module, etc.) results in something similar to the following:

ng generate @nestjs/schematics:module storage --dry-run --no-interactive
CREATE src/storage/storage.module.ts (84 bytes)
NOTE: The "dryRun" flag means no changes were made.
Process completed

When I would expect something like this when generating off a Nest project via the "Projects" page:

ng generate @nestjs/schematics:module storage --path=app --sourceRoot=apps/api/src --dry-run --no-interactive
CREATE apps/api/src/app/storage/storage.module.ts (84 bytes)
UPDATE apps/api/src/app/app.module.ts (426 bytes)
NOTE: The "dryRun" flag means no changes were made.
Process completed

@johannesschobel
Copy link
Contributor

@METACEO ,

please see my answer. This is, because NestJs requires the nest-cli.json file in the root directory of the nx-workspace (or in fact any structure) to determine where the file should be stored. Currently, the nest-cli.json file is not auto-generated when adding a nest application to nrwl/nx (this may be considered a bug!). However, as this file is not present, it will create everything in the main folder of the project.

Simply add the nest-cli.json file to the root of your nx-workspace as described above!
All the best

@Maryannah
Copy link
Author

I think the question was about handling several nest projects.

Will adding this file automatically work for 2, 3, 4 nest projects ?

@johannesschobel
Copy link
Contributor

@Maryannah : no, because you need to "hardcode" the path to the project you want to add the module / controller / service / ... to (i.e., the sourceRoot field).

However, if you specify the sourceRoot : "apps" this would work, if you then call
nest g module MyModule api/src/modules

@johannesschobel
Copy link
Contributor

further comments: i mean, the generators are fine, but after a time you don't really use them a lot i think.. i mostly create my service / controllers / modules myself.. so i guess this is not really a problem 🤔

@Maryannah
Copy link
Author

And would it be possible to do it like Angular, with a parameter to choose the app ?

And your answer is opinionated, while I agree with you, we both can't speak for every people that uses the generator, hope you understand !

@johannesschobel
Copy link
Contributor

@Maryannah of course. I believe, the best option is to auto-generate the nest-cli.json file upon creating the first nest application in a given workspace.

However, it also may be possible to add an additional parameter to the schematics cli interface to select the specific app / lib to be used when generating respective file..

@METACEO
Copy link

METACEO commented Aug 12, 2019

METACEO, please see my answer. This is, because NestJs requires the nest-cli.json file in the root directory of the nx-workspace (or in fact any structure) to determine where the file should be stored. Currently, the nest-cli.json file is not auto-generated when adding a nest application to nrwl/nx (this may be considered a bug!). However, as this file is not present, it will create everything in the main folder of the project.

Well, while I'm not here to argue if the lack of the nest-cli.json file is a bug or not, my guess is that even if one was generated, I'd still have a problem or the inconvenience of generating Nestjs schematics for a specified Nestjs project.

@kylecordes
Copy link

It would be great to have "auto-generate the nest-cli.json file upon creating the first nest application" between now and a fuller implementation.

@MSakamaki
Copy link
Contributor

MSakamaki commented Aug 23, 2019

It is more complicated than creating nest-cli.json, but if you have multiple nest applications and libs, I thought it would be better to use workspace-schematic.

Fortunately, @nest/cli has a schematic, so you can welcome it to the nrwl project by adding workspace-schematic.
This is the sample code.
https://gist.github.com/MSakamaki/6ee0a7d7c0d6878462a7661c5d848c9f

This is working without problems in my environment, and if this correspondence is not a problem, I would like to give a PR as a schematic.

@vsavkin vsavkin self-assigned this Aug 28, 2019
@vsavkin
Copy link
Member

vsavkin commented Aug 28, 2019

Folks. I looked into it. It looks like generating a nest-cli.json isn't the best option because a workspace can contain many apps and libs.

So I don't think there is anything that needs to be done on the Nx side.

Your options are:

  • ng generate @nestjs/schematics:controller mycontroller --source-root=apps/api/src
  • create a workspace schematic that invokes externalSchematic('@nestjs/schematics:controller')

We are also working on improving Angular Console, so you can right click on a folder and invoke a schematic from it. We will keep this use case in mind, such that we either create source-root or project parameter based on the selected schematic.

@kylecordes
Copy link

Has anyone tested whether the Nest CLI might notice and use a config file at the per-app level of the file layout? Our whether it could be cajoled to do so?

@MSakamaki
Copy link
Contributor

MSakamaki commented Aug 29, 2019

@kylecordes
Does the config file indicate angular.json?
If so, nest-cli doesn't seem to refer to angular.json.

If I wanted to reference the file layout of angular.json, I could solve it by bridging using the workspace schematic.

@vsavkin
Copy link
Member

vsavkin commented Sep 30, 2019

Folks. I'm going to close this issue because it should be fixed on the NestJS side.

But I'm happy to chime in if you talk about it with Kamil.

@vsavkin vsavkin closed this as completed Sep 30, 2019
@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants