'use strict';

// externals
const path = require('path');

// local
const tools = require('../../utils/tools');


// fetch args
const { dryRun } = tools.getArgs();


module.exports.getCurrentVersion = (project) => {
  return Promise.resolve()
    .then(() => {
      let version;
      const pathPkgJson = path.resolve(process.cwd(), project.folder, 'package.json');
      const pkgJson = require(pathPkgJson);
      version = pkgJson.version;

      return version;
    })

    .catch((e) => {
      throw e;
    })
}


module.exports.updatePackageVersion = (project, newVersion) => {
  return Promise.resolve()
    .then(() => {
      const pkgJsonFile = path.join(process.cwd(), project.folder, 'package.json');
      const pkgJson = require(pkgJsonFile);

      tools.logTitle('Updating package version');
      pkgJson.version = newVersion;

      if (!dryRun) {
        tools.writeJsonFileSync(pkgJsonFile, pkgJson);
      }

      tools.logSuccess(newVersion);
    })
    .catch((e) => {
      throw e;
    })
}
