Comparing version 1.0.0-6 to 1.0.0-7
@@ -21,8 +21,8 @@ /** | ||
* @param {string} filename - path to a tsconfig.json or a .ts source file (absolute or relative to cwd) | ||
* @param {ParseOptions} options - options | ||
* @returns {Promise<ParseResult>} | ||
* @throws {ParseError} | ||
* @param {TSConfckParseOptions} options - options | ||
* @returns {Promise<TSConfckParseResult>} | ||
* @throws {TSConfckParseError} | ||
*/ | ||
declare function parse(filename: string, options?: ParseOptions): Promise<ParseResult>; | ||
interface ParseOptions { | ||
declare function parse(filename: string, options?: TSConfckParseOptions): Promise<TSConfckParseResult>; | ||
interface TSConfckParseOptions { | ||
/** | ||
@@ -35,5 +35,10 @@ * optional cache map to speed up repeated parsing with multiple files | ||
*/ | ||
cache?: Map<string, ParseResult>; | ||
cache?: Map<string, TSConfckParseResult>; | ||
/** | ||
* treat missing tsconfig as empty result instead of an error | ||
* parse resolves with { filename: 'no_tsconfig_file_found',tsconfig:{}} instead of reject with error | ||
*/ | ||
resolveWithEmptyIfConfigNotFound?: boolean; | ||
} | ||
interface ParseResult { | ||
interface TSConfckParseResult { | ||
/** | ||
@@ -50,7 +55,7 @@ * absolute path to parsed tsconfig.json | ||
*/ | ||
solution?: ParseResult; | ||
solution?: TSConfckParseResult; | ||
/** | ||
* ParseResults for all tsconfig files referenced in a solution | ||
*/ | ||
referenced?: ParseResult[]; | ||
referenced?: TSConfckParseResult[]; | ||
/** | ||
@@ -61,4 +66,15 @@ * ParseResult for all tsconfig files | ||
*/ | ||
extended?: ParseResult[]; | ||
extended?: TSConfckParseResult[]; | ||
} | ||
declare class TSConfckParseError extends Error { | ||
constructor(message: string, code: string, cause?: Error); | ||
/** | ||
* error code | ||
*/ | ||
code: string; | ||
/** | ||
* the cause of this error | ||
*/ | ||
cause: Error | undefined; | ||
} | ||
@@ -81,8 +97,8 @@ /** | ||
* @param {string} filename - path to a tsconfig.json or a .ts source file (absolute or relative to cwd) | ||
* @param {ParseNativeOptions} options - options | ||
* @returns {Promise<ParseNativeResult>} | ||
* @throws {ParseNativeError} | ||
* @param {TSConfckParseNativeOptions} options - options | ||
* @returns {Promise<TSConfckParseNativeResult>} | ||
* @throws {TSConfckParseNativeError} | ||
*/ | ||
declare function parseNative(filename: string, options?: ParseNativeOptions): Promise<ParseNativeResult>; | ||
interface ParseNativeOptions { | ||
declare function parseNative(filename: string, options?: TSConfckParseNativeOptions): Promise<TSConfckParseNativeResult>; | ||
interface TSConfckParseNativeOptions { | ||
/** | ||
@@ -95,5 +111,10 @@ * optional cache map to speed up repeated parsing with multiple files | ||
*/ | ||
cache?: Map<string, ParseNativeResult>; | ||
cache?: Map<string, TSConfckParseNativeResult>; | ||
/** | ||
* treat missing tsconfig as empty result instead of an error | ||
* parseNative resolves with { filename: 'no_tsconfig_file_found',tsconfig:{}, result: null} instead of reject with error | ||
*/ | ||
resolveWithEmptyIfConfigNotFound?: boolean; | ||
} | ||
interface ParseNativeResult { | ||
interface TSConfckParseNativeResult { | ||
/** | ||
@@ -110,7 +131,7 @@ * absolute path to parsed tsconfig.json | ||
*/ | ||
solution?: ParseNativeResult; | ||
solution?: TSConfckParseNativeResult; | ||
/** | ||
* ParseNativeResults for all tsconfig files referenced in a solution | ||
*/ | ||
referenced?: ParseNativeResult[]; | ||
referenced?: TSConfckParseNativeResult[]; | ||
/** | ||
@@ -121,3 +142,24 @@ * full output of ts.parseJsonConfigFileContent | ||
} | ||
declare class TSConfckParseNativeError extends Error { | ||
constructor(diagnostic: TSDiagnosticError, result?: any); | ||
/** | ||
* code of typescript diagnostic, prefixed with "TS " | ||
*/ | ||
code: string; | ||
/** | ||
* full ts diagnostic that caused this error | ||
*/ | ||
diagnostic: any; | ||
/** | ||
* native result if present, contains all errors in result.errors | ||
*/ | ||
result: any | undefined; | ||
} | ||
interface TSDiagnosticError { | ||
code: number; | ||
category: number; | ||
messageText: string; | ||
start?: number; | ||
} | ||
export { find, findNative, parse, parseNative, toJson }; | ||
export { TSConfckParseError, TSConfckParseNativeError, TSConfckParseNativeOptions, TSConfckParseNativeResult, TSConfckParseOptions, TSConfckParseResult, find, findNative, parse, parseNative, toJson }; |
@@ -324,3 +324,17 @@ var __defProp = Object.defineProperty; | ||
} | ||
const tsconfigFile = await resolveTSConfig(filename) || await find(filename); | ||
let tsconfigFile; | ||
if (options == null ? void 0 : options.resolveWithEmptyIfConfigNotFound) { | ||
try { | ||
tsconfigFile = await resolveTSConfig(filename) || await find(filename); | ||
} catch (e) { | ||
const notFoundResult = { | ||
filename: "no_tsconfig_file_found", | ||
tsconfig: {} | ||
}; | ||
cache == null ? void 0 : cache.set(filename, notFoundResult); | ||
return notFoundResult; | ||
} | ||
} else { | ||
tsconfigFile = await resolveTSConfig(filename) || await find(filename); | ||
} | ||
let result; | ||
@@ -352,3 +366,3 @@ if (cache == null ? void 0 : cache.has(tsconfigFile)) { | ||
} catch (e) { | ||
throw new ParseError(`parsing ${tsconfigFile} failed: ${e}`, "PARSE_FILE", e); | ||
throw new TSConfckParseError(`parsing ${tsconfigFile} failed: ${e}`, "PARSE_FILE", e); | ||
} | ||
@@ -384,3 +398,3 @@ } | ||
const circle = extended.concat({ filename: extendedTSConfigFile, tsconfig: null }).map((e) => e.filename).join(" -> "); | ||
throw new ParseError(`Circular dependency in "extends": ${circle}`, "EXTENDS_CIRCULAR"); | ||
throw new TSConfckParseError(`Circular dependency in "extends": ${circle}`, "EXTENDS_CIRCULAR"); | ||
} | ||
@@ -398,3 +412,3 @@ extended.push(await parseFile(extendedTSConfigFile, cache)); | ||
} catch (e) { | ||
throw new ParseError(`failed to resolve "extends":"${extended}" in ${from}`, "EXTENDS_RESOLVE", e); | ||
throw new TSConfckParseError(`failed to resolve "extends":"${extended}" in ${from}`, "EXTENDS_RESOLVE", e); | ||
} | ||
@@ -470,7 +484,7 @@ } | ||
} | ||
var ParseError = class extends Error { | ||
var TSConfckParseError = class extends Error { | ||
constructor(message, code, cause) { | ||
super(message); | ||
Object.setPrototypeOf(this, ParseError.prototype); | ||
this.name = ParseError.name; | ||
Object.setPrototypeOf(this, TSConfckParseError.prototype); | ||
this.name = TSConfckParseError.name; | ||
this.code = code; | ||
@@ -500,5 +514,23 @@ this.cause = cause; | ||
} | ||
let tsconfigFile = await resolveTSConfig(filename); | ||
if (!tsconfigFile) { | ||
tsconfigFile = await findNative(filename); | ||
let tsconfigFile; | ||
if (options == null ? void 0 : options.resolveWithEmptyIfConfigNotFound) { | ||
try { | ||
tsconfigFile = await resolveTSConfig(filename); | ||
if (!tsconfigFile) { | ||
tsconfigFile = await findNative(filename); | ||
} | ||
} catch (e) { | ||
const notFoundResult = { | ||
filename: "no_tsconfig_file_found", | ||
tsconfig: {}, | ||
result: null | ||
}; | ||
cache == null ? void 0 : cache.set(filename, notFoundResult); | ||
return notFoundResult; | ||
} | ||
} else { | ||
tsconfigFile = await resolveTSConfig(filename); | ||
if (!tsconfigFile) { | ||
tsconfigFile = await findNative(filename); | ||
} | ||
} | ||
@@ -526,3 +558,3 @@ let result; | ||
if (error) { | ||
throw new ParseNativeError(error, null); | ||
throw new TSConfckParseNativeError(error, null); | ||
} | ||
@@ -560,3 +592,3 @@ const host = { | ||
if (criticalError) { | ||
throw new ParseNativeError(criticalError, nativeResult); | ||
throw new TSConfckParseNativeError(criticalError, nativeResult); | ||
} | ||
@@ -619,7 +651,7 @@ } | ||
} | ||
var ParseNativeError = class extends Error { | ||
var TSConfckParseNativeError = class extends Error { | ||
constructor(diagnostic, result) { | ||
super(diagnostic.messageText); | ||
Object.setPrototypeOf(this, ParseNativeError.prototype); | ||
this.name = ParseNativeError.name; | ||
Object.setPrototypeOf(this, TSConfckParseNativeError.prototype); | ||
this.name = TSConfckParseNativeError.name; | ||
this.code = `TS ${diagnostic.code}`; | ||
@@ -631,2 +663,4 @@ this.diagnostic = diagnostic; | ||
export { | ||
TSConfckParseError, | ||
TSConfckParseNativeError, | ||
find, | ||
@@ -633,0 +667,0 @@ findNative, |
{ | ||
"name": "tsconfck", | ||
"version": "1.0.0-6", | ||
"version": "1.0.0-7", | ||
"description": "A utility to work with tsconfig.json without typescript", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -84,2 +84,16 @@ # tsconfck | ||
### error handling | ||
find and parse reject for all errors they encounter. | ||
For parse, you can choose to resolve with an empty result instead if no tsconfig file was found | ||
```js | ||
import { parse } from 'tsconfck'; | ||
const result = await parse('some/path/without/tsconfig/foo.ts', { | ||
resolveWithEmptyIfConfigNotFound: true | ||
}); | ||
// result = { filename: 'no_tsconfig_file_found',tsconfig: {} } | ||
``` | ||
### cli | ||
@@ -86,0 +100,0 @@ |
export { find } from './find.js'; | ||
export { toJson } from './to-json.js'; | ||
export { parse } from './parse.js'; | ||
export { parse, TSConfckParseOptions, TSConfckParseResult, TSConfckParseError } from './parse.js'; | ||
export { findNative } from './find-native.js'; | ||
export { parseNative } from './parse-native.js'; | ||
export { | ||
parseNative, | ||
TSConfckParseNativeOptions, | ||
TSConfckParseNativeResult, | ||
TSConfckParseNativeError | ||
} from './parse-native.js'; |
@@ -17,10 +17,10 @@ import path from 'path'; | ||
* @param {string} filename - path to a tsconfig.json or a .ts source file (absolute or relative to cwd) | ||
* @param {ParseNativeOptions} options - options | ||
* @returns {Promise<ParseNativeResult>} | ||
* @throws {ParseNativeError} | ||
* @param {TSConfckParseNativeOptions} options - options | ||
* @returns {Promise<TSConfckParseNativeResult>} | ||
* @throws {TSConfckParseNativeError} | ||
*/ | ||
export async function parseNative( | ||
filename: string, | ||
options?: ParseNativeOptions | ||
): Promise<ParseNativeResult> { | ||
options?: TSConfckParseNativeOptions | ||
): Promise<TSConfckParseNativeResult> { | ||
const cache = options?.cache; | ||
@@ -30,7 +30,27 @@ if (cache?.has(filename)) { | ||
} | ||
let tsconfigFile = await resolveTSConfig(filename); | ||
if (!tsconfigFile) { | ||
tsconfigFile = await findNative(filename); | ||
let tsconfigFile; | ||
if (options?.resolveWithEmptyIfConfigNotFound) { | ||
try { | ||
tsconfigFile = await resolveTSConfig(filename); | ||
if (!tsconfigFile) { | ||
tsconfigFile = await findNative(filename); | ||
} | ||
} catch (e) { | ||
const notFoundResult = { | ||
filename: 'no_tsconfig_file_found', | ||
tsconfig: {}, | ||
result: null | ||
}; | ||
cache?.set(filename, notFoundResult); | ||
return notFoundResult; | ||
} | ||
} else { | ||
tsconfigFile = await resolveTSConfig(filename); | ||
if (!tsconfigFile) { | ||
tsconfigFile = await findNative(filename); | ||
} | ||
} | ||
let result: ParseNativeResult; | ||
let result: TSConfckParseNativeResult; | ||
if (cache?.has(tsconfigFile)) { | ||
@@ -55,4 +75,4 @@ result = cache.get(tsconfigFile)!; | ||
ts: any, | ||
cache?: Map<string, ParseNativeResult> | ||
): Promise<ParseNativeResult> { | ||
cache?: Map<string, TSConfckParseNativeResult> | ||
): Promise<TSConfckParseNativeResult> { | ||
if (cache?.has(tsconfigFile)) { | ||
@@ -65,3 +85,3 @@ return cache.get(tsconfigFile)!; | ||
if (error) { | ||
throw new ParseNativeError(error, null); | ||
throw new TSConfckParseNativeError(error, null); | ||
} | ||
@@ -84,3 +104,3 @@ | ||
checkErrors(nativeResult); | ||
const result: ParseNativeResult = { | ||
const result: TSConfckParseNativeResult = { | ||
filename: tsconfigFile, | ||
@@ -95,5 +115,5 @@ tsconfig: result2tsconfig(nativeResult, ts), | ||
async function parseReferences( | ||
result: ParseNativeResult, | ||
result: TSConfckParseNativeResult, | ||
ts: any, | ||
cache?: Map<string, ParseNativeResult> | ||
cache?: Map<string, TSConfckParseNativeResult> | ||
) { | ||
@@ -114,3 +134,3 @@ if (!result.tsconfig.references) { | ||
* @param {nativeResult} any - native typescript parse result to check for errors | ||
* @throws {ParseNativeError} for critical error | ||
* @throws {TSConfckParseNativeError} for critical error | ||
*/ | ||
@@ -127,3 +147,3 @@ function checkErrors(nativeResult: any) { | ||
if (criticalError) { | ||
throw new ParseNativeError(criticalError, nativeResult); | ||
throw new TSConfckParseNativeError(criticalError, nativeResult); | ||
} | ||
@@ -215,3 +235,3 @@ } | ||
export interface ParseNativeOptions { | ||
export interface TSConfckParseNativeOptions { | ||
/** | ||
@@ -224,6 +244,12 @@ * optional cache map to speed up repeated parsing with multiple files | ||
*/ | ||
cache?: Map<string, ParseNativeResult>; | ||
cache?: Map<string, TSConfckParseNativeResult>; | ||
/** | ||
* treat missing tsconfig as empty result instead of an error | ||
* parseNative resolves with { filename: 'no_tsconfig_file_found',tsconfig:{}, result: null} instead of reject with error | ||
*/ | ||
resolveWithEmptyIfConfigNotFound?: boolean; | ||
} | ||
export interface ParseNativeResult { | ||
export interface TSConfckParseNativeResult { | ||
/** | ||
@@ -242,3 +268,3 @@ * absolute path to parsed tsconfig.json | ||
*/ | ||
solution?: ParseNativeResult; | ||
solution?: TSConfckParseNativeResult; | ||
@@ -248,3 +274,3 @@ /** | ||
*/ | ||
referenced?: ParseNativeResult[]; | ||
referenced?: TSConfckParseNativeResult[]; | ||
@@ -257,8 +283,8 @@ /** | ||
export class ParseNativeError extends Error { | ||
export class TSConfckParseNativeError extends Error { | ||
constructor(diagnostic: TSDiagnosticError, result?: any) { | ||
super(diagnostic.messageText); | ||
// Set the prototype explicitly. | ||
Object.setPrototypeOf(this, ParseNativeError.prototype); | ||
this.name = ParseNativeError.name; | ||
Object.setPrototypeOf(this, TSConfckParseNativeError.prototype); | ||
this.name = TSConfckParseNativeError.name; | ||
this.code = `TS ${diagnostic.code}`; | ||
@@ -265,0 +291,0 @@ this.diagnostic = diagnostic; |
@@ -18,7 +18,10 @@ import path from 'path'; | ||
* @param {string} filename - path to a tsconfig.json or a .ts source file (absolute or relative to cwd) | ||
* @param {ParseOptions} options - options | ||
* @returns {Promise<ParseResult>} | ||
* @throws {ParseError} | ||
* @param {TSConfckParseOptions} options - options | ||
* @returns {Promise<TSConfckParseResult>} | ||
* @throws {TSConfckParseError} | ||
*/ | ||
export async function parse(filename: string, options?: ParseOptions): Promise<ParseResult> { | ||
export async function parse( | ||
filename: string, | ||
options?: TSConfckParseOptions | ||
): Promise<TSConfckParseResult> { | ||
const cache = options?.cache; | ||
@@ -28,3 +31,17 @@ if (cache?.has(filename)) { | ||
} | ||
const tsconfigFile = (await resolveTSConfig(filename)) || (await find(filename)); | ||
let tsconfigFile; | ||
if (options?.resolveWithEmptyIfConfigNotFound) { | ||
try { | ||
tsconfigFile = (await resolveTSConfig(filename)) || (await find(filename)); | ||
} catch (e) { | ||
const notFoundResult = { | ||
filename: 'no_tsconfig_file_found', | ||
tsconfig: {} | ||
}; | ||
cache?.set(filename, notFoundResult); | ||
return notFoundResult; | ||
} | ||
} else { | ||
tsconfigFile = (await resolveTSConfig(filename)) || (await find(filename)); | ||
} | ||
let result; | ||
@@ -45,4 +62,4 @@ if (cache?.has(tsconfigFile)) { | ||
tsconfigFile: string, | ||
cache?: Map<string, ParseResult> | ||
): Promise<ParseResult> { | ||
cache?: Map<string, TSConfckParseResult> | ||
): Promise<TSConfckParseResult> { | ||
if (cache?.has(tsconfigFile)) { | ||
@@ -61,3 +78,3 @@ return cache.get(tsconfigFile)!; | ||
} catch (e) { | ||
throw new ParseError(`parsing ${tsconfigFile} failed: ${e}`, 'PARSE_FILE', e); | ||
throw new TSConfckParseError(`parsing ${tsconfigFile} failed: ${e}`, 'PARSE_FILE', e); | ||
} | ||
@@ -79,3 +96,6 @@ } | ||
async function parseReferences(result: ParseResult, cache?: Map<string, ParseResult>) { | ||
async function parseReferences( | ||
result: TSConfckParseResult, | ||
cache?: Map<string, TSConfckParseResult> | ||
) { | ||
if (!result.tsconfig.references) { | ||
@@ -90,3 +110,3 @@ return; | ||
async function parseExtends(result: ParseResult, cache?: Map<string, ParseResult>) { | ||
async function parseExtends(result: TSConfckParseResult, cache?: Map<string, TSConfckParseResult>) { | ||
if (!result.tsconfig.extends) { | ||
@@ -109,3 +129,6 @@ return; | ||
.join(' -> '); | ||
throw new ParseError(`Circular dependency in "extends": ${circle}`, 'EXTENDS_CIRCULAR'); | ||
throw new TSConfckParseError( | ||
`Circular dependency in "extends": ${circle}`, | ||
'EXTENDS_CIRCULAR' | ||
); | ||
} | ||
@@ -125,3 +148,3 @@ extended.push(await parseFile(extendedTSConfigFile, cache)); | ||
} catch (e) { | ||
throw new ParseError( | ||
throw new TSConfckParseError( | ||
`failed to resolve "extends":"${extended}" in ${from}`, | ||
@@ -145,3 +168,3 @@ 'EXTENDS_RESOLVE', | ||
]; | ||
function extendTSConfig(extending: ParseResult, extended: ParseResult): any { | ||
function extendTSConfig(extending: TSConfckParseResult, extended: TSConfckParseResult): any { | ||
const extendingConfig = extending.tsconfig; | ||
@@ -224,3 +247,3 @@ const extendedConfig = extended.tsconfig; | ||
export interface ParseOptions { | ||
export interface TSConfckParseOptions { | ||
/** | ||
@@ -233,6 +256,12 @@ * optional cache map to speed up repeated parsing with multiple files | ||
*/ | ||
cache?: Map<string, ParseResult>; | ||
cache?: Map<string, TSConfckParseResult>; | ||
/** | ||
* treat missing tsconfig as empty result instead of an error | ||
* parse resolves with { filename: 'no_tsconfig_file_found',tsconfig:{}} instead of reject with error | ||
*/ | ||
resolveWithEmptyIfConfigNotFound?: boolean; | ||
} | ||
export interface ParseResult { | ||
export interface TSConfckParseResult { | ||
/** | ||
@@ -251,3 +280,3 @@ * absolute path to parsed tsconfig.json | ||
*/ | ||
solution?: ParseResult; | ||
solution?: TSConfckParseResult; | ||
@@ -257,3 +286,3 @@ /** | ||
*/ | ||
referenced?: ParseResult[]; | ||
referenced?: TSConfckParseResult[]; | ||
@@ -265,11 +294,11 @@ /** | ||
*/ | ||
extended?: ParseResult[]; | ||
extended?: TSConfckParseResult[]; | ||
} | ||
export class ParseError extends Error { | ||
export class TSConfckParseError extends Error { | ||
constructor(message: string, code: string, cause?: Error) { | ||
super(message); | ||
// Set the prototype explicitly. | ||
Object.setPrototypeOf(this, ParseError.prototype); | ||
this.name = ParseError.name; | ||
Object.setPrototypeOf(this, TSConfckParseError.prototype); | ||
this.name = TSConfckParseError.name; | ||
this.code = code; | ||
@@ -276,0 +305,0 @@ this.cause = cause; |
import path from 'path'; | ||
import { promises as fs } from 'fs'; | ||
import { ParseResult } from './parse'; | ||
import { TSConfckParseResult } from './parse'; | ||
@@ -94,3 +94,3 @@ const POSIX_SEP_RE = new RegExp('\\' + path.posix.sep, 'g'); | ||
export function resolveReferencedTSConfigFiles(result: ParseResult): string[] { | ||
export function resolveReferencedTSConfigFiles(result: TSConfckParseResult): string[] { | ||
const dir = path.dirname(result.filename); | ||
@@ -103,3 +103,6 @@ return result.tsconfig.references.map((ref: { path: string }) => { | ||
export function resolveSolutionTSConfig(filename: string, result: ParseResult): ParseResult { | ||
export function resolveSolutionTSConfig( | ||
filename: string, | ||
result: TSConfckParseResult | ||
): TSConfckParseResult { | ||
if ( | ||
@@ -123,3 +126,3 @@ result.referenced && | ||
function isIncluded(filename: string, result: ParseResult): boolean { | ||
function isIncluded(filename: string, result: TSConfckParseResult): boolean { | ||
const dir = native2posix(path.dirname(result.filename)); | ||
@@ -126,0 +129,0 @@ const files = (result.tsconfig.files || []).map((file: string) => resolve2posix(dir, file)); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
185395
2539
162