From b484e64fedfa29ec64d555d26f5f7c9b8ea18fdd Mon Sep 17 00:00:00 2001 From: Lexus Drumgold Date: Sat, 4 Feb 2023 20:51:52 -0500 Subject: [PATCH] feat(utils): `loadPluginConfigs` Signed-off-by: Lexus Drumgold --- .../__tests__/load-plugin-configs.spec.ts | 39 +++++++++++++++++++ src/utils/index.ts | 3 +- src/utils/load-plugin-configs.ts | 26 +++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/utils/__tests__/load-plugin-configs.spec.ts create mode 100644 src/utils/load-plugin-configs.ts diff --git a/src/utils/__tests__/load-plugin-configs.spec.ts b/src/utils/__tests__/load-plugin-configs.spec.ts new file mode 100644 index 00000000..2388622a --- /dev/null +++ b/src/utils/__tests__/load-plugin-configs.spec.ts @@ -0,0 +1,39 @@ +/** + * @file Unit Tests - loadPluginConfigs + * @module tsconfig-utils/utils/tests/unit/loadPluginConfigs + */ + +import type { Spy } from '#tests/interfaces' +import type { Plugin } from '@flex-development/tsconfig-types' +import loadCompilerOptions from '../load-compiler-options' +import testSubject from '../load-plugin-configs' + +vi.mock('../load-compiler-options') + +describe('unit:utils/loadPluginConfigs', () => { + let id: string + let loadCompilerOptionsMock: Spy + + beforeAll(() => { + id = 'tsconfig.json' + loadCompilerOptionsMock = + loadCompilerOptions as unknown as typeof loadCompilerOptionsMock + }) + + it('should return Plugin object array', () => { + // Arrange + const plugins: Plugin[] = [{ name: 'typescript-styled-plugin' }] + loadCompilerOptionsMock.mockReturnValueOnce({ plugins }) + + // Act + Expect + expect(testSubject(id)).to.deep.equal(plugins) + }) + + it('should return empty array if compilerOptions.plugins is NIL', () => { + // Arrange + loadCompilerOptionsMock.mockReturnValueOnce({}) + + // Act + Expect + expect(testSubject(id)).to.deep.equal([]) + }) +}) diff --git a/src/utils/index.ts b/src/utils/index.ts index b51a73ea..c5ef3eb9 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -4,5 +4,6 @@ */ export { default as loadCompilerOptions } from './load-compiler-options' -export { default as loadTsconfig } from './load-tsconfig' export { default as loadPathAliases } from './load-path-aliases' +export { default as loadPluginConfigs } from './load-plugin-configs' +export { default as loadTsconfig } from './load-tsconfig' diff --git a/src/utils/load-plugin-configs.ts b/src/utils/load-plugin-configs.ts new file mode 100644 index 00000000..006d795c --- /dev/null +++ b/src/utils/load-plugin-configs.ts @@ -0,0 +1,26 @@ +/** + * @file Utilities - loadPluginConfigs + * @module tsconfig-utils/utils/loadPluginConfigs + */ + +import type { LoadTsconfigOptions } from '#src/interfaces' +import type { Plugin } from '@flex-development/tsconfig-types' +import type { URL } from 'node:url' +import loadCompilerOptions from './load-compiler-options' + +/** + * Loads [language service plugin configurations][1] from a [tsconfig][2] file. + * + * [1]: https://www.typescriptlang.org/tsconfig#plugins + * [2]: https://www.typescriptlang.org/tsconfig + * + * @param {URL | string} id - Module id of tsconfig file + * @param {LoadTsconfigOptions} [options={}] - Tsconfig loading options + * @return {Plugin[]} Language service plugin configurations array + */ +const loadPluginConfigs = ( + id: URL | string, + options: LoadTsconfigOptions = {} +): Plugin[] => loadCompilerOptions(id, options).plugins ?? [] + +export default loadPluginConfigs