Skip to content

Commit

Permalink
feat(cli): added option to generate cache via ts-node (#3796)
Browse files Browse the repository at this point in the history
[References](#3795 (reply in thread))

Allow user to generate ts cache via CLI.

Co-authored-by: Martin Adámek <banan23@gmail.com>
  • Loading branch information
kpervin and B4nan committed Dec 2, 2022
1 parent 42fa288 commit 268bd68
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 9 deletions.
21 changes: 15 additions & 6 deletions 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<T> implements CommandModule<T, CacheArgs> {

command = 'cache:generate';
describe = 'Generate metadata cache for production';
describe = 'Generate metadata cache';
builder = (args: Argv<T>) => {
args.option('ts-node', {
alias: 'ts',
type: 'boolean',
desc: `Use ts-node to generate '.ts' cache`,
});
return args as Argv<CacheArgs>;
};

/**
* @inheritDoc
*/
async handler(args: ArgumentsCamelCase) {
async handler(args: ArgumentsCamelCase<CacheArgs>) {
const config = await CLIHelper.getConfiguration();

if (!config.get('cache').enabled) {
Expand All @@ -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`));
}

}
29 changes: 26 additions & 3 deletions 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);

Expand All @@ -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);

Expand Down

0 comments on commit 268bd68

Please sign in to comment.