Comparing version 3.0.0-next.6 to 3.0.0-next.7
{ | ||
"name": "tsconfck", | ||
"version": "3.0.0-next.6", | ||
"version": "3.0.0-next.7", | ||
"description": "A utility to work with tsconfig.json without typescript", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -63,2 +63,15 @@ # tsconfck | ||
### finding tsconfig.json files inside node_modules | ||
By default, tsconfck ignores tsconfig.json files inside node_modules, similar to esbuild. | ||
If you want to include tsconfig.json files inside node_modules in your find/parse results, set `scanNodeModules` | ||
```js | ||
import { find } from 'tsconfck'; | ||
// does not return some-lib/tsconfig.json but first tsconfig outside of node_modules | ||
const fooTSConfig = await find('node_modules/some-lib/src/foo.ts'); | ||
// returns some-lib/tsconfig.json if it exists otherwise continues finding up the tree | ||
const fooResult = await find('node_modules/some-lib/src/foo.ts', { scanNodeModules: true }); | ||
``` | ||
### caching | ||
@@ -111,12 +124,20 @@ | ||
find and parse reject for all errors they encounter. | ||
find and parse reject for errors they encounter, but return null or empty result if no config was found | ||
For parse, you can choose to resolve with an empty result instead if no tsconfig file was found | ||
If you want them to error instead, test the result and throw | ||
```js | ||
import { parse } from 'tsconfck'; | ||
const result = await parse('some/path/without/tsconfig/foo.ts', { | ||
resolveWithEmptyIfConfigNotFound: true | ||
find('some/path/without/tsconfig/foo.ts').then((result) => { | ||
if (result === null) { | ||
throw new Error('not found'); | ||
} | ||
return result; | ||
}); | ||
// result = { tsconfigFile: 'no_tsconfig_file_found',tsconfig: {} } | ||
parse('some/path/without/tsconfig/foo.ts').then((result) => { | ||
if (result.tsconfigFile === null) { | ||
throw new Error('not found'); | ||
} | ||
return result; | ||
}); | ||
``` | ||
@@ -123,0 +144,0 @@ |
@@ -1,2 +0,2 @@ | ||
import path from 'path'; | ||
import path from 'node:path'; | ||
import { readdir } from 'node:fs'; | ||
@@ -3,0 +3,0 @@ |
@@ -1,3 +0,3 @@ | ||
import path from 'path'; | ||
import { loadTS } from './util.js'; | ||
import path from 'node:path'; | ||
import { loadTS, stripNodeModules } from './util.js'; | ||
@@ -14,11 +14,14 @@ /** | ||
export async function findNative(filename, options) { | ||
const fileDir = path.dirname(path.resolve(filename)); | ||
let dir = path.dirname(path.resolve(filename)); | ||
if (!options?.scanNodeModules) { | ||
dir = stripNodeModules(dir); | ||
} | ||
const cache = options?.cache; | ||
const root = options?.root ? path.resolve(options.root) : undefined; | ||
if (cache?.hasTSConfigPath(fileDir)) { | ||
return cache.getTSConfigPath(fileDir); | ||
if (cache?.hasTSConfigPath(dir)) { | ||
return cache.getTSConfigPath(dir); | ||
} | ||
const ts = await loadTS(); | ||
const { findConfigFile, sys } = ts; | ||
let tsconfigFile = findConfigFile(fileDir, sys.fileExists); | ||
let tsconfigFile = findConfigFile(dir, sys.fileExists); | ||
if (!tsconfigFile || is_out_of_root(tsconfigFile, root)) { | ||
@@ -28,3 +31,3 @@ tsconfigFile = null; | ||
if (cache) { | ||
cache_result(tsconfigFile, fileDir, cache, root); | ||
cache_result(tsconfigFile, dir, cache, root); | ||
} | ||
@@ -31,0 +34,0 @@ return tsconfigFile; |
import path from 'node:path'; | ||
import fs from 'node:fs'; | ||
import { stripNodeModules } from './util.js'; | ||
/** | ||
@@ -13,2 +14,5 @@ * find the closest tsconfig.json file | ||
let dir = path.dirname(path.resolve(filename)); | ||
if (!options?.scanNodeModules) { | ||
dir = stripNodeModules(dir); | ||
} | ||
if (cache?.hasTSConfigPath(dir)) { | ||
@@ -15,0 +19,0 @@ return cache.getTSConfigPath(dir); |
@@ -1,2 +0,2 @@ | ||
import path from 'path'; | ||
import path from 'node:path'; | ||
import { | ||
@@ -3,0 +3,0 @@ loadTS, |
@@ -1,2 +0,2 @@ | ||
import path from 'path'; | ||
import path from 'node:path'; | ||
import { promises as fs } from 'node:fs'; | ||
@@ -3,0 +3,0 @@ import { createRequire } from 'module'; |
@@ -17,2 +17,9 @@ import { TSConfckCache } from './cache.js'; | ||
root?: string; | ||
/** | ||
* set to true if you want to find tsconfig.json files inside node_modules | ||
* | ||
* @default false | ||
*/ | ||
scanNodeModules?: boolean; | ||
} | ||
@@ -19,0 +26,0 @@ |
@@ -54,2 +54,15 @@ import path from 'node:path'; | ||
/** | ||
* remove path segments inside node_modules | ||
* | ||
* @param {string} dir an absolute directory path | ||
* @returns {string} parent dir of node_modules if inside of node_modules, dir if not | ||
*/ | ||
export const stripNodeModules = IS_POSIX | ||
? (dir) => { | ||
const i = dir.indexOf('/node_modules/'); | ||
return i > -1 ? dir.slice(0, i) : dir; | ||
} | ||
: (dir) => dir.replace(/[/\\]node_modules[/\\].*$/, ''); | ||
/** | ||
* convert posix separator to native separator | ||
@@ -56,0 +69,0 @@ * |
@@ -139,2 +139,9 @@ declare module 'tsconfck' { | ||
root?: string; | ||
/** | ||
* set to true if you want to find tsconfig.json files inside node_modules | ||
* | ||
* @default false | ||
*/ | ||
scanNodeModules?: boolean; | ||
} | ||
@@ -141,0 +148,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61540
1702
222