import fs from 'fs';
import path from 'path';
import inquirer from 'inquirer';
import kebabCase from 'lodash.kebabcase';
import shelljs from 'shelljs';

const questions = [
  {
    type: 'input',
    name: 'name',
    message: `what is your app's name?`,
  },
  {
    type: 'input',
    name: 'description',
    message: 'what is a brief description of your app?',
  },
  {
    type: 'input',
    name: 'git',
    message: 'what is your git url?'
  }
];

inquirer.prompt(questions, answers => {
  setupPackage({ name: kebabCase(answers.name), description: answers.description, git: answers.git })
    .then(setupGit)
    .then(() => {
      console.log(chalk.green('Success!'));
      console.log(chalk.white('Don\'t forget to setup your manifest.json and run `domo publish`'));
    })
    .catch((err) => {
      console.log(chalk.red('There was an error!'));
      console.error(err);
    });
});

function setupPackage({ name, description, git }) {
  return new Promise((resolve, reject) => {
    const filePath = path.resolve(__dirname, '../package.json');
    fs.readFile(filePath, (err, data) => {
      if (err) reject(err);

      const p = JSON.parse(data);
      p.name = name;
      p.version = '0.0.1';
      p.description = description;
      p.repository = git;

      fs.writeFile(filePath, JSON.stringify(p, null, 4), (err, data) => {
        if (err) reject(err);

        resolve({ name, description, git });
      });
    });
  });
}

function setupGit({ name, description, git }) {
  shelljs.exec('git remote rename origin generator');
  shelljs.exec(`git remote add origin ${git}`);
  shelljs.exec('git push -u origin master');
}



import fs from 'fs';
import path from 'path';
import inquirer from 'inquirer';
import kebabCase from 'lodash.kebabcase';
import shelljs from 'shelljs';

// TODO: update with the repo location on Github after we migrate
const STARTER_REPO = 'https://github.com/DomoApps/starter-kit.git';

const QUESTIONS = [
  {
    type: 'input',
    name: 'name',
    message: `what is your app's name?`,
  },
  {
    type: 'input',
    name: 'description',
    message: 'what is a brief description of your app?',
  },
  {
    type: 'input',
    name: 'git',
    message: 'what is your git url?'
  }
];

const remotes = getRemotesAsMap();
if (remotes.has('generator')) {
  console.log(`Your repo is already setup to receive dev tool updates. Run ${chalk.bold('npm run update-tools')} for updates.`);
} else if (hasChangedOriginRemote(remotes)) {
  addGeneratorRemote();
} else {
  initializeProject();
}

function getRemotesAsMap() {
  const remotes = new Map();

  const remotesOutput = shelljs.exec('git remote -v', { silent: true }).output;
  remotesOutput.trim()
    .split('\n')
    .filter(line => line.indexOf('(fetch)') !== -1)
    .forEach(remoteLine => {
      const [name, verboseLocation] = remoteLine.split('\t');
      const location = verboseLocation.replace(' (fetch)', '');
      remotes.set(name, location);
    });

  return remotes;
}

function hasChangedOriginRemote(remotes) {
  return (remotes.has('origin') && remotes.get('origin').indexOf(STARTER_REPO) !== -1);
}

function addGeneratorRemote() {
  const results = shelljs.exec(`git remote add generator ${STARTER_REPO}`);
  if (results.code === 0) {
    console.log(`${chalk.green('SUCCESS!')} The ${chalk.bold('generator')} remote has been setup. ` +
                `Run ${chalk.bold('npm run update-tools')} for updates.`);
  } else {
    console.log(`${chalk.red('ERROR:')} ${results.output}`);
  }
}

function initializeProject() {
  inquirer.prompt(QUESTIONS, answers => {
    setupPackage({ name: kebabCase(answers.name), description: answers.description, git: answers.git })
      .then(setupGit)
      .then(() => {
        console.log(chalk.green('Success!'));
        console.log(chalk.white('Don\'t forget to setup your manifest.json and run `domo publish`'));
      })
      .catch((err) => {
        console.log(chalk.red('There was an error!'));
        console.error(err);
      });
  });
}

function setupPackage({ name, description, git }) {
  return new Promise((resolve, reject) => {
    const filePath = path.resolve(__dirname, '../package.json');
    fs.readFile(filePath, (err, data) => {
      if (err) reject(err);

      const p = JSON.parse(data);
      p.name = name;
      p.version = '0.0.1';
      p.description = description;
      p.repository = git;

      fs.writeFile(filePath, JSON.stringify(p, null, 4), (err, data) => {
        if (err) reject(err);

        resolve({ name, description, git });
      });
    });
  });
}

function setupGit({ name, description, git }) {
  shelljs.exec('git remote rename origin generator');
  shelljs.exec(`git remote add origin ${git}`);
  shelljs.exec('git push -u origin master');
}



Make the `setup` script able to add the `generator` remote when the project has already been setup for the first time.