<commit_before>'use strict';

var mu = require('mu2');
var Q = require('q');
var path = require('path');
var inflection = require('inflection');
var stream = require('stream');
var utils = require('./../../helpers/utils');

exports = module.exports = API;

function modelMap(model) {
	var properties = [];
	var propertiesMap = model.getAllProperties();
	Object.keys(propertiesMap).forEach(function(propertyName) {
		var property = propertiesMap[propertyName];

		if(!property.options.isPrivate) {
			properties.push({
				name: property.name,
				capitalName: inflection.capitalize(property.name),
				hasMethod: !!property.options.hasMethod,
				hasMany: !!property.options.hasMany
			});
		}
	});

	return {
		name: model.getName(),
		authenticatingPropertyName: model.options.authenticatingProperty ? model.options.authenticatingProperty.name : null,
		isAuthenticator: model.isAuthenticator(),
		pluralName: inflection.pluralize(model.getName()),
		lowerCaseName: inflection.camelize(model.getName(), true),
		properties: properties
	};
}

/**
 * The API module.
 *
 * This module generates model controllers of all models during build phase and executes the controllers in the run phase. The model controllers get generated to /_api. To generate the model controllers:
 *
 * ```
 * $ fire generate api
 * ```
 *
 * @access private
 *
 * @param {App} app The app.
 * @constructor
 * @memberof API
 */
function API(app) {
	this.app = app;
}

/**
 * Loads the model controllers in the run phase. This method is called when the app starts up. The model controllers are loaded in the `_api/` directory.
 *
 * @param  {String} basePath The root directory of the app.
 */
API.prototype.setup = function(basePath) {
	utils.requireDirSync(path.join(basePath, '_api'));
};

API.prototype.ignoreDisabled = true;

/**
 * Writes the model controller of `model` to `writeStream`.
 *
 * Once this method finishes, it does close the `writeStream`.
 *
 * @param {Model} model       The model.
 * @param {fs.Writable} writeStream The write stream to write to.
 */
API.prototype.generateModelController = function(model, writeStream) {
	var defer = Q.defer();

	if(model.disableAutomaticModelController) {
		console.log('Warning: not generating ' + model.getName() + ' because disableAutomaticModelController is set to true. Please update your Gruntfile.js to hide this warning.');

		defer.resolve(null);
	}

	var readStream = mu.compileAndRender(path.join(__dirname, 'templates', 'model-controller-js.mu'), {
		fire: process.env.NODE_ENV === 'test' ? './..' : 'fire',
		model: modelMap(model),
		controllerName: model.getName() + 'ModelController',
		appName: this.app.name
	});

	var errorCallback = function(error) {
		removeEventListeners();

		defer.reject(error);
	};
	var successCallback = function() {
		removeEventListeners();

		defer.resolve(false);
	};

	function removeEventListeners() {
		readStream.removeListener('end', successCallback);
		writeStream.removeListener('error', errorCallback);
		writeStream.removeListener('finish', successCallback);
	}

	writeStream.once('error', errorCallback);

	if(writeStream instanceof stream.Writable) {
		writeStream.once('finish', successCallback);
	}
	else {
		// The memory stream unfortunately does not emit the finish event. Instead, we'll listen when reading ends.
		readStream.once('end', successCallback);
	}

	readStream.pipe(writeStream);
	return defer.promise;
};<commit_msg>fixes issue with generating disabled model controller<commit_after>'use strict';

var mu = require('mu2');
var Q = require('q');
var path = require('path');
var inflection = require('inflection');
var stream = require('stream');
var utils = require('./../../helpers/utils');

exports = module.exports = API;

function modelMap(model) {
	var properties = [];
	var propertiesMap = model.getAllProperties();
	Object.keys(propertiesMap).forEach(function(propertyName) {
		var property = propertiesMap[propertyName];

		if(!property.options.isPrivate) {
			properties.push({
				name: property.name,
				capitalName: inflection.capitalize(property.name),
				hasMethod: !!property.options.hasMethod,
				hasMany: !!property.options.hasMany
			});
		}
	});

	return {
		name: model.getName(),
		authenticatingPropertyName: model.options.authenticatingProperty ? model.options.authenticatingProperty.name : null,
		isAuthenticator: model.isAuthenticator(),
		pluralName: inflection.pluralize(model.getName()),
		lowerCaseName: inflection.camelize(model.getName(), true),
		properties: properties
	};
}

/**
 * The API module.
 *
 * This module generates model controllers of all models during build phase and executes the controllers in the run phase. The model controllers get generated to /_api. To generate the model controllers:
 *
 * ```
 * $ fire generate api
 * ```
 *
 * @access private
 *
 * @param {App} app The app.
 * @constructor
 * @memberof API
 */
function API(app) {
	this.app = app;
}

/**
 * Loads the model controllers in the run phase. This method is called when the app starts up. The model controllers are loaded in the `_api/` directory.
 *
 * @param  {String} basePath The root directory of the app.
 */
API.prototype.setup = function(basePath) {
	utils.requireDirSync(path.join(basePath, '_api'));
};

API.prototype.ignoreDisabled = true;

/**
 * Writes the model controller of `model` to `writeStream`.
 *
 * Once this method finishes, it does close the `writeStream`.
 *
 * @param {Model} model       The model.
 * @param {fs.Writable} writeStream The write stream to write to.
 */
API.prototype.generateModelController = function(model, writeStream) {
	var defer = Q.defer();

	if(model.disableAutomaticModelController) {
		console.log('Warning: not generating ' + model.getName() + ' because disableAutomaticModelController is set to true. Please update your Gruntfile.js to hide this warning.');

		defer.resolve(null);
	}
	else {
		var readStream = mu.compileAndRender(path.join(__dirname, 'templates', 'model-controller-js.mu'), {
			fire: process.env.NODE_ENV === 'test' ? './..' : 'fire',
			model: modelMap(model),
			controllerName: model.getName() + 'ModelController',
			appName: this.app.name
		});

		var errorCallback = function(error) {
			removeEventListeners();

			defer.reject(error);
		};
		var successCallback = function() {
			removeEventListeners();

			defer.resolve(false);
		};

		var removeEventListeners = function() {
			readStream.removeListener('end', successCallback);
			writeStream.removeListener('error', errorCallback);
			writeStream.removeListener('finish', successCallback);
		};

		writeStream.once('error', errorCallback);

		if(writeStream instanceof stream.Writable) {
			writeStream.once('finish', successCallback);
		}
		else {
			// The memory stream unfortunately does not emit the finish event. Instead, we'll listen when reading ends.
			readStream.once('end', successCallback);
		}

		readStream.pipe(writeStream);
	}

	return defer.promise;
};
{'fraction_non_alphanumeric': 0.1313364055299539, 'fraction_numerical': 0.0002880184331797235, 'mean_word_length': 4.4771293375394325, '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': '4858677', 'n_tokens_mistral': 2143, 'n_tokens_neox': 1992, 'n_words': 552}