From 268bd681993edd5a8fd754658a96c26d5c970350 Mon Sep 17 00:00:00 2001 From: Kristofer Pervin <7747148+kpervin@users.noreply.github.com> Date: Fri, 2 Dec 2022 12:10:44 -0400 Subject: [PATCH] feat(cli): added option to generate cache via ts-node (#3796) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [References](https://github.com/mikro-orm/mikro-orm/discussions/3795#discussioncomment-4253521) Allow user to generate ts cache via CLI. Co-authored-by: Martin Adámek --- .../cli/src/commands/GenerateCacheCommand.ts | 21 ++++++++++---- .../features/cli/GenerateCacheCommand.test.ts | 29 +++++++++++++++++-- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/commands/GenerateCacheCommand.ts b/packages/cli/src/commands/GenerateCacheCommand.ts index e967b49dea34..d59080feb67f 100644 --- a/packages/cli/src/commands/GenerateCacheCommand.ts +++ b/packages/cli/src/commands/GenerateCacheCommand.ts @@ -1,16 +1,25 @@ -import type { ArgumentsCamelCase, CommandModule } from 'yargs'; +import type { ArgumentsCamelCase, Argv, CommandModule } from 'yargs'; import { MetadataDiscovery, MetadataStorage, colors } from '@mikro-orm/core'; import { CLIHelper } from '../CLIHelper'; -export class GenerateCacheCommand implements CommandModule { +type CacheArgs = { ts?: boolean }; +export class GenerateCacheCommand implements CommandModule { command = 'cache:generate'; - describe = 'Generate metadata cache for production'; + describe = 'Generate metadata cache'; + builder = (args: Argv) => { + args.option('ts-node', { + alias: 'ts', + type: 'boolean', + desc: `Use ts-node to generate '.ts' cache`, + }); + return args as Argv; + }; /** * @inheritDoc */ - async handler(args: ArgumentsCamelCase) { + async handler(args: ArgumentsCamelCase) { const config = await CLIHelper.getConfiguration(); if (!config.get('cache').enabled) { @@ -20,9 +29,9 @@ export class GenerateCacheCommand implements CommandModule { config.set('logger', CLIHelper.dump.bind(null)); config.set('debug', true); const discovery = new MetadataDiscovery(MetadataStorage.init(), config.getDriver().getPlatform(), config); - await discovery.discover(false); + await discovery.discover(args.ts ?? false); - CLIHelper.dump(colors.green('Metadata cache was successfully generated')); + CLIHelper.dump(colors.green(`${args.ts ? 'TS' : 'JS'} metadata cache was successfully generated`)); } } diff --git a/tests/features/cli/GenerateCacheCommand.test.ts b/tests/features/cli/GenerateCacheCommand.test.ts index ca401b4f104e..4e7be7abbc04 100644 --- a/tests/features/cli/GenerateCacheCommand.test.ts +++ b/tests/features/cli/GenerateCacheCommand.test.ts @@ -1,12 +1,18 @@ import type { MetadataStorage } from '@mikro-orm/core'; import { Configuration, MetadataDiscovery } from '@mikro-orm/core'; import { CLIHelper } from '@mikro-orm/cli'; -import { GenerateCacheCommand } from '../../../packages/cli/src/commands/GenerateCacheCommand'; +import { + GenerateCacheCommand, +} from '../../../packages/cli/src/commands/GenerateCacheCommand'; (global as any).console.log = jest.fn(); const getConfigurationMock = jest.spyOn(CLIHelper, 'getConfiguration'); -getConfigurationMock.mockResolvedValue(new Configuration({ type: 'mysql', cache: { enabled: true }, getDriver: () => ({ getPlatform: jest.fn() }) } as any, false)); +getConfigurationMock.mockResolvedValue(new Configuration({ + type: 'mysql', + cache: { enabled: true }, + getDriver: () => ({ getPlatform: jest.fn() }), +} as any, false)); const discoverMock = jest.spyOn(MetadataDiscovery.prototype, 'discover'); discoverMock.mockResolvedValue({} as MetadataStorage); @@ -15,14 +21,31 @@ describe('GenerateCacheCommand', () => { test('handler', async () => { const cmd = new GenerateCacheCommand(); + const mockOption = jest.fn(); + const args = { option: mockOption }; + cmd.builder(args as any); + expect(mockOption).toBeCalledWith('ts-node', { + alias: 'ts', + type: 'boolean', + desc: `Use ts-node to generate '.ts' cache`, + }); + expect(discoverMock.mock.calls.length).toBe(0); await expect(cmd.handler({} as any)).resolves.toBeUndefined(); expect(discoverMock.mock.calls.length).toBe(1); expect(discoverMock.mock.calls[0][0]).toBe(false); + + await expect(cmd.handler({ ts: true } as any)).resolves.toBeUndefined(); + expect(discoverMock.mock.calls.length).toBe(2); + expect(discoverMock.mock.calls[1][0]).toBe(true); }); test('handler throws when cache is disabled', async () => { - getConfigurationMock.mockResolvedValue(new Configuration({ type: 'mysql', cache: { enabled: false }, getDriver: () => ({ getPlatform: jest.fn() }) } as any, false)); + getConfigurationMock.mockResolvedValue(new Configuration({ + type: 'mysql', + cache: { enabled: false }, + getDriver: () => ({ getPlatform: jest.fn() }), + } as any, false)); discoverMock.mockReset(); discoverMock.mockResolvedValue({} as MetadataStorage);