@sprucelabs/path-resolver
Advanced tools
Comparing version 1.0.7 to 1.0.8
import PathResolver, { IPathResolverOptions } from './PathResolver'; | ||
export default PathResolver; | ||
export declare function register(options: IPathResolverOptions): void; | ||
export declare function register(options?: IPathResolverOptions): void; |
@@ -9,5 +9,5 @@ "use strict"; | ||
function register(options) { | ||
new PathResolver_1.default(options); | ||
new PathResolver_1.default(options || {}); | ||
} | ||
exports.register = register; | ||
//# sourceMappingURL=index.js.map |
export interface IPathResolverOptions { | ||
/** Paths to find tsConfig. First one wins */ | ||
tsConfigDirs: string[]; | ||
/** Current working directory, i'll start here and work up directories until i find a tsconfig.json */ | ||
cwd?: string; | ||
/** Supported file extensions, defaults to everything setup in node */ | ||
@@ -5,0 +5,0 @@ extensions?: string[]; |
@@ -13,7 +13,6 @@ "use strict"; | ||
constructor(options) { | ||
var _a; | ||
this.replacePaths = {}; | ||
this.pathCache = {}; | ||
this.extensions = []; | ||
const { tsConfigDirs, | ||
const { cwd, | ||
// @ts-ignore | ||
@@ -28,24 +27,22 @@ extensions = Object.keys(coreModuleLoader._extensions) } = options; | ||
// We have to set these to at least something or ts will not pass lint | ||
this.cwd = ''; | ||
this.cwd = cwd || (process && process.cwd()) || '/'; | ||
this.compilerOptions = {}; | ||
log_1.default.info('PathResolver setup for', this.extensions); | ||
let found = false; | ||
for (const candidate of tsConfigDirs) { | ||
const tsConfigPath = path.join(candidate, 'tsconfig.json'); | ||
const pathParts = this.cwd.split(path.sep); | ||
if (pathParts[0] === '') { | ||
pathParts[0] = path.sep; | ||
} | ||
do { | ||
const tsConfigPath = path.join(...pathParts, 'tsconfig.json'); | ||
if (fs_1.default.existsSync(tsConfigPath)) { | ||
log_1.default.info(`Loading tsconfig from ${candidate}/tsconfig.json`); | ||
this.compilerOptions = require(`${candidate}/tsconfig.json`).compilerOptions; | ||
// Set to base url | ||
this.cwd = | ||
this.compilerOptions.baseUrl && | ||
this.compilerOptions.baseUrl[0] === path.sep | ||
? this.compilerOptions.baseUrl | ||
: path.join(candidate, (_a = this.compilerOptions.baseUrl) !== null && _a !== void 0 ? _a : '.'); | ||
log_1.default.info('Setting resolver cwd to', this.cwd); | ||
log_1.default.info(`Loading tsconfig from ${tsConfigPath}`); | ||
this.compilerOptions = require(tsConfigPath).compilerOptions; | ||
found = true; | ||
break; | ||
} | ||
} | ||
pathParts.pop(); | ||
} while (pathParts.length > 0 && !found); | ||
if (!found) { | ||
throw new Error(`Could not found ts config in:\n\n ${tsConfigDirs.join('\n')}`); | ||
throw new Error(`Could not found ts config in ${cwd} or anywhere upstream.`); | ||
} | ||
@@ -86,4 +83,2 @@ // Setup all replace paths based on compiler options | ||
const candidates = []; | ||
// Try relative to cwd | ||
candidates.push(path.join(this.cwd, request.replace(regex, candidatePath))); | ||
// Try relative to output dir | ||
@@ -93,2 +88,4 @@ if (typeof this.compilerOptions.outDir === 'string') { | ||
} | ||
// Try relative to cwd | ||
candidates.push(path.join(this.cwd, request.replace(regex, candidatePath))); | ||
// Does this candidate exist? if so, mutate the request so core loads it correctly | ||
@@ -95,0 +92,0 @@ foundMatch = true; |
@@ -0,1 +1,3 @@ | ||
## [1.0.8](https://github.com/sprucelabsai/path-resolver/compare/v1.0.7...v1.0.8) (2020-04-14) | ||
## [1.0.7](https://github.com/sprucelabsai/path-resolver/compare/v1.0.6...v1.0.7) (2020-04-14) | ||
@@ -2,0 +4,0 @@ |
@@ -9,3 +9,3 @@ { | ||
], | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"main": "./build/index.js", | ||
@@ -12,0 +12,0 @@ "types": "./build/index.d.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
3
15919
154