Skip to content

Commit

Permalink
feat(utils): loadPluginConfigs
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Feb 5, 2023
1 parent 3dd561d commit b484e64
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/utils/__tests__/load-plugin-configs.spec.ts
Original file line number Diff line number Diff line change
@@ -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<typeof loadCompilerOptions>

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([])
})
})
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
26 changes: 26 additions & 0 deletions src/utils/load-plugin-configs.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b484e64

Please sign in to comment.