<commit_before>/**
 * Module dependencies.
 */

var _ = require('lodash'),
	npm = require('enpeem'),
	path = require('path'),
	async = require('async'),
	Sails = require('../../../lib/app'),
	switcher = require('sails-util/switcher'),
	GenerateModuleHelper = require('../_helpers/module'),
	GenerateFolderHelper = require('../_helpers/folder');
	GenerateJSONHelper = require('../_helpers/jsonfile');

/**
 * Expose `sails new` functionality
 *
 * @param {Object} options
 * @param {Object} [handlers]
 */

module.exports = {


	/**
	 * Generate new Sails app.
	 * 
	 * @param  {[type]} options  [description]
	 * @param  {[type]} handlers [description]
	 * @return {[type]}          [description]
	 */
	generate: function createNewApp( options, handlers ) {

		handlers = switcher(handlers);

		if ( !options.appName ) return handlers.missingAppName();

		// Resolve absolute appPath
		var appPath = path.resolve( process.cwd(), options.appName );

		options.appPath = appPath;

		var folders = [
			'api',
				'api/controllers',
				'api/models',
				'api/adapters',
				'api/policies',
				'api/services',
			'config',
			'views',
			'assets',
				'assets/js',
				'assets/styles',
				'assets/templates',
			'node_modules',
		];

		var templateFiles = [
			require('sails-generate-gruntfile'),
			require('./generators/gitignore'),
			require('./generators/README.md'),
			'assets/js/sails.io.js',
			'assets/js/socket.io.js',
			'assets/js/socketio_example.js'
		];

		

		if (options.dry) {
			log.debug( 'DRY RUN');
			return handlers.success('Would have created a new app `' + options.appName + '` at ' + appPath + '.');
		}
		

		async.auto({

			folders: function(cb) {
				async.each(folders, function(folder, cb) {
					cb = switcher(cb);

					// Build folders
					GenerateFolderHelper({
						pathToNew: path.resolve(appPath,folder),
						gitkeep: true
					}, {
						alreadyExists: function (path) {
							return cb('A file or folder already exists at the destination path :: ' + path);
						},
						error: cb,
						success: cb.success
					});
				}, cb);
			},

			templates: ['folders', function(cb) {
				async.each(templateFiles, function(fileOrGenerator, cb) {
					cb = switcher(cb);

					// Build new options set
					var opts;
					if (typeof fileOrGenerator === "string") {

						// No custom generator exists: just copy file from templates
						opts = _.extend({force: true}, options,{
							generator: {},
							templateFilePath: path.resolve(__dirname,'./templates/' + fileOrGenerator),
							pathToNew: path.resolve(appPath, fileOrGenerator)
						});
					}
					else {

						// Use custom generator
						opts = _.extend({force: true}, options,{
							generator: fileOrGenerator
						});
					}

					// Generate module
					GenerateModuleHelper(opts, cb);
				}, cb);
			}],

			'sails': function (cb) {
				// Bootstrap sails to get the version
				var sails = new Sails();
				sails.load({
					appPath: options.appPath,
					globals: false,
					loadHooks: ['userconfig', 'moduleloader']
				}, function loadedSails (err) {
					if (err) return cb(err);
					cb(null,sails);
				});
			},

			'package.json': ['folders','sails', function (cb, async_data) {
				cb = switcher(cb);

				var sails = async_data.sails;

				// Generate package.json options
				var opts = {
					pathToNew: path.resolve(appPath, 'package.json'),
					data: {
						name: options.appName,
						'private': true,
						version: '0.0.0',
						description: 'a Sails application',
						dependencies: {
							'sails'			: '~' + sails.version,
							'sails-disk'	: sails.dependencies['sails-disk'],
							'ejs'			: sails.dependencies['ejs'],
							'grunt'			: sails.dependencies['grunt']
						},
						scripts: {
							// TODO: Include this later when we have "sails test" ready.
							// test: './node_modules/mocha/bin/mocha -b',
							start: 'node app.js',
							debug: 'node debug app.js'
						},
						main: 'app.js',
						repository: '',
						author: '',
						license: ''
					}
				};

				// Generate json file
				GenerateJSONHelper(opts, cb);

			}],


			// TODO: Copy required app-level dependencies
			// (to avoid having to do a local npm install in new projects)
			copyAppDependencies: ['folders','sails', function (cb, async_data) {
				
				var sails = async_data.sails;

				// Build dependency strings
				// The dependencies we should copy over:
				var dependenciesToCopy = [
					'sails-disk@'+sails.dependencies['sails-disk'],
					'ejs@'+sails.dependencies['ejs'],
					'grunt@'+sails.dependencies['grunt']
				];

				// `cd` into the newly created app and load up npm
				process.chdir(appPath);

				// Just for diagnostics
				// var cmd = 'npm install ' + dependenciesToCopy.join(' ');
				// console.time(cmd);

				// Install dependencies from npm cache
				npm.install( dependenciesToCopy, {
					// see: https://github.com/isaacs/npm/issues/2568#issuecomment-30626394
					'cache-min': 999999999,<commit_msg>Removed unnecessary code in package.json generation.<commit_after>/**
 * Module dependencies.
 */

var _ = require('lodash'),
	npm = require('enpeem'),
	path = require('path'),
	async = require('async'),
	Sails = require('../../../lib/app'),
	switcher = require('sails-util/switcher'),
	GenerateModuleHelper = require('../_helpers/module'),
	GenerateFolderHelper = require('../_helpers/folder');
	GenerateJSONHelper = require('../_helpers/jsonfile');

/**
 * Expose `sails new` functionality
 *
 * @param {Object} options
 * @param {Object} [handlers]
 */

module.exports = {


	/**
	 * Generate new Sails app.
	 * 
	 * @param  {[type]} options  [description]
	 * @param  {[type]} handlers [description]
	 * @return {[type]}          [description]
	 */
	generate: function createNewApp( options, handlers ) {

		handlers = switcher(handlers);

		if ( !options.appName ) return handlers.missingAppName();

		// Resolve absolute appPath
		var appPath = path.resolve( process.cwd(), options.appName );

		options.appPath = appPath;

		var folders = [
			'api',
				'api/controllers',
				'api/models',
				'api/adapters',
				'api/policies',
				'api/services',
			'config',
			'views',
			'assets',
				'assets/js',
				'assets/styles',
				'assets/templates',
			'node_modules',
		];

		var templateFiles = [
			require('sails-generate-gruntfile'),
			require('./generators/gitignore'),
			require('./generators/README.md'),
			'assets/js/sails.io.js',
			'assets/js/socket.io.js',
			'assets/js/socketio_example.js'
		];

		

		if (options.dry) {
			log.debug( 'DRY RUN');
			return handlers.success('Would have created a new app `' + options.appName + '` at ' + appPath + '.');
		}
		

		async.auto({

			folders: function(cb) {
				async.each(folders, function(folder, cb) {
					cb = switcher(cb);

					// Build folders
					GenerateFolderHelper({
						pathToNew: path.resolve(appPath,folder),
						gitkeep: true
					}, {
						alreadyExists: function (path) {
							return cb('A file or folder already exists at the destination path :: ' + path);
						},
						error: cb,
						success: cb.success
					});
				}, cb);
			},

			templates: ['folders', function(cb) {
				async.each(templateFiles, function(fileOrGenerator, cb) {
					cb = switcher(cb);

					// Build new options set
					var opts;
					if (typeof fileOrGenerator === "string") {

						// No custom generator exists: just copy file from templates
						opts = _.extend({force: true}, options,{
							generator: {},
							templateFilePath: path.resolve(__dirname,'./templates/' + fileOrGenerator),
							pathToNew: path.resolve(appPath, fileOrGenerator)
						});
					}
					else {

						// Use custom generator
						opts = _.extend({force: true}, options,{
							generator: fileOrGenerator
						});
					}

					// Generate module
					GenerateModuleHelper(opts, cb);
				}, cb);
			}],

			'sails': function (cb) {
				// Bootstrap sails to get the version
				var sails = new Sails();
				sails.load({
					appPath: options.appPath,
					globals: false,
					loadHooks: ['userconfig', 'moduleloader']
				}, function loadedSails (err) {
					if (err) return cb(err);
					cb(null,sails);
				});
			},

			'package.json': ['folders','sails', function (cb, async_data) {
				cb = switcher(cb);

				var sails = async_data.sails;

				// Generate package.json file
				GenerateJSONHelper({
					pathToNew: path.resolve(appPath, 'package.json'),
					data: {
						name: options.appName,
						'private': true,
						version: '0.0.0',
						description: 'a Sails application',
						dependencies: {
							'sails'			: '~' + sails.version,
							'sails-disk'	: sails.dependencies['sails-disk'],
							'ejs'			: sails.dependencies['ejs'],
							'grunt'			: sails.dependencies['grunt']
						},
						scripts: {
							// TODO: Include this later when we have "sails test" ready.
							// test: './node_modules/mocha/bin/mocha -b',
							start: 'node app.js',
							debug: 'node debug app.js'
						},
						main: 'app.js',
						repository: '',
						author: '',
						license: ''
					}
				}, cb);

			}],


			// TODO: Copy required app-level dependencies
			// (to avoid having to do a local npm install in new projects)
			copyAppDependencies: ['folders','sails', function (cb, async_data) {
				
				var sails = async_data.sails;

				// Build dependency strings
				// The dependencies we should copy over:
				var dependenciesToCopy = [
					'sails-disk@'+sails.dependencies['sails-disk'],
					'ejs@'+sails.dependencies['ejs'],
					'grunt@'+sails.dependencies['grunt']
				];

				// `cd` into the newly created app and load up npm
				process.chdir(appPath);

				// Just for diagnostics
				// var cmd = 'npm install ' + dependenciesToCopy.join(' ');
				// console.time(cmd);

				// Install dependencies from npm cache
				npm.install( dependenciesToCopy, {
					// see: https://github.com/isaacs/npm/issues/2568#issuecomment-30626394
					'cache-min': 999999999,
{'fraction_non_alphanumeric': 0.15547063555913113, 'fraction_numerical': 0.004827031375703942, 'mean_word_length': 2.6335403726708075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '5256984', 'n_tokens_mistral': 4220, 'n_tokens_neox': 3078, 'n_words': 723}