<commit_before>#!/usr/bin/env node

// Dependencies
var _ = require('underscore');
_.str = require('underscore.string');
var ejs = require('ejs');
var fs = require('fs-extra');
var util = require('util');


var optimist = require('optimist');
var argv = optimist.argv;

// Build mock sails object
var sails = require('./mockSails.js');

// Output in directory where cmdline tool was run
var outputPath = '.';

// Basic usage
if (argv._.length === 0) {
	sails.log.info('Welcome to Sails!\n');
	sailsUsage();
}
// Start this app
else if ( _.contains(['lift', 'raise', 'launch', 'start', 'server', 'run', 's', 'l'], argv._[0]) ) {
	require(process.cwd()+'/app.js');
}
// Get the sails version
else if (argv.v || argv.version || argv._[0] === 'version') {
	sails.log.info('v'+sails.version);
}
// Generate file(s)
else if(argv._[0].match(/^g$|^ge$|^gen$|^gene$|^gener$|^genera$|^generat$|^generate$/) || argv.g || argv.generate) {

	verifyArg(1, "Please specify the name for the new model and controller as the second argument.");


	// Generate a model
	if(argv._[1] === 'model') {
		var entity = argv._[2];
		verifyArg(2, "Please specify the name for the new model as the third argument.");

		// Figure out attributes based on args
		var options = _.extend({},argv);
		options.attributes = argv._.splice(3);
		generateModel(entity, options);
	}

	// Generate a controller
	else if(argv._[1] === 'controller') {
		var entity = argv._[2];
		verifyArg(2, "Please specify the name for the new controller as the third argument.");
		
		// Figure out actions based on args
		var options = _.extend({},argv);
		options.actions = argv._.splice(3);
		generateController(entity, options);
	}

	// Otherwise generate a model, controller, and view directory
	else {
		var entity = argv._[1];
		verifyArg(1, "Please specify the name of the entity to generate a model, controller, and view for as the second argument.");
		sails.log.info("Generating model and controller for " + entity);
		
		var options = _.extend({},argv);
		options.actions = argv._.splice(2);

		generateModel(entity,options);
		generateController(entity,options);
	}
}

// Create a new app
else if(argv._[0].match(/^n$|^ne$|^new$/) || argv.n || argv['new']) {
	sails.log.info("Generating Sails project...");
	verifyArg(1, "Please specify the name of the new project directory as the first argument.");

	// If not an action, first argument == app name
	var appName = argv._[1];
	outputPath = outputPath + "/" + appName;
	verifyDoesntExist(outputPath, "A file or directory already exists at: " + outputPath);

	// Create app directory
	generateDir();
	
	// Create default app structure
	generateDir('ui');
	generateDir(sails.config.paths.dependencies);
	generateDir(sails.config.paths['public']);
	generateDir("ui/public/styles");
	generateDir("ui/public/images");
	generateDir("ui/public/js");
	generateDir(sails.config.paths.views);
	generateDir(sails.config.paths.templates);
	generateFile('layout.ejs', sails.config.paths.layout);

	generateDir('api');
	generateDir(sails.config.paths.models);
	generateDir(sails.config.paths.controllers);
	generateDir(sails.config.paths.middleware);
	// NOTE: We are not creating an adapters directory for now to keep things simple.
	// NOTE: We are not creating a services directory for now to keep things simple.
	
	generateDir('config');
	generateFile('config/routes.js', 'config/routes.js');
	generateFile('config/policy.js', 'config/policy.js');
	generateDir('config/locales');
	generateFile('config/locales/english.js', 'config/locales/english.js');
	generateFile('config/local.js', 'config/local.js');
	


	// Create driver file
	generateFile('app.js', 'app.js');

	// Create default home page
	generateFile('index.html', sails.config.paths['public'] + '/index.html');

	// Copy default favicon
	copyBlueprint('favicon.ico',sails.config.paths['public'] + '/favicon.ico');

	// Create default css reset
	generateFile('reset.css', sails.config.paths['public'] + "/styles/reset.css");

	// Create default user management and auth middleware
	// TODO	

	// Create readme files
	// generateFile('__readme_models.md', "models/__readme.md");
	// generateFile('__readme_controllers.md', "controllers/__readme.md");
	// generateFile('__readme_views.md', "views/__readme.md");
	// generateFile('__readme_middleware.md', "middleware/__readme_middleware.md");

	// Create .gitignore
	generateFile('gitignore', '.gitignore');

	// Generate package.json
	sails.log.debug("Generating package.json...");
	fs.writeFileSync(outputPath + '/package.json', JSON.stringify({
		name: appName,
		'private': true,
		version: '0.0.0',
		description: 'a Sails application',
		dependencies: {
			sails: sails.version
		},
		scripts: {
			test: 'mocha -b',
			start: 'node app.js'
		},
		main: 'app.js',
		repository: '',
		author: '',
		license: 'MIT'
	}, null, 4));

	// Generate README
	sails.log.debug("Generating README.md...");
	fs.writeFileSync(outputPath + '/README.md', '# ' + appName + '\n### a Sails application');
}
// Unknown command
else  {
	sails.log.error("Unknown usage!");
	sailsUsage();
}


