Skip to content

Commit

Permalink
fix(core): support comments in tsconfig.json
Browse files Browse the repository at this point in the history
Closes #730
  • Loading branch information
B4nan committed Aug 10, 2020
1 parent c017f05 commit 6506695
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Expand Up @@ -55,6 +55,7 @@
"fs-extra": "^9.0.1",
"globby": "^11.0.1",
"reflect-metadata": "^0.1.13",
"strip-json-comments": "^3.1.1",
"uuid": "^8.1.0"
},
"peerDependencies": {
Expand Down
17 changes: 11 additions & 6 deletions packages/core/src/utils/ConfigurationLoader.ts
@@ -1,5 +1,6 @@
import { pathExists } from 'fs-extra';
import path from 'path';
import { pathExists, readFile } from 'fs-extra';
import { join, isAbsolute } from 'path';
import stripJsonComments from 'strip-json-comments';
import { IDatabaseDriver } from '../drivers';
import { Configuration } from './Configuration';
import { Utils } from './Utils';
Expand Down Expand Up @@ -56,17 +57,16 @@ export class ConfigurationLoader {
return paths;
}

static async registerTsNode(configPath = 'tsconfig.json') {
const tsConfigPath = path.join(process.cwd(), configPath);
static async registerTsNode(configPath = 'tsconfig.json'): Promise<void> {
const tsConfigPath = isAbsolute(configPath) ? configPath : join(process.cwd(), configPath);

Utils.requireFrom('ts-node', tsConfigPath).register({
project: tsConfigPath,
transpileOnly: true,
});

if (await pathExists(tsConfigPath)) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const tsConfig = require(tsConfigPath);
const tsConfig = await this.getTsConfig(tsConfigPath);
/* istanbul ignore next */
const paths = tsConfig.compilerOptions?.paths;

Expand All @@ -79,6 +79,11 @@ export class ConfigurationLoader {
}
}

static async getTsConfig(tsConfigPath: string): Promise<Dictionary> {
const json = await readFile(tsConfigPath);
return JSON.parse(stripJsonComments(json.toString()));
}

}

export interface Settings {
Expand Down
7 changes: 7 additions & 0 deletions tests/cli/CLIHelper.test.ts
Expand Up @@ -88,6 +88,13 @@ describe('CLIHelper', () => {
await expect(CLIHelper.getConfiguration()).rejects.toThrowError(`MikroORM config file not found in ['./mikro-orm.config.js']`);
});

test('registerTsNode works with tsconfig.json with comments', async () => {
const requireFromMock = jest.spyOn(Utils, 'requireFrom');
requireFromMock.mockImplementation(() => ({ register: jest.fn() }));
await expect(ConfigurationLoader.registerTsNode(__dirname + '/../tsconfig.json')).resolves.toBeUndefined();
await expect(ConfigurationLoader.registerTsNode('./tests/tsconfig.json')).resolves.toBeUndefined();
});

test('gets ORM configuration [no package.json]', async () => {
const pathExistsMock = jest.spyOn(require('fs-extra'), 'pathExists');
pathExistsMock.mockImplementation(async path => (path as string).endsWith('mikro-orm.config.js'));
Expand Down
2 changes: 1 addition & 1 deletion tests/tsconfig.json
@@ -1,4 +1,4 @@
{
"extends": "../tsconfig.json",
"extends": "../tsconfig.json", /* omg */
"include": ["**/*", "../packages/*/src/**/*"]
}
6 changes: 5 additions & 1 deletion yarn.lock

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

0 comments on commit 6506695

Please sign in to comment.