"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.getVersion=getVersion;exports.getProjectEnv=getProjectEnv;exports.getGit=getGit;exports.getPackages=getPackages;exports.getInstalledDependencies=getInstalledDependencies;exports.getRootPackage=getRootPackage;exports.isCorenodeProject=isCorenodeProject;exports.isDevMode=isDevMode;exports.isProjectMode=isProjectMode;exports.modifyRuntimeEnv=modifyRuntimeEnv;exports.isDependencyInstalled=isDependencyInstalled;exports.addDependency=addDependency;exports.bumpVersion=bumpVersion;exports.syncPackageVersionFromName=syncPackageVersionFromName;exports.syncAllPackagesVersions=syncAllPackagesVersions;var _path=_interopRequireDefault(require("path"));
var _process=_interopRequireDefault(require("process"));
var _fs=_interopRequireDefault(require("fs"));let _require=

require("@corenode/utils"),schemizedStringify=_require.schemizedStringify,schemizedParse=_require.schemizedParse,verbosity=_require.verbosity,readRootDirectorySync=_require.readRootDirectorySync;
verbosity=verbosity.options({method:"[RUNTIME]"});








function getVersion(opts){
let version="0.0.0";

const projectRuntime=global._env;
const enginePkgPath=global._packages._engine;
const projectPkgPath=global._packages._project;

try{
const pkgEngine=_fs.default.existsSync(enginePkgPath)?require(enginePkgPath):{};
const pkgProject=_fs.default.existsSync(projectPkgPath)?require(projectPkgPath):{};

if(opts!==null&&opts!==void 0&&opts.engine){var _pkgEngine$version;
version=(_pkgEngine$version=pkgEngine["version"])!==null&&_pkgEngine$version!==void 0?_pkgEngine$version:"0.0.0";
}else{
if(projectRuntime.version){
version=projectRuntime.version;
}else if(typeof pkgProject["version"]!=="undefined"){
version=pkgProject["version"];
}
}
}catch(error){

}
return version;
}






function getProjectEnv(){
return global._env;
}






function getGit(){
const envs=getProjectEnv().devRuntime;
if(!envs||typeof envs.originGit=="undefined"){
return false;
}
return envs.originGit;
}







function getPackages(params){
return readRootDirectorySync("packages",params);
}







function getInstalledDependencies(params){
return readRootDirectorySync("node_modules",params);
}






function getRootPackage(){var _global$_packages;
const projectPkgPath=(_global$_packages=global._packages)===null||_global$_packages===void 0?void 0:_global$_packages._project;

if(projectPkgPath&&_fs.default.existsSync(projectPkgPath)){
return require(projectPkgPath);
}

return false;
}






function isCorenodeProject(){
return getRootPackage().name==="corenode";
}






function isDevMode(){
return _fs.default.existsSync(_path.default.resolve(_process.default.cwd(),"./.dev"));
}







function isProjectMode(dir){
const from=dir!==null&&dir!==void 0?dir:_process.default.cwd();
const packagesDir=_path.default.resolve(from,"./packages");

if(_fs.default.existsSync(packagesDir)){
if(_fs.default.readdirSync(packagesDir)){
return true;
}
return false;
}

return false;
}

function modifyRuntimeEnv(mutation){

}







function isDependencyInstalled(name){var _getRootPackage$depen,_currentPackages$name;
const currentPackages=(_getRootPackage$depen=getRootPackage().dependencies)!==null&&_getRootPackage$depen!==void 0?_getRootPackage$depen:{};
return(_currentPackages$name=currentPackages[name])!==null&&_currentPackages$name!==void 0?_currentPackages$name:false;
}











function addDependency(dependency,write=false){var _getRootPackage;
const projectPkgPath=global._packages._project;

let packageJSON=(_getRootPackage=getRootPackage())!==null&&_getRootPackage!==void 0?_getRootPackage:{};
packageJSON.dependencies[dependency.key]=dependency.value;

if(write){
_fs.default.writeFileSync(projectPkgPath,JSON.stringify(packageJSON,null,2)+"\n","utf-8");
}
return packageJSON;
}







function bumpVersion(params){
if(!params)return false;

const version=getVersion();
const parsedVersion=schemizedParse(version,global._versionScheme,".");

const bumps=[
{
type:"major",
do:()=>{
parsedVersion.major=parsedVersion.major+1;
parsedVersion.minor=0;
parsedVersion.patch=0;
}},

{
type:"minor",
do:()=>{
parsedVersion.minor=parsedVersion.minor+1;
parsedVersion.patch=0;
}},

{
type:"patch",
do:()=>{
parsedVersion.patch=parsedVersion.patch+1;
}}];



bumps.forEach(bump=>{
if(params.includes(bump.type)){
if(typeof bump.do=="function"){
bump.do();
}
}
});

const after=schemizedStringify(parsedVersion,global._versionScheme,".");
console.log(`🏷 Updated to new version ${after} > before ${version}`);

global._env.version=after;

return rewriteRuntimeEnv();
}








function syncPackageVersionFromName(name,write){
const currentVersion=getVersion();
const packageDir=_path.default.resolve(_process.default.cwd(),`./packages/${name}`);
const pkgJSONPath=_path.default.resolve(packageDir,"./package.json");

if(_fs.default.existsSync(packageDir)&&_fs.default.existsSync(pkgJSONPath)){
let pkg=require(pkgJSONPath);
if(pkg){var _global$_env$devRunti;
pkg.version=currentVersion;

if(typeof pkg["dependencies"]!=="undefined"&&typeof((_global$_env$devRunti=global._env.devRuntime)===null||_global$_env$devRunti===void 0?void 0:_global$_env$devRunti.headPackage)!=="undefined"){
Object.keys(pkg["dependencies"]).forEach(name=>{var _global$_env$devRunti2;


if(name.startsWith(`@${(_global$_env$devRunti2=global._env.devRuntime)===null||_global$_env$devRunti2===void 0?void 0:_global$_env$devRunti2.headPackage}`)){
pkg["dependencies"][name]=currentVersion;
}
});
}

if(write){
verbosity.dump(`writting update version on package [${name}] > ${pkgJSONPath} > ${JSON.stringify(pkg)}`);
_fs.default.writeFileSync(pkgJSONPath,JSON.stringify(pkg,null,2)+"\n","utf-8");
}

return pkg;
}

}
}





function syncAllPackagesVersions(){
const pkgs=getPackages();
pkgs.forEach(pkg=>{
try{
syncPackageVersionFromName(pkg,true);
verbosity.options({dumpFile:true,method:false,time:false}).log(`[${pkg}] ✅ New version synchronized`);
}catch(error){
verbosity.options({dumpFile:true,method:false,time:false}).log(`[${pkg}] ❌ Error syncing ! > ${error}`);
}
});
}

function rewriteRuntimeEnv(){
verbosity.options({dumpFile:true,time:false,method:false}).warn(`Runtime environment rewrited > ${global._envpath}`);
return _fs.default.writeFileSync(global._envpath,JSON.stringify(global._env,null,2)+"\n","utf-8");
}