Skip to content

Commit

Permalink
feat(utils): loadTsconfig
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 c9cdf33 commit 7538fdc
Show file tree
Hide file tree
Showing 15 changed files with 460 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
# DIRECTORIES & FILES
**/*.snap
**/.DS_Store
__fixtures__/tsconfig.empty.json
__fixtures__/tsconfig.invalid-json.json
__tests__/report.json
coverage/
dist/
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
.prettierignore
.yarn/
Brewfile
__fixtures__/tsconfig.invalid-json.json
__tests__/report.json
coverage/
dist/
Expand Down
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@
"icon": "graphql"
},
{
"extensions": ["build.json", "typecheck.json"],
"extensions": [
"build.json",
"empty.json",
"invalid-json.json",
"invalid-schema.json",
"typecheck.json"
],
"format": "svg",
"icon": "tsconfig"
},
Expand Down
12 changes: 12 additions & 0 deletions __fixtures__/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"lib": ["es2020"],
"noEmitOnError": true,
"skipLibCheck": false,
"target": "es2020"
},
"exclude": ["**/__mocks__/**", "**/__tests__/**"],
"extends": "../tsconfig",
"include": ["../dist", "../src"],
"references": []
}
File renamed without changes.
3 changes: 3 additions & 0 deletions __fixtures__/tsconfig.invalid-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
'compilerOptions': {}
}
1 change: 1 addition & 0 deletions __fixtures__/tsconfig.invalid-schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@
"typecheck:watch": "vitest typecheck"
},
"dependencies": {
"@flex-development/errnode": "1.4.0",
"@flex-development/errnode": "1.5.0",
"@flex-development/mlly": "1.0.0-alpha.9",
"@flex-development/pathe": "1.0.3",
"@flex-development/tsconfig-types": "2.0.3",
"@flex-development/tutils": "6.0.0-alpha.10",
"merge-anything": "5.1.4",
"sort-keys": "5.0.0",
"strip-bom": "5.0.0",
"strip-json-comments": "5.0.0"
},
Expand Down Expand Up @@ -133,6 +135,7 @@
"jsonc-eslint-parser": "2.1.0",
"lint-staged": "13.1.0",
"mri": "1.2.0",
"node-fetch": "3.3.0",
"node-notifier": "10.0.1",
"prettier": "2.8.3",
"prettier-plugin-sh": "0.12.8",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*/

export * from './interfaces'
export * from './utils'
77 changes: 77 additions & 0 deletions src/utils/__snapshots__/load-tsconfig.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Vitest Snapshot v1

exports[`unit:utils/loadTsconfig > should return TSConfig object if tsconfig file is found 1`] = `
{
"compilerOptions": {
"allowJs": true,
"allowUnreachableCode": false,
"alwaysStrict": false,
"baseUrl": "..",
"checkJs": false,
"declaration": true,
"declarationMap": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"exactOptionalPropertyTypes": true,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"lib": [
"es2020",
],
"module": "esnext",
"moduleDetection": "force",
"moduleResolution": "node",
"newLine": "lf",
"noEmit": true,
"noEmitOnError": true,
"noErrorTruncation": true,
"noFallthroughCasesInSwitch": true,
"noImplicitAny": true,
"noImplicitOverride": true,
"noImplicitReturns": true,
"noUncheckedIndexedAccess": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"outDir": "../dist",
"paths": {
"#fixtures/*": [
"__fixtures__/*",
],
"#src": [
"src/index",
],
"#src/*": [
"src/*",
],
"#tests/*": [
"__tests__/*",
],
},
"preserveConstEnums": true,
"preserveSymlinks": true,
"pretty": true,
"resolveJsonModule": true,
"rootDir": "..",
"skipLibCheck": false,
"sourceMap": true,
"strict": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"target": "es2020",
"useDefineForClassFields": true,
"useUnknownInCatchVariables": true,
},
"exclude": [
"**/__mocks__/**",
"**/__tests__/**",
],
"extends": "../tsconfig",
"include": [
"../dist",
"../src",
],
"references": [],
}
`;
64 changes: 64 additions & 0 deletions src/utils/__tests__/load-tsconfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @file Unit Tests - loadTsconfig
* @module tsconfig-utils/utils/tests/unit/loadTsconfig
*/

import { ErrorCode, type NodeError } from '@flex-development/errnode'
import { pathToFileURL, type URL } from 'node:url'
import testSubject from '../load-tsconfig'

describe('unit:utils/loadTsconfig', () => {
it('should return TSConfig object if tsconfig file is found', () => {
// Arrange
const id: URL = pathToFileURL('__fixtures__/tsconfig.build.json')

// Act + Expect
expect(testSubject(id)).toMatchSnapshot()
})

it('should return empty object if tsconfig file is empty', () => {
expect(testSubject('__fixtures__/tsconfig.empty.json')).to.deep.equal({})
})

it('should return null if tsconfig file does not exist', () => {
expect(testSubject('__fixtures__/tsconfig.dev.json')).to.be.null
})

describe('throws', () => {
it('should throw if tsconfig file does not contain valid JSON', () => {
// Arrange
const code: ErrorCode = ErrorCode.ERR_OPERATION_FAILED
let error: NodeError

// Act
try {
testSubject('__fixtures__/tsconfig.invalid-json')
} catch (e: unknown) {
error = e as typeof error
}

// Expect
expect(error!).to.not.be.undefined
expect(error!).to.have.property('code').equal(code)
})

it('should throw if tsconfig file does not convert to plain object', () => {
// Arrange
const code: ErrorCode = ErrorCode.ERR_INVALID_RETURN_VALUE
const message_regex: RegExp = /plain object .+ 'parseJSON'/
let error: NodeError

// Act
try {
testSubject('__fixtures__/tsconfig.invalid-schema')
} catch (e: unknown) {
error = e as typeof error
}

// Expect
expect(error!).to.not.be.undefined
expect(error!).to.have.property('code').equal(code)
expect(error!).to.have.property('message').match(message_regex)
})
})
})
6 changes: 6 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* @file Entry Point - Utilities
* @module tsconfig-utils/utils
*/

export { default as loadTsconfig } from './load-tsconfig'
Loading

0 comments on commit 7538fdc

Please sign in to comment.