const chalk = require('chalk');
const cli = require('./cli');
const build = require('./build');
const postBuild = require('./post-build');
const install = require('./install');

const appGenerate = (options) => {
  console.info(chalk.cyan('Configuring your project : please answer the following : \n'));

  var cliConfig;

  // Query the questions
  // -------------------
  return Promise.resolve()
    .then(() => {
      return cli.start(options);
    })

    // Storing config
    // --------------
    .then((config) => {
      cliConfig = config;
      console.log(config);
    })

    // Building the file structure
    // ---------------------------
    .then(() => {
      return build.start({
        config: cliConfig,
        targetPath: options.targetPath
      });
    })

    // Post build operations
    // ---------------------
    .then(() => {
      return postBuild.start({
        config: cliConfig,
        targetPath: options.targetPath
      });
    })

    // Install dependencies
    // --------------------
    .then(() => {
      return install.start({
        config: cliConfig,
        targetPath: options.targetPath,
      });
    })

    .catch((e) => {
      console.error(e);
      process.exitCode = 1;
    })

    .finally(() => process.exit())
}


module.exports = {
  appGenerate,
};
