<commit_before>/**
 * Module dependencies
 */

var path = require('path');
var exec = require('child_process').exec;
var spawn = require('cross-spawn');
var _ = require('lodash');
var async = require('async');
var fsx = require('fs-extra');


/**
 * after()
 *
 * Runs after this generator has successfully finished
 * processing all of its targets.
 *
 * (if any target fails, then it would run the `error` LC instead)
 *
 * @param  {Dictionary}   scope
 * @param  {Function} cb
 */

module.exports = function afterGenerate(scope, cb) {

  // Read the contents of the newly created package.json file.
  var pathToPackageJson = path.resolve(scope.rootPath, './package.json');
  var pjData = fsx.readJsonSync(pathToPackageJson);

  // Delete the sails dependency--we'll add it separately.
  // (it always gets npm linked)
  delete pjData.dependencies.sails;

  // Get the installed NPM version, if possible.
  exec('npm -v', function(err, stdout, stderr) {

    var version;
    // The strategy for NPM 3+ will always work, regardless of the
    // actual NPM version (as long as NPM is installed).  So we
    // default to the '3.0.0' strategy if `npm -v` doesn't work
    // for some reason.
    if (err) {
      version = '3.0.0';
    }
    else {
      version = stdout;
    }


    // If this is NPM version 2 or the `link` flag was enabled...
    if (version.split('.')[0] <= 2 || scope.link === true) {

      // Automatically create symlinks for all of the necessary dependencies
      // in the new app's node_modules/ subdirectory:
      async.auto({

        // Create the node_modules folder
        create_node_modules: function (next) {
          fsx.mkdir(path.resolve(scope.rootPath, './node_modules'), next);
        },

        // Create links to all necessary dependencies
        dependencies: ['create_node_modules', function(results, next) {

          async.parallel(_.map(_.keys(pjData.dependencies), function copyDependency(moduleName){

            // Make a symlink between the dependency in the sails node_modules folder,
            // and the new app's node_modules.
            return function _copyDependency(next) {
              var srcModulePath = path.resolve(scope.sailsRoot, 'node_modules', moduleName);
              var destModulePath = path.resolve(scope.rootPath, 'node_modules', moduleName);

              // Use the "junction" option for Windows
              fsx.symlink(srcModulePath, destModulePath, 'junction', function(symLinkErr) {
                // If a symbolic link fails, push it to the `nonFatalErrorsWhenCreatingSymlinks` stack,
                if (symLinkErr) {
                  nonFatalErrorsWhenCreatingSymlinks.push(symLinkErr);
                }
                // >- but keep going either way.
                return next();
              });
            };

          }), next);//</async.parallel>

        }],//</dependencies>

        // Create a link to the sails we used to create the app
        create_sails_link: ['create_node_modules', function create_sails_link(results, next) {
          fsx.symlink(scope.sailsRoot, scope.rootPath + '/node_modules/sails', 'junction', function(symLinkErr) {
            // If a symbolic link fails, push it to the `nonFatalErrorsWhenCreatingSymlinks` stack,
            if (symLinkErr) {
              nonFatalErrorsWhenCreatingSymlinks.push(symLinkErr);
            }
            // but keep going either way.
            return next();
          });
        }]
      }, function (err){
        if (err) { return cb(err); }

        // SUCCESS!
        cb.log.info('Created a new Sails app `' + scope.appName + '`!');

        // Warn that user needs to run `npm install`:
        if (nonFatalErrorsWhenCreatingSymlinks.length > 0) {
          cb.log.warn('Could not create symbolic links in the newly generated `node_modules` folder');
          cb.log.warn('(usually this is due to a permission issue on your filesystem)');
          cb.log.warn('Before you run your new app, `cd` into the directory and run:');
          cb.log.warn('$ npm install');
        }
        return cb();
      });//</async.auto :: link deps (i.e. for npm <=v2 or if `link` flag is enabled)>

      return;

    }//</if this is npm <= v2>


    // --•
    // Otherwise, this is npm v3, so we'll have to actually `npm install` some
    // of the dependencies again locally.  Luckily, we can still at least symlink
    // the `sails` dependency.  But we can't safely symlink the others, at least
    // not yet.
    //
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // > If you work on NPM, or if you've dealt with this problem before and have
    // > feedback on how to make `sails new` run faster on NPM v3, PLEASE let someone
    // > from the core team know- we could use your help!
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    // Keep track of any non-fatal errors that occur below
    // when we try to create symlinks.
    var nonFatalErrorsWhenCreatingSymlinks = [];

    async.auto({

      // Create the node_modules folder
      create_node_modules: function create_node_modules(proceed) {
        fsx.mkdir(scope.rootPath + '/node_modules', proceed);
      },

      // Create a link to the sails we used to create the app
      create_sails_link: ['create_node_modules', function create_sails_link(results, proceed) {
        fsx.symlink(scope.sailsRoot, scope.rootPath + '/node_modules/sails', 'junction', function(symLinkErr) {
          // If a symbolic link fails, push it to the `nonFatalErrorsWhenCreatingSymlinks` stack,
          if (symLinkErr) {
            nonFatalErrorsWhenCreatingSymlinks.push(symLinkErr);
          }
          // but keep going either way.
          return proceed();
        });
      }],

      // Install other dependencies with NPM
      install_other_deps: ['create_sails_link', function(results, proceed) {
        console.log(
          '=====================================================\n'+
          'You seem to be using NPM >= v3.0.0, which no longer\n'+
          'supports symbolic links in the `node_modules/` folder.\n'+
          'As of Feb. 2016, the Sails team is still working on a\n'+
          'speedier solution to optimize `sails new` that will\n'+
          'be released as a 0.12.x patch. But in the mean time,\n'+
          'your new Sails app\'s dependencies must be set up\n'+
          'using `npm install`.\n'+
          'Because of this, creating your new Sails app will\n'+
          'take longer than you might be used to. Please bear\n'+
          'with us; or if you have a need for speed, downgrade\n'+
          'to NPM < v3.0.0 in the mean time.  Thanks!\n'+
          '====================================================='+
          '\n'+
          'Installing dependencies... (this could take a little while)'
        );

        var npmInstall = spawn('npm', ['install'], {cwd: scope.rootPath, stdio: 'inherit'});
        npmInstall.on('close', proceed);
      }]

    }, function (err) {
      if (err) { return cb(err); }

      // SUCCESS!
      cb.log.info('Created a new Sails app `' + scope.appName + '`!');

      // Warn that user needs to run `npm install`:
      if (nonFatalErrorsWhenCreatingSymlinks.length > 0) {
        cb.log.warn('Could not create symbolic links in the newly generated `node_modules` folder');
        cb.log.warn('(usually this is due to a permission issue on your filesystem)');
        cb.log.warn('Before you run your new app, `cd` into the directory and run:');
        cb.log.warn('$ npm install');
      }
      return cb();
    });//</async.auto :: install deps for npm 3>

  });//</child_process.exec :: `npm -v`)>

};<commit_msg>Use path.resolve() everywhere instead of path math w/ plus signs.  Also update verbiage re the situation w/ 'sails new' wait time in npm 2 vs npm >= v3.<commit_after>/**
 * Module dependencies
 */

