Skip to content

Commit

Permalink
feat(cli): allow the use of TS path mapping (#554)
Browse files Browse the repository at this point in the history
This enables the use of path aliases in your entities and ORM configuration file.

Example `tsconfig.json`:

```json
{
	"compilerOptions": {
		"paths": {
			"@foo": ["src/foo/index.ts"]
		}
	},
}
```
  • Loading branch information
AlexAegis authored and B4nan committed May 21, 2020
1 parent f21d70f commit 87ed392
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 18 deletions.
1 change: 1 addition & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"cli-highlight": "^2.1.4",
"cli-table3": "^0.6.0",
"fs-extra": "^9.0.0",
"tsconfig-paths": "^3.9.0",
"yargonaut": "^1.1.4",
"yargs": "^15.3.1"
}
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/src/CLIHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ export class CLIHelper {
const settings = await ConfigurationLoader.getSettings();

if (settings.useTsNode) {
require('ts-node').register({
project: settings.tsConfigPath,
});
await ConfigurationLoader.registerTsNode(settings.tsConfigPath);
}

// noinspection HtmlDeprecatedTag
Expand Down
21 changes: 21 additions & 0 deletions packages/core/src/utils/ConfigurationLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,27 @@ export class ConfigurationLoader {
return paths;
}

static async registerTsNode(tsConfigPath = process.cwd() + '/tsconfig.json') {
require('ts-node').register({
project: tsConfigPath,
});

if (await pathExists(tsConfigPath)) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const tsConfig = require(tsConfigPath);

/* istanbul ignore next */
const paths = tsConfig?.compilerOptions?.paths;

if (paths) {
require('tsconfig-paths').register({
baseUrl: tsConfig.compilerOptions.baseUrl,
paths: tsConfig.compilerOptions.paths,
});
}
}
}

}

export interface Settings {
Expand Down
50 changes: 35 additions & 15 deletions tests/cli/CLIHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ jest.mock('../../tests/mikro-orm.config.js', () => ({ type: 'mongo', dbName: 'fo
jest.mock('../../tests/mikro-orm.config.ts', () => ({ type: 'mongo', dbName: 'foo_bar', entitiesDirs: ['.'] }), { virtual: true });
const pkg = { 'mikro-orm': {} } as any;
jest.mock('../../tests/package.json', () => pkg, { virtual: true });
const tsc = { compilerOptions: {} } as any;
jest.mock('../../tests/tsconfig.json', () => tsc, { virtual: true });
const cwd = process.cwd;
(global as any).process.cwd = () => '../../../../tests';
const log = jest.fn();
Expand Down Expand Up @@ -38,28 +40,46 @@ describe('CLIHelper', () => {
});

test('configures yargs instance [ts-node]', async () => {
const pathExistsMock = jest.spyOn(require('fs-extra'), 'pathExists');
pathExistsMock.mockImplementation(path => path === '../../../../tests/package.json');
pkg['mikro-orm'].useTsNode = true;
const tsNodeMock = jest.spyOn(require('ts-node'), 'register');
tsNodeMock.mockImplementation(i => i);
const cli = await CLIHelper.configure() as any;
expect(cli.$0).toBe('mikro-orm');
expect(tsNodeMock).toHaveBeenCalled();
pathExistsMock.mockRestore();
});

test('configures yargs instance [ts-node] without paths', async () => {
const pathExistsMock = jest.spyOn(require('fs-extra'), 'pathExists');
pathExistsMock.mockResolvedValue(true);
pkg['mikro-orm'].useTsNode = true;
delete tsc.compilerOptions.paths;
const tsNodeMock = jest.spyOn(require('ts-node'), 'register');
tsNodeMock.mockImplementation(i => i);
const tsPathsMock = jest.spyOn(require('tsconfig-paths'), 'register');
tsPathsMock.mockImplementation(i => i);
const cli = await CLIHelper.configure() as any;
expect(cli.$0).toBe('mikro-orm');
expect(tsNodeMock).toHaveBeenCalled();
expect(cli.getCommandInstance().getCommands()).toEqual([
'cache:clear',
'cache:generate',
'generate-entities',
'database:import',
'schema:create',
'schema:drop',
'schema:update',
'migration:create',
'migration:up',
'migration:down',
'migration:list',
'migration:pending',
'debug',
]);
expect(tsPathsMock).not.toHaveBeenCalled();
pathExistsMock.mockRestore();
});

test('configures yargs instance [ts-node and ts-paths]', async () => {
const pathExistsMock = jest.spyOn(require('fs-extra'), 'pathExists');
pathExistsMock.mockResolvedValue(true);
pkg['mikro-orm'].useTsNode = true;
tsc.compilerOptions.paths = { alternativePath: ['alternativePath'] };
const tsNodeMock = jest.spyOn(require('ts-node'), 'register');
tsNodeMock.mockImplementation(i => i);
const tsPathsMock = jest.spyOn(require('tsconfig-paths'), 'register');
tsPathsMock.mockImplementation(i => i);
const cli = await CLIHelper.configure() as any;
expect(cli.$0).toBe('mikro-orm');
expect(tsNodeMock).toHaveBeenCalled();
expect(tsPathsMock).toHaveBeenCalled();
pathExistsMock.mockRestore();
});

Expand Down
22 changes: 22 additions & 0 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 87ed392

Please sign in to comment.