var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.isScopedPackagePath=exports.doResolvePath=exports.doResolve=void 0;var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _resolve=_interopRequireDefault(require("resolve"));
var _fs=_interopRequireDefault(require("fs"));
var _path=_interopRequireDefault(require("path"));function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly)symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable;});keys.push.apply(keys,symbols);}return keys;}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};if(i%2){ownKeys(Object(source),true).forEach(function(key){(0,_defineProperty2.default)(target,key,source[key]);});}else if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source));}else{ownKeys(Object(source)).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key));});}}return target;}
















var doResolve=function doResolve(aPath){var _options$basedir;var mandatory=arguments.length>1&&arguments[1]!==undefined?arguments[1]:true;var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};
options.basedir=(_options$basedir=options.basedir)!=null?_options$basedir:process.cwd();
try{
if(aPath.startsWith('file:')){
return _doResolveFSPath(aPath,options);
}
return _doResolveExternalPackage(aPath,options);
}catch(err){

if(mandatory)throw err;
}
};exports.doResolve=doResolve;

var doResolvePath=function doResolvePath(aPath){var _options$basedir2;var mandatory=arguments.length>1&&arguments[1]!==undefined?arguments[1]:true;var options=arguments.length>2&&arguments[2]!==undefined?arguments[2]:{};var fallbackBase=arguments.length>3&&arguments[3]!==undefined?arguments[3]:'';
options.basedir=(_options$basedir2=options.basedir)!=null?_options$basedir2:process.cwd();

try{
var pathArr=aPath.split('/');

if(pathArr[0]===''){
pathArr.shift();
}
if(pathArr[0]==='node_modules'){
pathArr.shift();
}
if(pathArr[0]==='packages'){
pathArr.shift();
}
var cleanPath=pathArr.join('/');
if(pathArr[0].startsWith('@')){
pathArr.shift();
}
pathArr.shift();
var realPath=doResolve(cleanPath,mandatory,options);
if(realPath){
return _path.default.join.apply(_path.default,[realPath].concat((0,_toConsumableArray2.default)(pathArr)));
}
return _path.default.join(fallbackBase,aPath);
}catch(err){
if(mandatory)throw err;
}
};exports.doResolvePath=doResolvePath;

var isScopedPackagePath=function isScopedPackagePath(aPath){
if(aPath.startsWith('@')){
if(!aPath.includes('/')){
throw new Error("Scoped packages must include subpackage portion e.g. '@aScope/subpackage'. Supplied path: "+
aPath);

}
return true;
}
};exports.isScopedPackagePath=isScopedPackagePath;

var _getPackagePathParts=function _getPackagePathParts(aPath){
var parts=[];
if(isScopedPackagePath(aPath)){
parts=aPath.match(/^([^/]+\/[^/]+)(?:\/?(.*))/);
}else{
parts=aPath.match(/^([^/]+)\/?(.*)/);
}
if(!Array.isArray(parts)){
throw new Error("Unsuitable path for resolving: "+aPath);
}
return parts.slice(1);
};




var _doResolveFSPath=function _doResolveFSPath(aPath,options){
var fileRelPath=""+(
options.basedir?(options.basedir+"/").replace(/.*\/+$/,'/'):'')+
aPath.replace('file:','');
if(!_fs.default.existsSync(fileRelPath)){
throw new Error("Explicit filepath "+
aPath+" does not resolve to dir or file");

}
return fileRelPath;
};




var _doResolveExternalPackage=function _doResolveExternalPackage(aPath,options){var _getPackagePathParts2=
_getPackagePathParts(aPath),_getPackagePathParts3=(0,_slicedToArray2.default)(_getPackagePathParts2,2),packageBase=_getPackagePathParts3[0],packageSuffix=_getPackagePathParts3[1];

try{var _options$extensions,_options$keepSuffix;
var resolvedPath=_resolve.default.
sync(packageBase,_objectSpread(_objectSpread({
packageFilter:function packageFilter(pkg){
pkg.main='package.json';
return pkg;
}},
options),{},{
extensions:['.js','.json'].concat((_options$extensions=options.extensions)!=null?_options$extensions:[])})).

replace(/\/package.json$/,'');
return((_options$keepSuffix=options.keepSuffix)!=null?_options$keepSuffix:false)?
resolvedPath+"/"+packageSuffix:
resolvedPath;
}catch(e){
return null;
}
};
//# sourceMappingURL=resolve.js.map