### Install get-tsconfig via npm Source: https://github.com/privatenumber/get-tsconfig/blob/master/README.md Installs the get-tsconfig package using npm. This is the primary method for adding the library to your project. ```bash npm install get-tsconfig ``` -------------------------------- ### Get tsconfig from current directory or specified path Source: https://github.com/privatenumber/get-tsconfig/blob/master/README.md Finds and parses the nearest tsconfig.json file starting from the provided search path. It can also be used to find custom configuration files like jsconfig.json. Returns null if no config is found. ```typescript import { getTsconfig } from 'get-tsconfig' // Searches for tsconfig.json starting in the current directory console.log(getTsconfig()) // Find tsconfig.json from a TypeScript file path console.log(getTsconfig('./path/to/index.ts')) // Find tsconfig.json from a directory file path console.log(getTsconfig('./path/to/directory')) // Explicitly pass in tsconfig.json path console.log(getTsconfig('./path/to/tsconfig.json')) // Search for jsconfig.json - https://code.visualstudio.com/docs/languages/jsconfig console.log(getTsconfig('.', 'jsconfig.json')) ``` -------------------------------- ### createFileMatcher Source: https://github.com/privatenumber/get-tsconfig/blob/master/README.md Given a tsconfig.json file, returns a file-matcher function that determines whether it should apply to a file path. ```APIDOC ## createFileMatcher(tsconfig: TsconfigResult, caseSensitivePaths?: boolean) ### Description Given a `tsconfig.json` file, it returns a file-matcher function that determines whether it should apply to a file path. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **tsconfig** (`TsconfigResult`): Pass in the return value from `getTsconfig`, or a `TsconfigResult` object. - **caseSensitivePaths** (`boolean`, Optional): By default, it uses [`is-fs-case-sensitive`](https://github.com/privatenumber/is-fs-case-sensitive) to detect whether the file-system is case-sensitive. Pass in `true` to make it case-sensitive. ### Request Example ```javascript import { getTsconfig, createFileMatcher } from 'get-tsconfig' const tsconfig = getTsconfig() const fileMatcher = tsconfig && createFileMatcher(tsconfig) /* * Returns tsconfig.json if it matches the file, * undefined if not */ const configForFile = fileMatcher?.('/path/to/file.ts') // const distCode = compileTypescript({ // code: sourceCode, // tsconfig: configForFile // }) ``` ### Response #### Success Response (200) - **fileMatcher** (`(filePath: string) => TsConfigJsonResolved | undefined`): A function that takes a file path and returns the tsconfig if it matches, otherwise undefined. #### Response Example ```javascript // Example usage within the fileMatcher function: // If tsconfig.include contains '/src/**/*.ts' and filePath is '/src/main.ts', it returns the tsconfig. // If tsconfig.exclude contains '/tests/**/*.ts' and filePath is '/tests/test.ts', it returns undefined. ``` ``` -------------------------------- ### getTsconfig Source: https://github.com/privatenumber/get-tsconfig/blob/master/README.md Searches for a tsconfig file (defaults to tsconfig.json) in the specified search path and parses it. Returns null if not found, or an object containing the path and parsed TSConfig. ```APIDOC ## getTsconfig(searchPath?, configName?, cache?) ### Description Searches for a tsconfig file (defaults to `tsconfig.json`) in the `searchPath` and parses it. Returns `null` if a config file cannot be found, or an object containing the path and parsed TSConfig object if found. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **searchPath** (`string`, Optional, Default: `process.cwd()`): Accepts a path to a file or directory to search up for a `tsconfig.json` file. - **configName** (`string`, Optional, Default: `tsconfig.json`): The file name of the TypeScript config file. - **cache** (`Map`, Optional, Default: `new Map()`): Optional cache for fs operations. ### Request Example ```javascript import { getTsconfig } from 'get-tsconfig' // Searches for tsconfig.json starting in the current directory console.log(getTsconfig()) // Find tsconfig.json from a TypeScript file path console.log(getTsconfig('./path/to/index.ts')) // Find tsconfig.json from a directory file path console.log(getTsconfig('./path/to/directory')) // Explicitly pass in tsconfig.json path console.log(getTsconfig('./path/to/tsconfig.json')) // Search for jsconfig.json - https://code.visualstudio.com/docs/languages/jsconfig console.log(getTsconfig('.', 'jsconfig.json')) ``` ### Response #### Success Response (200) - **path** (`string`): The path to the tsconfig.json file. - **config** (`TsConfigJsonResolved`): The resolved tsconfig.json file. #### Response Example ```json { "path": "/path/to/your/tsconfig.json", "config": { "compilerOptions": { "target": "es2016", "module": "commonjs", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "skipLibCheck": true } } } ``` ``` -------------------------------- ### Create Paths Matcher from Tsconfig Source: https://github.com/privatenumber/get-tsconfig/blob/master/README.md Generates a function that matches import specifiers against tsconfig.json's `paths` compiler option. It returns an array of potential paths for resolution, aiding compatibility with various file systems. Requires a TsconfigResult object as input. ```typescript import { getTsconfig, createPathsMatcher } from 'get-tsconfig' const tsconfig = getTsconfig() const pathsMatcher = createPathsMatcher(tsconfig) const exampleResolver = (request: string) => { if (pathsMatcher) { const tryPaths = pathsMatcher(request) // Check if paths in `tryPaths` exist } } ``` -------------------------------- ### Parse tsconfig.json using TypeScript API Source: https://github.com/privatenumber/get-tsconfig/blob/master/README.md Demonstrates how to parse a `tsconfig.json` file programmatically using TypeScript's built-in APIs. This method involves finding, reading, and parsing the configuration file, including resolving any `extends` properties. It utilizes `findConfigFile`, `readConfigFile`, and `parseJsonConfigFileContent`. ```typescript import { sys as tsSys, findConfigFile, readConfigFile, parseJsonConfigFileContent } from 'typescript' // Find tsconfig.json file const tsconfigPath = findConfigFile(process.cwd(), tsSys.fileExists, 'tsconfig.json') // Read tsconfig.json file const tsconfigFile = readConfigFile(tsconfigPath, tsSys.readFile) // Resolve extends const parsedTsconfig = parseJsonConfigFileContent( tsconfigFile.config, tsSys, path.dirname(tsconfigPath) ) ``` -------------------------------- ### parseTsconfig Source: https://github.com/privatenumber/get-tsconfig/blob/master/README.md Parses the tsconfig file provided at the given path. Returns the parsed tsconfig as TsConfigJsonResolved. ```APIDOC ## parseTsconfig(tsconfigPath, cache?) ### Description Parse the tsconfig file provided. Used internally by `getTsconfig`. Returns the parsed tsconfig as `TsConfigJsonResolved`. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters (Function Arguments) - **tsconfigPath** (`string`): Required path to the tsconfig file. - **cache** (`Map`, Optional, Default: `new Map()`): Optional cache for fs operations. ### Request Example ```javascript import { parseTsconfig } from 'get-tsconfig' // Must pass in a path to an existing tsconfig.json file console.log(parseTsconfig('./path/to/tsconfig.custom.json')) ``` ### Response #### Success Response (200) - **config** (`TsConfigJsonResolved`): The resolved tsconfig.json file. #### Response Example ```json { "compilerOptions": { "target": "es2016", "module": "commonjs", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "skipLibCheck": true } } ``` ``` -------------------------------- ### Create a file matcher function from tsconfig Source: https://github.com/privatenumber/get-tsconfig/blob/master/README.md Generates a function that checks if a given file path is included or excluded by the provided tsconfig.json, based on its 'include', 'exclude', and 'files' properties. It can optionally respect file system case sensitivity. ```typescript import { getTsconfig, createFileMatcher } from 'get-tsconfig' const tsconfig = getTsconfig() const fileMatcher = tsconfig && createFileMatcher(tsconfig) /* * Returns tsconfig.json if it matches the file, * undefined if not */ const configForFile = fileMatcher?.('/path/to/file.ts') const distCode = compileTypescript({ code: sourceCode, tsconfig: configForFile }) ``` -------------------------------- ### Parse a specific tsconfig file Source: https://github.com/privatenumber/get-tsconfig/blob/master/README.md Parses a tsconfig.json file directly from a given file path. This function is used internally by getTsconfig and is useful when the path to the tsconfig file is already known. ```typescript import { parseTsconfig } from 'get-tsconfig' // Must pass in a path to an existing tsconfig.json file console.log(parseTsconfig('./path/to/tsconfig.custom.json')) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.