nestjs / schematics

Nest architecture element generation based on Angular schematics 🎬

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error when invoking 'generate module' and there's no nestjs module to update

micalevisk opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

when using nest generate module and there's no occurrence of @Module() of some .module.ts file at the entry dir, the CLI breaks with the following error:

Error: Cannot read properties of undefined (reading 'properties')

Failed to execute command: node @nestjs/schematics:module --name=foo --no-dry-run --no-skip-import --language="ts" --source-root="src" --no-spec --no-flat --spec-file-suffix="spec"

image

Minimum reproduction code

https://gitlab.com/micalevisk/nestjs-schematics-issue-1444

Steps to reproduce

  1. git clone https://gitlab.com/micalevisk/nestjs-schematics-issue-1444.git
  2. cd nestjs-schematics-issue-1444
  3. npm ci
  4. npx nest g module foo
  5. see the error

the reason is pretty clear here:

const decoratorNodes: Node[] = this.getDecoratorMetadata(source, '@Module');
const node: Node = decoratorNodes[0];
const matchingProperties: ObjectLiteralElement[] = (
node as ObjectLiteralExpression
).properties

those lines didn't cover the scenario of decoratorNodes.length === 0

Expected behavior

The CLI should create the module without updating any other existing module, just like when there's no .module.ts file

Package version

9.2.0

NestJS version

No response

Node.js version

18

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

I tried to fix that already but didn't succeeded. I need more time to understand that metadata.manager.ts

Good catch! If you want to create a PR for this issue, that would be great! I believe adding a simple if condition should be enough(?)