<commit_before>var _ = require('lodash');
_.str = require('underscore.string');
var ejs = require('ejs');
var fs = require('fs-extra');

// Make existsSync not crash on older versions of Node
fs.existsSync = fs.existsSync || require('path').existsSync;

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

module.exports = {
	generateFile: generateFile,
	generateDir: generateDir,
	verifyDoesntExist: verifyDoesntExist,
	fileExists: fileExists,
	renderBoilerplateTemplate: renderBoilerplateTemplate,
	verfiyExists: verifyExists,
	copySails: copySails,
	copyBoilerplate: copyBoilerplate,
	copySailsDependency: copySailsDependency,
	verifyValidEntity: verifyValidEntity,
	trimSlashes: trimSlashes,
	capitalize: capitalize
};

// Generate a file
function generateFile(boilerplatePath, newPath) {
	var fullBpPath = __dirname + '/boilerplates/' + (boilerplatePath || '');
	var file = fs.readFileSync(fullBpPath, 'utf8');
	var newFilePath = (newPath || '');
	verifyDoesntExist(newFilePath, 'A file/directory already exists at ' + newFilePath);

	// Touch output file to make sure the path to it exists
	if (fs.createFileSync(newFilePath)) {
		sails.log.error('Could not create file, ' + newFilePath + '!');
		process.exit(1);
	}
	fs.writeFileSync(newFilePath, file);
}

// Generate a directory
function generateDir(newPath, gitkeep) {
	if (!newPath) {
		sails.log.debug('Generating app directory...');
	} else {
		sails.log.debug('Generating directory ' + newPath + '...');
	}
	var newDirPath = (newPath || '');
	verifyDoesntExist(newDirPath, 'A file/directory already exists at ' + newDirPath);
	fs.mkdirSync(newDirPath);
	// If directory will be empty, create a .gitkeep in it
	if (gitkeep) {
		generateFile('.gitkeep', newPath + '/.gitkeep');
	}
}

function verifyDoesntExist(path, msg) {
	if (fileExists(path)) {
		sails.log.error(msg);
		process.exit(1);
	}
}

// Check if a file or directory exists

function fileExists(path) {
	try {
		// Query the entry
		var stats = fs.lstatSync(path);

		// Is it a directory?
		if (stats.isDirectory() || stats.isFile()) {
			return true;
		}
	} catch (e) {
		// ...
	}

	return false;
}

// Read a boilerplate and render the template

function renderBoilerplateTemplate(boilerplate, data) {
	var boilerplatePath = __dirname + '/boilerplates/templates/' + boilerplate;
	verifyExists(boilerplatePath, "Boilerplate (" + boilerplate + ") doesn't exist!");
	var file = fs.readFileSync(boilerplatePath, 'utf8');
	return ejs.render(file, data);
}

function verifyExists(path, msg) {
	if (!fileExists(path)) {
		sails.log.error(msg);
		process.exit(1);
	}
}

// Copy a boilerplate directory or file
function copyBoilerplate(boilerplate, destination, cb) {
	var boilerplatePath = __dirname + '/boilerplates/' + boilerplate;
	fs.copy(boilerplatePath, destination, function(err) {
		return cb && cb(err);
	});
}

// Copy Sails into a project as a local dependency
function copySails(destination, cb) {
	sails.log.debug('Copying Sails.js runtime into new project...');
	try {
		fs.mkdirSync(destination);
	}
	catch(e) {
		return cb && cb(e);
	}

	require('async').forEach(['lib', 'package.json', 'node_modules'], function (fileOrDir, cb) {
		fs.copy(__dirname + '/../' + fileOrDir, destination + '/' + fileOrDir, cb);
	}, function (err) {
		return cb && cb(err);
	});
}