// Display usage
function sailsUsage () {
	sails.log.info('Usage: sails <command>\n'+
		'\n'+
		'sails lift\t\tRun this Sails app (in the current dir)\n'+
		'sails new <appName>\t\tCreate a new Sails project in the current dir\n'+
		'sails generate <foo>\tGenerate model and controller for this app\n'+
		'sails version\t\tGet the current globally installed Sails version'
	);
}


// Generate a file
function generateFile(blueprintPath, newPath) {
	var file = fs.readFileSync(__dirname + '/blueprints/' + (blueprintPath || ""), 'utf8');
	var newFilePath = outputPath + '/' + (newPath || "");
	verifyDoesntExist(newFilePath, "A file/directory already exists at "+newFilePath);
	fs.writeFileSync(newFilePath, file);
}

// Generate a directory
function generateDir(newPath) {
	if (!newPath) sails.log.debug("Generating app directory...");
	else sails.log.debug("Generating directory " + newPath + "...");
	var newDirPath = outputPath + "/" + (newPath || "");<commit_msg>Allowed express/rails-style app creation usage (e.g. [32minfo[39m: Generating Sails project (newApp)...   === [32minfo[39m: Generating Sails project (newApp)...  )<commit_after>#!/usr/bin/env node

// Dependencies
var _ = require('underscore');
_.str = require('underscore.string');
var ejs = require('ejs');
var fs = require('fs-extra');
var util = require('util');


var optimist = require('optimist');
var argv = optimist.argv;

// Build mock sails object
var sails = require('./mockSails.js');

// Output in directory where cmdline tool was run
var outputPath = '.';

// Basic usage
if (argv._.length === 0) {
	sails.log.info('Welcome to Sails!\n');
	sailsUsage();
}
// Start this app
else if ( _.contains(['lift', 'raise', 'launch', 'start', 'server', 'run', 's', 'l'], argv._[0]) ) {
	require(process.cwd()+'/app.js');
}
// Get the sails version
else if (argv.v || argv.version || argv._[0] === 'version') {
	sails.log.info('v'+sails.version);
}
// Generate file(s)
else if(argv._[0].match(/^g$|^ge$|^gen$|^gene$|^gener$|^genera$|^generat$|^generate$/) || argv.g || argv.generate) {

	verifyArg(1, "Please specify the name for the new model and controller as the second argument.");


	// Generate a model
	if(argv._[1] === 'model') {
		var entity = argv._[2];
		verifyArg(2, "Please specify the name for the new model as the third argument.");

		// Figure out attributes based on args
		var options = _.extend({},argv);
		options.attributes = argv._.splice(3);
		generateModel(entity, options);
	}

	// Generate a controller
	else if(argv._[1] === 'controller') {
		var entity = argv._[2];
		verifyArg(2, "Please specify the name for the new controller as the third argument.");
		
		// Figure out actions based on args
		var options = _.extend({},argv);
		options.actions = argv._.splice(3);
		generateController(entity, options);
	}

	// Otherwise generate a model, controller, and view directory
	else {
		var entity = argv._[1];
		verifyArg(1, "Please specify the name of the entity to generate a model, controller, and view for as the second argument.");
		sails.log.info("Generating model and controller for " + entity);
		
		var options = _.extend({},argv);
		options.actions = argv._.splice(2);

		generateModel(entity,options);
		generateController(entity,options);
	}
}

// Create a new app
// second argument == app name
else if(argv._[0].match(/^n$|^ne$|^new$/) || argv.n || argv['new']) {
	createNewApp(argv._[1]);
}

// Unknown command, assume creating a new app w/ that name
// First argument == app name
else  {
	verifyArg(0, "Please specify the name of the new project directory as the first argument.");
	createNewApp(argv._[0]);
}

function createNewApp (appName) {
	sails.log.info("Generating Sails project ("+appName+")...");

	outputPath = outputPath + "/" + appName;
	verifyDoesntExist(outputPath, "A file or directory already exists at: " + outputPath);

	// Create app directory
	generateDir();
	
	// Create default app structure
	generateDir('ui');
	generateDir(sails.config.paths.dependencies);
	generateDir(sails.config.paths['public']);
	generateDir("ui/public/styles");
	generateDir("ui/public/images");
	generateDir("ui/public/js");
	generateDir(sails.config.paths.views);
	generateDir(sails.config.paths.templates);
	generateFile('layout.ejs', sails.config.paths.layout);

	generateDir('api');
	generateDir(sails.config.paths.models);
	generateDir(sails.config.paths.controllers);
	generateDir(sails.config.paths.middleware);
	// NOTE: We are not creating an adapters directory for now to keep things simple.
	// NOTE: We are not creating a services directory for now to keep things simple.
	
	generateDir('config');
	generateFile('config/routes.js', 'config/routes.js');
	generateFile('config/policy.js', 'config/policy.js');
	generateDir('config/locales');
	generateFile('config/locales/english.js', 'config/locales/english.js');
	generateFile('config/local.js', 'config/local.js');
	


	// Create driver file
	generateFile('app.js', 'app.js');

	// Create default home page
	generateFile('index.html', sails.config.paths['public'] + '/index.html');

	// Copy default favicon
	copyBlueprint('favicon.ico',sails.config.paths['public'] + '/favicon.ico');

	// Create default css reset
	generateFile('reset.css', sails.config.paths['public'] + "/styles/reset.css");

	// Create default user management and auth middleware
	// TODO	

	// Create readme files
	// generateFile('__readme_models.md', "models/__readme.md");
	// generateFile('__readme_controllers.md', "controllers/__readme.md");
	// generateFile('__readme_views.md', "views/__readme.md");
	// generateFile('__readme_middleware.md', "middleware/__readme_middleware.md");

	// Create .gitignore
	generateFile('gitignore', '.gitignore');

	// Generate package.json
	sails.log.debug("Generating package.json...");
	fs.writeFileSync(outputPath + '/package.json', JSON.stringify({
		name: appName,
		'private': true,
		version: '0.0.0',
		description: 'a Sails application',
		dependencies: {
			sails: sails.version
		},
		scripts: {
			test: 'mocha -b',
			start: 'node app.js'
		},
		main: 'app.js',
		repository: '',
		author: '',
		license: 'MIT'
	}, null, 4));

	// Generate README
	sails.log.debug("Generating README.md...");
	fs.writeFileSync(outputPath + '/README.md', '# ' + appName + '\n### a Sails application');
}


// Display usage
function sailsUsage () {
	sails.log.info('Usage: sails <command>\n'+
		'\n'+
		'sails lift\t\tRun this Sails app (in the current dir)\n'+
		'sails new <appName>\t\tCreate a new Sails project in the current dir\n'+
		'sails generate <foo>\tGenerate model and controller for this app\n'+
		'sails version\t\tGet the current globally installed Sails version'
	);
}


// Generate a file
function generateFile(blueprintPath, newPath) {
	var file = fs.readFileSync(__dirname + '/blueprints/' + (blueprintPath || ""), 'utf8');
	var newFilePath = outputPath + '/' + (newPath || "");
	verifyDoesntExist(newFilePath, "A file/directory already exists at "+newFilePath);
	fs.writeFileSync(newFilePath, file);
}

// Generate a directory
function generateDir(newPath) {
	if (!newPath) sails.log.debug("Generating app directory...");
	else sails.log.debug("Generating directory " + newPath + "...");
	var newDirPath = outputPath + "/" + (newPath || "");
{'fraction_non_alphanumeric': 0.16930187302359523, 'fraction_numerical': 0.004621746533690099, 'mean_word_length': 4.33939393939394, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '5058449', 'n_tokens_mistral': 4149, 'n_tokens_neox': 3929, 'n_words': 1032}