Skip to content

Commit

Permalink
feat(utils): loadLibConfig
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 b484e64 commit 139f6c4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
"description": "Utilities for working with tsconfig files",
"version": "0.0.0",
"keywords": [
"programmatic",
"tsconfig",
"tsconfig.json",
"typescript"
"typescript",
"typescript-program"
],
"license": "BSD-3-Clause",
"homepage": "https://github.com/flex-development/tsconfig-utils",
Expand Down
39 changes: 39 additions & 0 deletions src/utils/__tests__/load-lib-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* @file Unit Tests - loadLibConfig
* @module tsconfig-utils/utils/tests/unit/loadLibConfig
*/

import type { Spy } from '#tests/interfaces'
import getTsconfigJson from '#tests/utils/get-tsconfig-json'
import type { Lib } from '@flex-development/tsconfig-types'
import loadCompilerOptions from '../load-compiler-options'
import testSubject from '../load-lib-config'

vi.mock('../load-compiler-options')

describe('unit:utils/loadLibConfig', () => {
let id: string
let loadCompilerOptionsMock: Spy<typeof loadCompilerOptions>

beforeAll(() => {
id = 'tsconfig.json'
loadCompilerOptionsMock =
loadCompilerOptions as unknown as typeof loadCompilerOptionsMock
})

it('should return Lib array', () => {
// Arrange
const lib: Lib[] = getTsconfigJson(id)!.compilerOptions!.lib!

// Act + Expect
expect(testSubject(id)).to.deep.equal(lib)
})

it('should return empty array if compilerOptions.lib is NIL', () => {
// Arrange
loadCompilerOptionsMock.mockReturnValueOnce({})

// Act + Expect
expect(testSubject(id)).to.deep.equal([])
})
})
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

export { default as loadCompilerOptions } from './load-compiler-options'
export { default as loadLibConfig } from './load-lib-config'
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-lib-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file Utilities - loadLibConfig
* @module tsconfig-utils/utils/loadLibConfig
*/

import type { LoadTsconfigOptions } from '#src/interfaces'
import type { Lib } from '@flex-development/tsconfig-types'
import type { URL } from 'node:url'
import loadCompilerOptions from './load-compiler-options'

/**
* Loads [type definition configurations][1] from a [tsconfig][2] file.
*
* [1]: https://www.typescriptlang.org/tsconfig#lib
* [2]: https://www.typescriptlang.org/tsconfig
*
* @param {URL | string} id - Module id of tsconfig file
* @param {LoadTsconfigOptions} [options={}] - Tsconfig loading options
* @return {Lib[]} Type definition configurations array
*/
const loadLibConfig = (
id: URL | string,
options: LoadTsconfigOptions = {}
): Lib[] => loadCompilerOptions(id, options).lib ?? []

export default loadLibConfig

0 comments on commit 139f6c4

Please sign in to comment.