// Copy a Sails dependency, in a smart way
function copySailsDependency(moduleName, pathToNewNodeModules, cb) {
	var path = __dirname + '/../node_modules/' + moduleName;
	fs.copy(path, pathToNewNodeModules + '/' + moduleName, function(err) {
		if (err) return cb && cb(err);

		// Parse the module's package.json
		var packageJSONPath = path + '/package.json';
		var packageJSON;
		try {
			packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf-8'));
		}
		catch (e) { 

			// Ignore missing package.json
			packageJSON = { dependencies: {} };
		}

		// Get actual dependencies in this module's node_modules directory
		var dependencies;
		try {
			dependencies = fs.readdirSync(path + '/node_modules');
			
			// Remove hidden files
			_.without(dependencies, function (val) {
				return val.match(/\..+/);
			});
		} 
		catch(e) { 
			// Assume empty dependencies
			dependencies = {};
		}

		// If there are any missing dependencies which are being pulled from Sails,
		// copy them from Sails' main node_modules directory
		var missingModules = _.difference(_.keys(packageJSON.dependencies || {}), _.values(dependencies));
		_.each(missingModules, function (missingModuleName) {
			sails.log.verbose('Resolving '+moduleName+'\'s missing dependency ('+missingModuleName+') using the version in Sails.');
			copySailsDependency(missingModuleName, pathToNewNodeModules + '/' + moduleName + '/node_modules/');
		});
		return cb && cb(err);
	});<commit_msg>turned on log<commit_after>var _ = require('lodash');
_.str = require('underscore.string');
var ejs = require('ejs');
var fs = require('fs-extra');

// Make existsSync not crash on older versions of Node
fs.existsSync = fs.existsSync || require('path').existsSync;

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

module.exports = {
	generateFile: generateFile,
	generateDir: generateDir,
	verifyDoesntExist: verifyDoesntExist,
	fileExists: fileExists,
	renderBoilerplateTemplate: renderBoilerplateTemplate,
	verfiyExists: verifyExists,
	copySails: copySails,
	copyBoilerplate: copyBoilerplate,
	copySailsDependency: copySailsDependency,
	verifyValidEntity: verifyValidEntity,
	trimSlashes: trimSlashes,
	capitalize: capitalize
};

// Generate a file
function generateFile(boilerplatePath, newPath) {
	var fullBpPath = __dirname + '/boilerplates/' + (boilerplatePath || '');
	var file = fs.readFileSync(fullBpPath, 'utf8');
	var newFilePath = (newPath || '');
	verifyDoesntExist(newFilePath, 'A file/directory already exists at ' + newFilePath);

	// Touch output file to make sure the path to it exists
	if (fs.createFileSync(newFilePath)) {
		sails.log.error('Could not create file, ' + newFilePath + '!');
		process.exit(1);
	}
	fs.writeFileSync(newFilePath, file);
}

// Generate a directory
function generateDir(newPath, gitkeep) {
	if (!newPath) {
		sails.log.debug('Generating app directory...');
	} else {
		sails.log.debug('Generating directory ' + newPath + '...');
	}
	var newDirPath = (newPath || '');
	verifyDoesntExist(newDirPath, 'A file/directory already exists at ' + newDirPath);
	fs.mkdirSync(newDirPath);
	// If directory will be empty, create a .gitkeep in it
	if (gitkeep) {
		generateFile('.gitkeep', newPath + '/.gitkeep');
	}
}

function verifyDoesntExist(path, msg) {
	if (fileExists(path)) {
		sails.log.error(msg);
		process.exit(1);
	}
}

// Check if a file or directory exists

function fileExists(path) {
	try {
		// Query the entry
		var stats = fs.lstatSync(path);

		// Is it a directory?
		if (stats.isDirectory() || stats.isFile()) {
			return true;
		}
	} catch (e) {
		// ...
	}

	return false;
}

// Read a boilerplate and render the template

function renderBoilerplateTemplate(boilerplate, data) {
	var boilerplatePath = __dirname + '/boilerplates/templates/' + boilerplate;
	verifyExists(boilerplatePath, "Boilerplate (" + boilerplate + ") doesn't exist!");
	var file = fs.readFileSync(boilerplatePath, 'utf8');
	return ejs.render(file, data);
}

function verifyExists(path, msg) {
	if (!fileExists(path)) {
		sails.log.error(msg);
		process.exit(1);
	}
}

// Copy a boilerplate directory or file
function copyBoilerplate(boilerplate, destination, cb) {
	var boilerplatePath = __dirname + '/boilerplates/' + boilerplate;
	fs.copy(boilerplatePath, destination, function(err) {
		return cb && cb(err);
	});
}

// Copy Sails into a project as a local dependency
function copySails(destination, cb) {
	sails.log.debug('Copying Sails.js runtime into new project...');
	try {
		fs.mkdirSync(destination);
	}
	catch(e) {
		return cb && cb(e);
	}

	require('async').forEach(['lib', 'package.json', 'node_modules'], function (fileOrDir, cb) {
		fs.copy(__dirname + '/../' + fileOrDir, destination + '/' + fileOrDir, cb);
	}, function (err) {
		return cb && cb(err);
	});
}

// Copy a Sails dependency, in a smart way
function copySailsDependency(moduleName, pathToNewNodeModules, cb) {
	var path = __dirname + '/../node_modules/' + moduleName;
	fs.copy(path, pathToNewNodeModules + '/' + moduleName, function(err) {
		if (err) return cb && cb(err);

		// Parse the module's package.json
		var packageJSONPath = path + '/package.json';
		var packageJSON;
		try {
			packageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf-8'));
		}
		catch (e) { 

			// Ignore missing package.json
			packageJSON = { dependencies: {} };
		}

		// Get actual dependencies in this module's node_modules directory
		var dependencies;
		try {
			dependencies = fs.readdirSync(path + '/node_modules');
			
			// Remove hidden files
			_.without(dependencies, function (val) {
				return val.match(/\..+/);
			});
		} 
		catch(e) { 
			// Assume empty dependencies
			dependencies = {};
		}

		// If there are any missing dependencies which are being pulled from Sails,
		// copy them from Sails' main node_modules directory
		var missingModules = _.difference(_.keys(packageJSON.dependencies || {}), _.values(dependencies));
		_.each(missingModules, function (missingModuleName) {
			sails.log('Resolving '+moduleName+'\'s missing dependency ('+missingModuleName+') using the version in Sails.');
			copySailsDependency(missingModuleName, pathToNewNodeModules + '/' + moduleName + '/node_modules/');
		});
		return cb && cb(err);
	});
{'fraction_non_alphanumeric': 0.14167539267015708, 'fraction_numerical': 0.001256544502617801, 'mean_word_length': 4.276795580110497, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1295415', 'n_tokens_mistral': 3308, 'n_tokens_neox': 3066, 'n_words': 780}