"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.moduleInstall=moduleInstall;exports.default=void 0;var _asyncToGenerator2=_interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));var _fs=_interopRequireDefault(require("fs"));
var _path=_interopRequireDefault(require("path"));

var _extract7z=require("../extract7z");
var _temporalDir=_interopRequireDefault(require("../temporalDir"));
var _random=require("../random");

var _utils=require("@corenode/utils");

const helpers=process.runtime.helpers;
const moduleController=process.runtime.modules;function

moduleInstall(_x){return _moduleInstall.apply(this,arguments)}function _moduleInstall(){_moduleInstall=(0,_asyncToGenerator2.default)(function*(_pathFile){
return new Promise(function(){var _ref=(0,_asyncToGenerator2.default)(function*(resolve,reject){
if(typeof _pathFile=="undefined"){
const err=`_pathFile is not defined`;
return reject(new Error(err));
}
if(!_fs.default.existsSync(_pathFile)){
const err=`_pathFile not exists > [${_pathFile}]`;
return reject(new Error(err));
}

if(!_path.default.isAbsolute(_pathFile)){
_pathFile=_path.default.resolve(_pathFile);
}

const _extractDir=_temporalDir.default.createNew((0,_random.generateName)());
const _ext=_pathFile.split(".").pop();

if(_ext==="7z"){
yield(0,_extract7z.extract)(_pathFile,_extractDir);
_pathFile=_extractDir;
}

const manifestPath=_path.default.resolve(_pathFile,`./manifest.json`);

if(!_fs.default.existsSync(manifestPath)){
const err=`manifest.json not exists > [${manifestPath}]`;
return reject(new Error(err));
}

const manifest=JSON.parse(_fs.default.readFileSync(manifestPath,"utf-8"));const

pkg=manifest.pkg,file=manifest.file,codec=manifest.codec,version=manifest.version,dependencies=manifest.dependencies,setup=manifest.setup;

if(typeof pkg=="undefined"){
return reject(new Error(`Undefined [pkg]`));
}
if(typeof file=="undefined"){
return reject(new Error(`Undefined [file]`));
}

let _manifestSetup={
pkg:pkg,
version:version!==null&&version!==void 0?version:"0.0.0",
codec:codec!==null&&codec!==void 0?codec:"utf-8",
file:file!==null&&file!==void 0?file:_path.default.resolve(_pathFile,"_module.js"),
dependencies:dependencies,
setup:setup};


if(_manifestSetup.dependencies){var _params;
if(Boolean((_params=params)===null||_params===void 0?void 0:_params.ignoreDeps)){
_utils.verbosity.options({dumpFile:true}).warn(`Ignoring dependencies installation`);
}else{
(0,_utils.objectToArrayMap)(_manifestSetup.dependencies).forEach(dep=>{
const isInstalled=helpers.isDependencyInstalled(dep.key)?true:false;
if(!isInstalled){
helpers.addDependency(dep,true);
}
});
}
}

if(_manifestSetup.setup){

}

moduleController.registryKey.add(_manifestSetup.pkg,_manifestSetup.version);

return resolve(_manifestSetup);
});return function(_x2,_x3){return _ref.apply(this,arguments)}}());
});return _moduleInstall.apply(this,arguments)}var _default=

moduleInstall;exports.default=_default;