Skip to content

Commit

Permalink
feat(cli): validate CLI package is installed locally
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Feb 5, 2022
1 parent 98d0bfb commit 8952149
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/cli/src/CLIHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ import { colors, ConfigurationLoader, MikroORM, Utils } from '@mikro-orm/core';
export class CLIHelper {

static async getConfiguration<D extends IDatabaseDriver = IDatabaseDriver>(validate = true, options: Partial<Options> = {}): Promise<Configuration<D>> {
const pkg = await ConfigurationLoader.getPackageConfig();
const deps = new Set([
...Object.keys(pkg.dependencies ?? {}),
...Object.keys(pkg.devDependencies ?? {}),
]);

if (!deps.has('@mikro-orm/cli') && !process.env.MIKRO_ORM_ALLOW_GLOBAL_CLI) {
throw new Error('@mikro-orm/cli needs to be installed as a local dependency!');
}

return ConfigurationLoader.getConfiguration(validate, options);
}

Expand Down
7 changes: 7 additions & 0 deletions tests/features/cli/CLIHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ describe('CLIHelper', () => {
await expect(CLIHelper.getConfiguration()).resolves.toBeInstanceOf(Configuration);
Object.keys(process.env).filter(k => k.startsWith('MIKRO_ORM_')).forEach(k => delete process.env[k]);
process.env.MIKRO_ORM_ALLOW_GLOBAL_CONTEXT = '1';
process.env.MIKRO_ORM_ALLOW_GLOBAL_CLI = '1';
});

test('disallows global install of CLI package', async () => {
delete process.env.MIKRO_ORM_ALLOW_GLOBAL_CLI;
await expect(CLIHelper.getConfiguration()).rejects.toThrowError(`@mikro-orm/cli needs to be installed as a local dependency!`);
process.env.MIKRO_ORM_ALLOW_GLOBAL_CLI = '1';
});

test('registerTsNode works with tsconfig.json with comments', async () => {
Expand Down
2 changes: 2 additions & 0 deletions tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { MetadataStorage } from '@mikro-orm/core';

process.env.MIKRO_ORM_ALLOW_GLOBAL_CONTEXT = '1';
process.env.MIKRO_ORM_ALLOW_GLOBAL_CLI = '1';

jest.restoreAllMocks();
MetadataStorage.clear();

0 comments on commit 8952149

Please sign in to comment.