Comparing version 3.0.0-next.5 to 3.0.0-next.6
{ | ||
"name": "tsconfck", | ||
"version": "3.0.0-next.5", | ||
"version": "3.0.0-next.6", | ||
"description": "A utility to work with tsconfig.json without typescript", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -51,3 +51,3 @@ /** @template T */ | ||
* @param file | ||
* @param {Promise<T>|T} result | ||
* @param {Promise<T>} result | ||
*/ | ||
@@ -73,3 +73,3 @@ setParseResult(file, result) { | ||
* @param {string} dir | ||
* @param {Promise<string|null>|string|null} tsconfigPath | ||
* @param {Promise<string|null>} tsconfigPath | ||
*/ | ||
@@ -76,0 +76,0 @@ setTSConfigPath(dir, tsconfigPath) { |
@@ -18,7 +18,3 @@ import path from 'path'; | ||
if (cache?.hasTSConfigPath(fileDir)) { | ||
const tsconfigFile = await cache.getTSConfigPath(fileDir); | ||
if (!tsconfigFile) { | ||
throw new Error(`no tsconfig file found for ${filename}`); | ||
} | ||
return tsconfigFile; | ||
return cache.getTSConfigPath(fileDir); | ||
} | ||
@@ -28,3 +24,3 @@ const ts = await loadTS(); | ||
let tsconfigFile = findConfigFile(fileDir, sys.fileExists); | ||
if (is_out_of_root(tsconfigFile, root)) { | ||
if (!tsconfigFile || is_out_of_root(tsconfigFile, root)) { | ||
tsconfigFile = null; | ||
@@ -35,5 +31,2 @@ } | ||
} | ||
if (!tsconfigFile) { | ||
throw new Error(`no tsconfig file found for ${filename}`); | ||
} | ||
return tsconfigFile; | ||
@@ -54,5 +47,5 @@ } | ||
* if no tsconfig was found, go up until root | ||
* @param {string} tsconfigFile | ||
* @param {string|null} tsconfigFile | ||
* @param {string} fileDir | ||
* @param {TSConfckCache} cache | ||
* @param {import('./cache.js').TSConfckCache} cache | ||
* @param {string} [root] | ||
@@ -73,6 +66,5 @@ */ | ||
} | ||
const p = Promise.resolve(tsconfigFile); | ||
directories.forEach((d) => { | ||
cache.setTSConfigPath(d, p); | ||
cache.setTSConfigPath(d, Promise.resolve(tsconfigFile)); | ||
}); | ||
} |
@@ -7,6 +7,12 @@ import path from 'path'; | ||
resolveSolutionTSConfig, | ||
resolveTSConfig | ||
resolveTSConfigJson | ||
} from './util.js'; | ||
import { findNative } from './find-native.js'; | ||
const notFoundResult = { | ||
tsconfigFile: null, | ||
tsconfig: {}, | ||
result: null | ||
}; | ||
/** | ||
@@ -17,3 +23,3 @@ * parse the closest tsconfig.json file with typescript native functions | ||
* | ||
* @param {string} filename - path to a tsconfig.json or a .ts source file (absolute or relative to cwd) | ||
* @param {string} filename - path to a tsconfig .json or a source file (absolute or relative to cwd) | ||
* @param {import('./public.d.ts').TSConfckParseNativeOptions} [options] - options | ||
@@ -29,24 +35,14 @@ * @returns {Promise<import('./public.d.ts').TSConfckParseNativeResult>} | ||
} | ||
let tsconfigFile; | ||
if (options?.resolveWithEmptyIfConfigNotFound) { | ||
try { | ||
tsconfigFile = await resolveTSConfig(filename, cache); | ||
if (!tsconfigFile) { | ||
tsconfigFile = await findNative(filename, options); | ||
} | ||
} catch (e) { | ||
const notFoundResult = { | ||
tsconfigFile: 'no_tsconfig_file_found', | ||
tsconfig: {}, | ||
result: null | ||
}; | ||
cache?.setParseResult(filename, Promise.resolve(notFoundResult)); | ||
return notFoundResult; | ||
} | ||
} else { | ||
tsconfigFile = await resolveTSConfig(filename, cache); | ||
if (!tsconfigFile) { | ||
tsconfigFile = await findNative(filename, options); | ||
} | ||
/** @type {(result: import('./public.d.ts').TSConfckParseNativeResult)=>void}*/ | ||
let resolveConfigPromise; | ||
/** @type {Promise<import('./public.d.ts').TSConfckParseNativeResult>}*/ | ||
const configPromise = new Promise((r) => { | ||
resolveConfigPromise = r; | ||
}); | ||
cache?.setParseResult(filename, configPromise); | ||
const tsconfigFile = | ||
(await resolveTSConfigJson(filename, cache)) || (await findNative(filename, options)); | ||
if (!tsconfigFile) { | ||
resolveConfigPromise(notFoundResult); | ||
return configPromise; | ||
} | ||
@@ -56,16 +52,13 @@ | ||
let result; | ||
if (cache?.hasParseResult(tsconfigFile)) { | ||
if (filename !== tsconfigFile && cache?.hasParseResult(tsconfigFile)) { | ||
result = await cache.getParseResult(tsconfigFile); | ||
} else { | ||
const ts = await loadTS(); | ||
result = await parseFile(tsconfigFile, ts, options); | ||
result = await parseFile(tsconfigFile, ts, options, filename === tsconfigFile); | ||
await parseReferences(result, ts, options); | ||
cache?.setParseResult(tsconfigFile, Promise.resolve(result)); | ||
} | ||
//@ts-ignore | ||
result = resolveSolutionTSConfig(filename, result); | ||
//@ts-ignore | ||
cache?.setParseResult(filename, Promise.resolve(result)); | ||
return result; | ||
resolveConfigPromise(resolveSolutionTSConfig(filename, result)); | ||
return configPromise; | ||
} | ||
@@ -78,7 +71,8 @@ | ||
* @param {import('./public.d.ts').TSConfckParseNativeOptions} [options] | ||
* @returns {Promise<import('./public.d.ts').TSConfckParseNativeResult>} | ||
* @param {boolean} [skipCache] | ||
* @returns {import('./public.d.ts').TSConfckParseNativeResult} | ||
*/ | ||
async function parseFile(tsconfigFile, ts, options) { | ||
function parseFile(tsconfigFile, ts, options, skipCache) { | ||
const cache = options?.cache; | ||
if (cache?.hasParseResult(tsconfigFile)) { | ||
if (!skipCache && cache?.hasParseResult(tsconfigFile)) { | ||
return cache.getParseResult(tsconfigFile); | ||
@@ -119,3 +113,5 @@ } | ||
}; | ||
cache?.setParseResult(tsconfigFile, Promise.resolve(result)); | ||
if (!skipCache) { | ||
cache?.setParseResult(tsconfigFile, Promise.resolve(result)); | ||
} | ||
return result; | ||
@@ -138,2 +134,3 @@ } | ||
); | ||
result.referenced.forEach((ref) => (ref.solution = result)); | ||
} | ||
@@ -140,0 +137,0 @@ |
@@ -11,3 +11,3 @@ import path from 'path'; | ||
resolveSolutionTSConfig, | ||
resolveTSConfig | ||
resolveTSConfigJson | ||
} from './util.js'; | ||
@@ -42,3 +42,4 @@ | ||
let tsconfigFile = (await resolveTSConfig(filename, cache)) || (await find(filename, options)); | ||
let tsconfigFile = | ||
(await resolveTSConfigJson(filename, cache)) || (await find(filename, options)); | ||
if (!tsconfigFile) { | ||
@@ -111,3 +112,3 @@ resolveConfigPromise(not_found_result); | ||
* @param {import('./public.d.ts').TSConfckParseResult} result | ||
* @param {TSConfckCache} [cache] | ||
* @param {import('./cache.js').TSConfckCache} [cache] | ||
* @returns {Promise<void>} | ||
@@ -122,2 +123,5 @@ */ | ||
await Promise.all(referenced.map((ref) => parseExtends(ref, cache))); | ||
referenced.forEach((ref) => { | ||
ref.solution = result; | ||
}); | ||
result.referenced = referenced; | ||
@@ -124,0 +128,0 @@ } |
@@ -34,32 +34,19 @@ import path from 'node:path'; | ||
*/ | ||
export async function resolveTSConfig(filename, cache) { | ||
export async function resolveTSConfigJson(filename, cache) { | ||
if (path.extname(filename) !== '.json') { | ||
return; | ||
return; // ignore files that are not json | ||
} | ||
const tsconfig = path.resolve(filename); | ||
if (cache) { | ||
if (cache.hasParseResult(tsconfig)) { | ||
if (cache.hasParseResult(tsconfig) || cache.hasParseResult(filename)) { | ||
return tsconfig; | ||
} | ||
if (path.basename(tsconfig) === 'tsconfig.json') { | ||
const dir = path.dirname(tsconfig); | ||
if (cache.hasTSConfigPath(dir)) { | ||
const cached = await cache.getTSConfigPath(dir); | ||
return cached === tsconfig ? tsconfig : undefined; | ||
} | ||
} | ||
} | ||
try { | ||
const stat = await fs.stat(tsconfig); | ||
return fs.stat(tsconfig).then((stat) => { | ||
if (stat.isFile() || stat.isFIFO()) { | ||
return tsconfig; | ||
} else { | ||
throw new Error(`${filename} exists but is not a regular file.`); | ||
} | ||
} catch (e) { | ||
// ignore does not exist error | ||
if (e.code !== 'ENOENT') { | ||
throw e; | ||
} | ||
} | ||
throw new Error(`no tsconfig file found for ${filename}`); | ||
}); | ||
} | ||
@@ -74,12 +61,8 @@ | ||
* | ||
* @param filename {string} filename with posix separators | ||
* @param {string} filename with posix separators | ||
* @returns {string} filename with native separators | ||
*/ | ||
export const posix2native = IS_POSIX | ||
? (s) => s | ||
: (filename) => { | ||
return filename.includes(path.posix.sep) | ||
? filename.replace(POSIX_SEP_RE, path.sep) | ||
: filename; | ||
}; | ||
? (filename) => filename | ||
: (filename) => filename.replace(POSIX_SEP_RE, path.sep); | ||
@@ -93,12 +76,8 @@ /** | ||
* | ||
* @param filename {string} filename with native separators | ||
* @param {string} filename - filename with native separators | ||
* @returns {string} filename with posix separators | ||
*/ | ||
export const native2posix = IS_POSIX | ||
? (s) => s | ||
: (filename) => { | ||
return filename.includes(path.sep) | ||
? filename.replace(NATIVE_SEP_RE, path.posix.sep) | ||
: filename; | ||
}; | ||
? (filename) => filename | ||
: (filename) => filename.replace(NATIVE_SEP_RE, path.posix.sep); | ||
@@ -114,12 +93,10 @@ /** | ||
*/ | ||
export function resolve2posix(dir, filename) { | ||
if (IS_POSIX) { | ||
return dir ? path.resolve(dir, filename) : path.resolve(filename); | ||
} | ||
return native2posix( | ||
dir | ||
? path.resolve(posix2native(dir), posix2native(filename)) | ||
: path.resolve(posix2native(filename)) | ||
); | ||
} | ||
export const resolve2posix = IS_POSIX | ||
? (dir, filename) => (dir ? path.resolve(dir, filename) : path.resolve(filename)) | ||
: (dir, filename) => | ||
native2posix( | ||
dir | ||
? path.resolve(posix2native(dir), posix2native(filename)) | ||
: path.resolve(posix2native(filename)) | ||
); | ||
@@ -154,6 +131,3 @@ /** | ||
if (solutionTSConfig) { | ||
return { | ||
...solutionTSConfig, | ||
solution: result | ||
}; | ||
return solutionTSConfig; | ||
} | ||
@@ -160,0 +134,0 @@ } |
@@ -96,3 +96,3 @@ declare module 'tsconfck' { | ||
* | ||
* @param filename - path to a tsconfig.json or a .ts source file (absolute or relative to cwd) | ||
* @param filename - path to a tsconfig .json or a source file (absolute or relative to cwd) | ||
* @param options - options | ||
@@ -99,0 +99,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
59890
1671