Thursday, 6 May 2021

How to get version from module

I have 2 repos and I am wanting to get the correct package.json version on repo A. My code currently returns me the version on repo B. The problem is that I am looking at the path ./package.json. How can I get the correct version?

In repo A:

@Controller('version')
export class VersionController {
  @Post()
  @HttpCode(200)
  @ApiResponse({
    status: 200,
    description: 'Success',
    type: VersionDto,
  })
  async versionHandler(): Promise<VersionDto> {
    const { version } = require('./package.json');

    try {
      return {
        version,
      };
    } catch (err) {
      throw new InternalServerErrorException(err);
    }
  }

And in repo B app.module.ts:

@Module({
  imports: [
    
    VersionModule,
  ],
  controllers: [],
})
export class AppModule {}

I call the version controller to get the version inside package.json, but unfortunately that gives me the version of repo A when instead, it is repo B's version that I want. How can that possible?

Directory Tree:

Repo A:

src/
  version/
     version.dto.ts
     version.controller.ts
     version.module.ts

Repo b:

app.module.ts
src/

My attempt at a dynamic Module. This is not working:

@Module({})
export class VersionModule {
  static register(options): DynamicModule {
    return {
      module: VersionModule,

      controllers: [VersionController],
      providers: [
        {
          provide: VersionController,
          useValue: options,
        },
      ],
    };
  }
}


from How to get version from module

No comments:

Post a Comment