Skip to content

Commit

Permalink
feat(internal): readFile
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 3, 2023
1 parent 15addba commit 8836dfa
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/internal/__tests__/read-file.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file Unit Tests - readFile
* @module tsconfig-utils/internal/tests/unit/readFile
*/

import pathe from '@flex-development/pathe'
import fs from 'node:fs'
import { URL, pathToFileURL } from 'node:url'
import testSubject from '../read-file'

describe('unit:internal/readFile', () => {
it('should return file content', () => {
// Arrange
const id: URL = new URL('tsconfig.json', pathToFileURL('.' + pathe.sep))

// Act + Expect
expect(testSubject(id)).to.equal(fs.readFileSync(id, 'utf8'))
})
})
1 change: 1 addition & 0 deletions src/internal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

export { default as isDirectory } from './is-directory'
export { default as isFile } from './is-file'
export { default as readFile } from './read-file'
export { default as validateFunction } from './validate-function'
export { default as validateString } from './validate-string'
export { default as validateURLString } from './validate-url-string'
24 changes: 24 additions & 0 deletions src/internal/read-file.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @file Internal - readFile
* @module tsconfig-utils/internal/readFile
*/

import type { NodeError } from '@flex-development/errnode'
import fs from 'node:fs'
import type { URL } from 'node:url'
import validateURLString from './validate-url-string'

/**
* Reads the file at the given module `id`.
*
* @param {URL | string} id - Module id to evaluate
* @return {string} File content at `id`
* @throws {NodeError<Error | TypeError>} If `id` is not a string or an instance
* of {@linkcode URL}, or if error occurs while reading file
*/
const readFile = (id: URL | string): string => {
validateURLString(id, 'id')
return fs.readFileSync(id, 'utf8')
}

export default readFile

0 comments on commit 8836dfa

Please sign in to comment.