var path = require('path');
var exec = require('child_process').exec;
var spawn = require('cross-spawn');
var _ = require('lodash');
var async = require('async');
var fsx = require('fs-extra');


/**
 * after()
 *
 * Runs after this generator has successfully finished
 * processing all of its targets.
 *
 * (if any target fails, then it would run the `error` LC instead)
 *
 * @param  {Dictionary}   scope
 * @param  {Function} cb
 */

module.exports = function afterGenerate(scope, cb) {

  // Read the contents of the newly created package.json file.
  var pathToPackageJson = path.resolve(scope.rootPath, './package.json');
  var pjData = fsx.readJsonSync(pathToPackageJson);

  // Delete the sails dependency--we'll add it separately.
  // (it always gets npm linked)
  delete pjData.dependencies.sails;

  // Get the installed NPM version, if possible.
  exec('npm -v', function(err, stdout, stderr) {

    var version;
    // The strategy for NPM 3+ will always work, regardless of the
    // actual NPM version (as long as NPM is installed).  So we
    // default to the '3.0.0' strategy if `npm -v` doesn't work
    // for some reason.
    if (err) {
      version = '3.0.0';
    }
    else {
      version = stdout;
    }


    // If this is NPM version 2 or the `link` flag was enabled...
    if (version.split('.')[0] <= 2 || scope.link === true) {

      // Automatically create symlinks for all of the necessary dependencies
      // in the new app's node_modules/ subdirectory:
      async.auto({

        // Create the node_modules folder
        create_node_modules: function (next) {
          fsx.mkdir(path.resolve(scope.rootPath, './node_modules'), next);
        },

        // Create links to all necessary dependencies
        dependencies: ['create_node_modules', function(results, next) {

          async.parallel(_.map(_.keys(pjData.dependencies), function copyDependency(moduleName){

            // Make a symlink between the dependency in the sails node_modules folder,
            // and the new app's node_modules.
            return function _copyDependency(next) {
              var srcModulePath = path.resolve(scope.sailsRoot, 'node_modules', moduleName);
              var destModulePath = path.resolve(scope.rootPath, 'node_modules', moduleName);

              // Use the "junction" option for Windows
              fsx.symlink(srcModulePath, destModulePath, 'junction', function(symLinkErr) {
                // If a symbolic link fails, push it to the `nonFatalErrorsWhenCreatingSymlinks` stack,
                if (symLinkErr) {
                  nonFatalErrorsWhenCreatingSymlinks.push(symLinkErr);
                }
                // >- but keep going either way.
                return next();
              });
            };

          }), next);//</async.parallel>

        }],//</dependencies>

        // Create a link to the sails we used to create the app
        create_sails_link: ['create_node_modules', function create_sails_link(results, next) {
          fsx.symlink(scope.sailsRoot, path.resolve(scope.rootPath, 'node_modules/sails'), 'junction', function(symLinkErr) {
            // If a symbolic link fails, push it to the `nonFatalErrorsWhenCreatingSymlinks` stack,
            if (symLinkErr) {
              nonFatalErrorsWhenCreatingSymlinks.push(symLinkErr);
            }
            // but keep going either way.
            return next();
          });
        }]
      }, function (err){
        if (err) { return cb(err); }

        // SUCCESS!
        cb.log.info('Created a new Sails app `' + scope.appName + '`!');

        // Warn that user needs to run `npm install`:
        if (nonFatalErrorsWhenCreatingSymlinks.length > 0) {
          cb.log.warn('Could not create symbolic links in the newly generated `node_modules` folder');
          cb.log.warn('(usually this is due to a permission issue on your filesystem)');
          cb.log.warn('Before you run your new app, `cd` into the directory and run:');
          cb.log.warn('$ rm -rf node_modules/ && npm install');
        }
        return cb();
      });//</async.auto :: link deps (i.e. for npm <=v2 or if `link` flag is enabled)>

      return;

    }//</if this is npm <= v2>


    // --•
    // Otherwise, this is npm v3, so we'll have to actually `npm install` some
    // of the dependencies again locally.  Luckily, we can still at least symlink
    // the `sails` dependency.  But we can't safely symlink the others, at least
    // not yet.
    //
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    // > If you work on NPM, or if you've dealt with this problem before and have
    // > feedback on how to make `sails new` run faster on NPM v3, PLEASE let someone
    // > from the core team know- we could use your help!
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    // Keep track of any non-fatal errors that occur below
    // when we try to create symlinks.
    var nonFatalErrorsWhenCreatingSymlinks = [];

    async.auto({

      // Create the node_modules folder
      create_node_modules: function create_node_modules(proceed) {
        fsx.mkdir(path.resolve(scope.rootPath, 'node_modules'), proceed);
      },

      // Create a link to the sails we used to create the app
      create_sails_link: ['create_node_modules', function create_sails_link(results, proceed) {
        fsx.symlink(scope.sailsRoot, path.resolve(scope.rootPath, 'node_modules/sails'), 'junction', function(symLinkErr) {
          // If a symbolic link fails, push it to the `nonFatalErrorsWhenCreatingSymlinks` stack,
          if (symLinkErr) {
            nonFatalErrorsWhenCreatingSymlinks.push(symLinkErr);
          }
          // but keep going either way.
          return proceed();
        });
      }],

      // Install other dependencies with NPM
      install_other_deps: ['create_sails_link', function(results, proceed) {
        console.log(
          '=====================================================\n'+
          'You seem to be using NPM >= v3.0.0, which no longer\n'+
          'supports symbolic links in the `node_modules/` folder.\n'+
          '\n'+
          'Because of this, creating your new Sails app will\n'+
          'take longer than you might be used to. Please bear\n'+
          'with us; or if you have a need for speed, downgrade\n'+
          'to NPM v2 in the mean time.  Thanks!\n'+
          '\n'+
          'For more info about downgrading to NPM v2, see:\n'+
          'https://github.com/npm/npm/issues/10214\n'+
          '====================================================='+
          '\n'+
          'Installing dependencies... (this could take a little while)'
        );

        var npmInstall = spawn('npm', ['install'], {cwd: scope.rootPath, stdio: 'inherit'});
        npmInstall.on('close', proceed);
      }]

    }, function (err) {
      if (err) { return cb(err); }

      // SUCCESS!
      cb.log.info('Created a new Sails app `' + scope.appName + '`!');

      // Warn that user needs to run `npm install`:
      if (nonFatalErrorsWhenCreatingSymlinks.length > 0) {
        cb.log.warn('Could not create symbolic links in the newly generated `node_modules` folder');
        cb.log.warn('(usually this is due to a permission issue on your filesystem)');
        cb.log.warn('Before you run your new app, `cd` into the directory and run:');
        cb.log.warn('$ rm -rf node_modules/ && npm install');
      }
      return cb();
    });//</async.auto :: install deps for npm 3>

  });//</child_process.exec :: `npm -v`)>

};
{'fraction_non_alphanumeric': 0.13792438271604937, 'fraction_numerical': 0.003793724279835391, 'mean_word_length': 2.1969167523124358, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 30, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1589352', 'n_tokens_mistral': 4636, 'n_tokens_neox': 4394, 'n_words': 1471}