'use strict';

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

// LOCAL
const tools = require('./tools');

// FETCH ARGS
const { dryRun } = tools.getArgs();

const timer = {
  start: new Date(),
  end: null,
  duration: 0,
}

const startTimer = () => {
  timer.start = new Date();
}
const endTimer = () => {
  timer.end = new Date();
}

const getTimerDuration = () => {
  timer.end = new Date();
  return timer.end - timer.start;
}

const setVariables = (rootPath, content) => {
  return Promise.resolve()
    .then(() => {
      tools.logInfo(`Updating content of root variables in ${rootPath}`);

      const variablesFile = path.join(rootPath, 'variables');

      var fileContent = '';
      if (tools.isFileExists(variablesFile)) {
        fileContent = tools.getFileContent(variablesFile);
      }

      tools.logInfo(`Write metadata on ${variablesFile}`);
      if (!dryRun) {
        fileContent += `${content}\n`;
        tools.writeFileContent(variablesFile, fileContent);
      }
    })
    .catch((e) => {
      throw e;
    })
}

module.exports = {
  setVariables,
  startTimer,
  endTimer,
  getTimerDuration
}
