"use strict";

var _os = require("os");

var _path = _interopRequireDefault(require("path"));

var _fs = _interopRequireDefault(require("fs"));

var _child_process = require("child_process");

var _index = require("./index");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function spawnSyncWithErrorLog(...args) {
  let result = (0, _child_process.spawnSync)(...args);

  if (result.stderr) {
    console.error(result.stderr);
  }
}

let options = (0, _index.getOptions)();
let {
  clone: {
    type,
    url,
    branch,
    revision,
    projectName,
    shallowClone,
    cacheDir,
    remoteName,
    shouldDelete
  }
} = options;

if (type && (type === 'git' || type === 'hg')) {
  (0, _index.log)(`Going to clone ${type} repository`);
} else {
  throw new Error('You must give valid type to clone a repository');
}

if (!cacheDir) {
  cacheDir = _path.default.join((0, _os.homedir)(), '.react-cli');
} else {
  cacheDir = _path.default.join(process.cwd(), cacheDir);
}

if (!projectName) {
  let {
    name
  } = _path.default.parse(url);

  projectName = name;
}

let projectPath = _path.default.join(cacheDir, projectName);

let cloneRepo = () => {
  if (!_fs.default.existsSync(cacheDir)) {
    _fs.default.mkdirSync(cacheDir);
  }

  let revisionOrBranch;

  if (type === 'git') {
    revisionOrBranch = `-b${branch}`;
  } else {
    if (revision) {
      revisionOrBranch = `-r${revision}`;
    } else {
      revisionOrBranch = `-b${branch}`;
    }
  }

  (0, _index.log)(`Going to clone ${url} repo to ${cacheDir} path`); // this is for some time error will because of hg or git so we addid error log for this

  let oargs = ['clone', url, revisionOrBranch, projectName];
  type === 'git' && shallowClone && oargs.push('--depth=1');
  spawnSyncWithErrorLog(type, oargs, {
    cwd: cacheDir,
    stdio: 'inherit'
  });

  _fs.default.writeFileSync(_path.default.join(projectPath, type === 'hg' ? '.hg' : '.git', 'remoteUrl'), url);

  (0, _index.log)('Repository cloned!');
};

let getRemoteURL = () => {
  let remoteUrl = _fs.default.readFileSync(_path.default.join(projectPath, type === 'hg' ? '.hg' : '.git', 'remoteUrl')).toString();

  return remoteUrl.trim();
};

if (_fs.default.existsSync(projectPath)) {
  let remoteUrl = getRemoteURL();

  if (remoteUrl === url) {
    if (type === 'git') {
      spawnSyncWithErrorLog(type, ['pull', remoteName, branch], {
        cwd: projectPath,
        stdio: 'inherit'
      });
    } else {
      let revisionOrBranch;

      if (revision) {
        revisionOrBranch = `-r${revision}`;
      } else {
        revisionOrBranch = `-b${branch}`;
      }

      spawnSyncWithErrorLog(type, ['pull', revisionOrBranch, '-u', url], {
        cwd: projectPath,
        stdio: 'inherit'
      });
    }
  } else {
    shouldDelete && (0, _child_process.spawnSync)('rm', ['-rf', projectName], {
      cwd: cacheDir,
      stdio: 'inherit'
    });
    (0, _index.log)('Existing repository deleted!');
    cloneRepo();
  }
} else {
  cloneRepo();
}