<commit_before>'use strict';

const childProcess = require('child_process');

const ora = require('ora');
const chalk = require('chalk');
const simpleGit = require('simple-git');

const command = require('./sahara');
const messages = require('./sahara/messages');
const templates = require('./create/templates');

exports = module.exports = (function() {
  let Create = function() {
    this.apiCall;

    this.exec = function(args, apiCall) {
      this.apiCall = apiCall || false;
      return new Promise((resolve, reject) => {
        if (Array.isArray(args) && args.length > 0) {
          if (!this.settings) {
            let projectDirectoryName = args.shift();
            let projectTemplate = args.shift() || 'vanilla';

            if (projectDirectoryName) {
              this.getAbsolutePathTo(projectDirectoryName)
              .then((projectAbsolutePath) => {
                this.deleteDirectory(projectAbsolutePath)
                .then((success) => {
                  this.createDirectory(projectAbsolutePath)
                  .then((success) => {
                    this.cliOptions.verbose && console.log(chalk.green(success));
                    this.cloneProjectTemplate(projectTemplate, projectAbsolutePath)
                    .then((success) => {
                      this.cliOptions.verbose && console.log(chalk.green(success));
                      this.installProjectDependencies(projectAbsolutePath)
                      .then((success) => {
                        if (success) {
                          this.cliOptions.verbose && console.log(chalk.green(success));
                        };
                        return resolve(messages.done.command.create);
                      }, (error) => {
                        if (error) {
                          console.log(chalk.red(error));
                        }
                        this.deleteDirectory(projectAbsolutePath, true)
                        .then((success) => {
                          return reject(messages.error.command.create);
                        }, (error) => {
                          return reject(messages.error.command.create);
                        });
                      });
                    }, (error) => {
                      if (error) {
                        console.log(chalk.red(error));
                      };
                      this.deleteDirectory(projectAbsolutePath, true)
                      .then((success) => {
                        return reject(messages.error.command.create);
                      }, (error) => {
                        return reject(messages.error.command.create);
                      });
                    });
                  }, (error) => {
                    if (error) {
                      console.log(chalk.red(error));
                    };
                    return reject(messages.error.command.create);
                  });
                }, (error) => {
                  if (error) {
                    console.log(chalk.red(error));
                  };
                  return reject(messages.error.command.create);
                });
              }, (error) => {
                console.log(chalk.red(error));
                return reject(messages.error.command.create);
              });
            } else {
              console.log(chalk.red(messages.error.argument.missing));
              return reject(messages.error.command.create);
            }
          } else {
            console.log(chalk.red(messages.error.sahara.projectDirectory));
            return reject(messages.error.command.create);
          }
        } else {
          console.log(chalk.red(messages.error.argument.missing));
          return reject(messages.error.command.create);
        }
      });
    };

    this.installProjectDependencies = function(projectAbsolutePath) {
      return new Promise((resolve, reject) => {
        let command = `cd ${projectAbsolutePath} && npm install`;

        let spinner = ora({
          text: chalk.yellow(messages.info.dependencies.install),
          spinner: 'pong',
          color: 'yellow'
        });

        spinner.start();

        childProcess.exec(command, (error, stdout, stderr) => {
          if (error) {
            spinner.fail(chalk.red(messages.info.dependencies.install));
            return reject(error);
          } else {
            if (stderr) {
              spinner.fail(chalk.red(messages.info.dependencies.install));
              return reject(stderr);
            } else {
              spinner.succeed(chalk.green(messages.info.dependencies.install));
              return resolve(messages.done.dependencies.install);
            }
          }
        });
      });
    };

    this.cloneProjectTemplate = function(projectTemplate, projectAbsolutePath) {
      this.cliOptions.verbose && console.log(chalk.yellow(messages.info.template.clone));

      return new Promise((resolve, reject) => {
        if (templates[projectTemplate]) {
          simpleGit().clone(templates[projectTemplate], projectAbsolutePath)
          .then((error, success) => {
            if (error) {
              console.log(chalk.red(error));
              return reject(messages.error.template.clone);
            } else {
              return resolve(messages.done.template.cloned);
            }
          });
        } else {
          return reject(messages.error.template.notFound);
        }
      });
    };
  };

  Create.prototype = command;<commit_msg>Adding a special case for npm >=5 throwing an stderr message rather than a notice.<commit_after>'use strict';

const childProcess = require('child_process');

const ora = require('ora');
const chalk = require('chalk');
const simpleGit = require('simple-git');

const command = require('./sahara');
const messages = require('./sahara/messages');
const templates = require('./create/templates');

exports = module.exports = (function() {
  let Create = function() {
    this.apiCall;

    this.exec = function(args, apiCall) {
      this.apiCall = apiCall || false;
      return new Promise((resolve, reject) => {
        if (Array.isArray(args) && args.length > 0) {
          if (!this.settings) {
            let projectDirectoryName = args.shift();
            let projectTemplate = args.shift() || 'vanilla';

            if (projectDirectoryName) {
              this.getAbsolutePathTo(projectDirectoryName)
              .then((projectAbsolutePath) => {
                this.deleteDirectory(projectAbsolutePath)
                .then((success) => {
                  this.createDirectory(projectAbsolutePath)
                  .then((success) => {
                    this.cliOptions.verbose && console.log(chalk.green(success));
                    this.cloneProjectTemplate(projectTemplate, projectAbsolutePath)
                    .then((success) => {
                      this.cliOptions.verbose && console.log(chalk.green(success));
                      this.installProjectDependencies(projectAbsolutePath)
                      .then((success) => {
                        if (success) {
                          this.cliOptions.verbose && console.log(chalk.green(success));
                        };
                        return resolve(messages.done.command.create);
                      }, (error) => {
                        if (error) {
                          console.log(chalk.red(error));
                        }
                        this.deleteDirectory(projectAbsolutePath, true)
                        .then((success) => {
                          return reject(messages.error.command.create);
                        }, (error) => {
                          return reject(messages.error.command.create);
                        });
                      });
                    }, (error) => {
                      if (error) {
                        console.log(chalk.red(error));
                      };
                      this.deleteDirectory(projectAbsolutePath, true)
                      .then((success) => {
                        return reject(messages.error.command.create);
                      }, (error) => {
                        return reject(messages.error.command.create);
                      });
                    });
                  }, (error) => {
                    if (error) {
                      console.log(chalk.red(error));
                    };
                    return reject(messages.error.command.create);
                  });
                }, (error) => {
                  if (error) {
                    console.log(chalk.red(error));
                  };
                  return reject(messages.error.command.create);
                });
              }, (error) => {
                console.log(chalk.red(error));
                return reject(messages.error.command.create);
              });
            } else {
              console.log(chalk.red(messages.error.argument.missing));
              return reject(messages.error.command.create);
            }
          } else {
            console.log(chalk.red(messages.error.sahara.projectDirectory));
            return reject(messages.error.command.create);
          }
        } else {
          console.log(chalk.red(messages.error.argument.missing));
          return reject(messages.error.command.create);
        }
      });
    };

    this.installProjectDependencies = function(projectAbsolutePath) {
      return new Promise((resolve, reject) => {
        let command = `cd ${projectAbsolutePath} && npm install`;

        let spinner = ora({
          text: chalk.yellow(messages.info.dependencies.install),
          spinner: 'pong',
          color: 'yellow'
        });

        spinner.start();

        childProcess.exec(command, (error, stdout, stderr) => {
          if (error) {
            spinner.fail(chalk.red(messages.info.dependencies.install));
            return reject(error);
          } else {
            if (stderr) {
              // To prevent failure while using npm >= 5.0.0.
              if (stderr.indexOf("npm notice created a lockfile as package-lock.json. You should commit this file.") !== -1) {
                spinner.succeed(chalk.green(messages.info.dependencies.install));
                return resolve(messages.done.dependencies.install);
              } else {
                spinner.fail(chalk.red(messages.info.dependencies.install));
                return reject(stderr);
              }
            } else {
              spinner.succeed(chalk.green(messages.info.dependencies.install));
              return resolve(messages.done.dependencies.install);
            }
          }
        });
      });
    };

    this.cloneProjectTemplate = function(projectTemplate, projectAbsolutePath) {
      this.cliOptions.verbose && console.log(chalk.yellow(messages.info.template.clone));

      return new Promise((resolve, reject) => {
        if (templates[projectTemplate]) {
          simpleGit().clone(templates[projectTemplate], projectAbsolutePath)
          .then((error, success) => {
            if (error) {
              console.log(chalk.red(error));
              return reject(messages.error.template.clone);
            } else {
              return resolve(messages.done.template.cloned);
            }
          });
        } else {
          return reject(messages.error.template.notFound);
        }
      });
    };
  };

  Create.prototype = command;
{'fraction_non_alphanumeric': 0.1217519770574433, 'fraction_numerical': 0.0006083253671678109, 'mean_word_length': 1.4817770109984905, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4585666', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2681, 'n_words': 432}