Skip to content

Commit

Permalink
Merge pull request #1780 from ZanMinKian/feat-tsconfig
Browse files Browse the repository at this point in the history
feat: tsconfig.json by default if tsconfig.build.json not exist
  • Loading branch information
kamilmysliwiec committed Sep 12, 2022
2 parents 1aac05e + 2ec5919 commit 649ec08
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/configuration/defaults.ts
@@ -1,4 +1,5 @@
import { Configuration } from './configuration';
import { getDefaultTsconfigPath } from '../utils/get-default-tsconfig-path';

export const defaultConfiguration: Required<Configuration> = {
language: 'ts',
Expand All @@ -8,7 +9,7 @@ export const defaultConfiguration: Required<Configuration> = {
projects: {},
monorepo: false,
compilerOptions: {
tsConfigPath: 'tsconfig.build.json',
tsConfigPath: getDefaultTsconfigPath(),
webpack: false,
webpackConfigPath: 'webpack.config.js',
plugins: [],
Expand Down
11 changes: 11 additions & 0 deletions lib/utils/get-default-tsconfig-path.ts
@@ -0,0 +1,11 @@
import * as fs from 'fs';
import { join } from 'path';

const TSCONFIG_BUILD_JSON = 'tsconfig.build.json';
const TSCONFIG_JSON = 'tsconfig.json';

export function getDefaultTsconfigPath() {
return fs.existsSync(join(process.cwd(), TSCONFIG_BUILD_JSON))
? TSCONFIG_BUILD_JSON
: TSCONFIG_JSON;
}
5 changes: 3 additions & 2 deletions test/lib/configuration/nest-configuration.loader.spec.ts
@@ -1,6 +1,7 @@
import { Configuration, ConfigurationLoader } from '../../../lib/configuration';
import { NestConfigurationLoader } from '../../../lib/configuration/nest-configuration.loader';
import { Reader } from '../../../lib/readers';
import { getDefaultTsconfigPath } from '../../../lib/utils/get-default-tsconfig-path';

describe('Nest Configuration Loader', () => {
let reader: Reader;
Expand Down Expand Up @@ -48,7 +49,7 @@ describe('Nest Configuration Loader', () => {
compilerOptions: {
assets: [],
plugins: [],
tsConfigPath: 'tsconfig.build.json',
tsConfigPath: getDefaultTsconfigPath(),
webpack: false,
webpackConfigPath: 'webpack.config.js',
},
Expand All @@ -71,7 +72,7 @@ describe('Nest Configuration Loader', () => {
compilerOptions: {
assets: [],
plugins: [],
tsConfigPath: 'tsconfig.build.json',
tsConfigPath: getDefaultTsconfigPath(),
webpack: false,
webpackConfigPath: 'webpack.config.js',
},
Expand Down
24 changes: 24 additions & 0 deletions test/lib/utils/get-default-tsconfig-path.spec.ts
@@ -0,0 +1,24 @@
import * as fs from 'fs';
import { getDefaultTsconfigPath } from '../../../lib/utils/get-default-tsconfig-path';

jest.mock('fs', () => {
return {
existsSync: jest.fn(),
};
});

describe('get default tsconfig path', () => {
afterAll(() => {
jest.clearAllMocks();
});
it('should get tsconfig.json when tsconfig.build.json not exist', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(false);
const result = getDefaultTsconfigPath();
expect(result).toBe('tsconfig.json');
});
it('should get tsconfig.build.json when tsconfig.build.json exist', () => {
jest.spyOn(fs, 'existsSync').mockReturnValue(true);
const result = getDefaultTsconfigPath();
expect(result).toBe('tsconfig.build.json');
});
});

0 comments on commit 649ec08

Please sign in to comment.