Skip to content

Commit

Permalink
Deprecate req.validate() in favor of actions2. Remove anchor dep. Rem…
Browse files Browse the repository at this point in the history
…ove reportback dep.
  • Loading branch information
mikermcneil committed Oct 17, 2016

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 5c865fe commit 68fa8ff
Showing 3 changed files with 167 additions and 158 deletions.
160 changes: 85 additions & 75 deletions bin/sails-generate.js
Original file line number Diff line number Diff line change
@@ -2,29 +2,28 @@
* Module dependencies
*/

var _ = require('lodash');
var util = require('util');
var path = require('path');
var assert = require('assert');
var _ = require('lodash');
var async = require('async');
var reportback = require('reportback')();
var sailsgen = require('sails-generate');
var CaptainsLog = require('captains-log');
var sailsGen = require('sails-generate');
var package = require('../package.json');
var rconf = require('../lib/app/configuration/rc');


/**
* `sails generate`
*
* Generate one or more file(s) in our working directory.
* This runs an appropriate generator.
*
* @stability 2
* @see http://sailsjs.org/documentation/reference/command-line-interface/sails-generate
* @see http://sailsjs.com/docs/reference/command-line-interface/sails-generate
*/

module.exports = function() {
module.exports = function () {

// Build initial scope
// Build initial scope for our call to sails-generate.
var scope = {
rootPath: process.cwd(),
sailsRoot: path.resolve(__dirname, '..'),
@@ -33,90 +32,101 @@ module.exports = function() {
};

// Mix-in rc config
// (note that we mix in everything namespaced under `generators` at the top level-
// but also that anything at the top level takes precedence)
_.merge(scope, rconf.generators);

// TODO: just do a top-level merge and reference
// `scope.generators.modules` as needed (simpler)
_.merge(scope, rconf);


// Pass the original CLI arguments down to the generator
// (but first, remove commander's extra argument)
// (also peel off the `generatorType` arg)
// Get a temporary logger just for use in `sails generate`.
// > This is so that logging levels are configurable, even when a
// > Sails app hasn't been loaded yet.
var log = CaptainsLog(rconf.log);


// Pass down the original serial args from the CLI.
// > Note that (A) first, we remove the last arg from commander using `_.initial`,
// > and then (B) second, we remove ANOTHER arg -- the one representing the
// > generator type -- in favor of just setting `scope.generatorType`.
var cliArguments = _.initial(arguments);
scope.generatorType = cliArguments.shift();
scope.args = cliArguments;
assert(arguments.length === (scope.args.length + 2), new Error('Consistency violation: Should have trimmed exactly two args.'));

// Create a new reportback
var cb = reportback.extend();

// Show usage if no generator type is defined
// If no generator type was defined, then log the expected usage.
if (!scope.generatorType) {
return cb.log.error('Usage: sails generate [something]');
console.log('Usage: sails generate [something]');
return;
}

// Handle unexpected errors.
cb.error = function(err) {
var log = this.log || cb.log;
log.error(err);
process.exit(1);
};
// Call out to `sails-generate`.
return sailsGen(scope, {

// Handle invalid usage.
cb.invalid = function(err) {
var log = this.log || cb.log;
// Handle unexpected errors.
error: function (err) {

// If this is an Error, don't bother logging the stack, just log the `.message`.
// (This is purely for readability.)
if (_.isError(err)) {
log.error(err.message);
}
else {
log.error(err);
}
return process.exit(1);

process.exit(1);
};
},//</on error :: sailsGen()>

// Handle success
cb.success = function() {

// Infer the `outputPath` if necessary/possible.
if (!scope.outputPath && scope.filename && scope.destDir) {
scope.outputPath = scope.destDir + scope.filename;
}

// Humanize the output path
var humanizedPath;
if (scope.outputPath) {
humanizedPath = ' at ' + scope.outputPath;
}
else if (scope.destDir) {
humanizedPath = ' in ' + scope.destDir;
}
else {
humanizedPath = '';
}

// Humanize the module identity
var humanizedId;
if (scope.id) {
humanizedId = util.format(' ("%s")',scope.id);
}
else {
humanizedId = '';
}

if (scope.generatorType !== 'new') {

cb.log.info(util.format(
'Created a new %s%s%s!',
scope.generatorType, humanizedId, humanizedPath
));

}
// Attend to invalid usage.
invalid: function (err) {

};
// If this is an Error, don't bother logging the stack, just log the `.message`.
// (This is purely for readability.)
if (_.isError(err)) {
log.error(err.message);
}
else {
log.error(err);
}

return process.exit(1);

},//</on invalid :: sailsGen()>

// Enjoy success.
success: function (){

// Infer the `outputPath` if necessary/possible.
if (!scope.outputPath && scope.filename && scope.destDir) {
scope.outputPath = scope.destDir + scope.filename;
}

// Humanize the output path
var humanizedPath;
if (scope.outputPath) {
humanizedPath = ' at ' + scope.outputPath;
}
else if (scope.destDir) {
humanizedPath = ' in ' + scope.destDir;
}
else {
humanizedPath = '';
}

// Humanize the module identity
var humanizedId;
if (scope.id) {
humanizedId = util.format(' ("%s")',scope.id);
}
else {
humanizedId = '';
}

if (scope.generatorType !== 'new') {

log.info(util.format(
'Created a new %s%s%s!',
scope.generatorType, humanizedId, humanizedPath
));

}

}//</on success :: sailsGen()>

});//</sailsGen()>

return sailsgen(scope, cb);
};
163 changes: 82 additions & 81 deletions lib/hooks/request/validate.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Module dependencies
*/
// /**
// * Module dependencies
// */

var _ = require('lodash');
var anchor = require('anchor');
// var _ = require('lodash');
// var anchor = require('anchor');



@@ -14,11 +14,10 @@ var anchor = require('anchor');
* @param {Response} res
* @return {Request}
*
* Note that built-in support for req.validate() will change in Sails v1.0 along
* with other major changes to `anchor`.
* Note that built-in support for req.validate() has changed in Sails v1.0
* (alongwith other major changes to `anchor`.)
*/
module.exports = function (req, res) {
req.validate = _validate;

/**
* req.validate()
@@ -30,35 +29,37 @@ module.exports = function (req, res) {
* (optional)
*
* @throws {Error}
* @api public
* @api deprecated
*/

function _validate (usage, redirectTo) {
usage = usage || {};

// Wrap `usage` in a `type` key, since req.params.all()
// always returns an object anyways.
var invalidParams = anchor(req.params.all()).to({type: usage});
if (invalidParams) {

var e = new E_INVALID_PARAMS({
invalidParams: invalidParams,
route: req.url,
method: req.method,
usage: usage
});

if (redirectTo) {
if (req.session && req.flash) {
req.flash('error', e);
}
res.redirect(redirectTo);
}
else {
throw e;
}
}
}
req.validate = function (usage, redirectTo) {
throw new Error('As of Sails v1, `req.validate` is no longer supported. Instead use actions2: http://sailsjs.com/docs/concepts/controllers');

// usage = usage || {};

// // Wrap `usage` in a `type` key, since req.params.all()
// // always returns an object anyways.
// var invalidParams = anchor(req.params.all()).to({type: usage});
// if (invalidParams) {

// var e = new E_INVALID_PARAMS({
// invalidParams: invalidParams,
// route: req.url,
// method: req.method,
// usage: usage
// });

// if (redirectTo) {
// if (req.session && req.flash) {
// req.flash('error', e);
// }
// res.redirect(redirectTo);
// }
// else {
// throw e;
// }
// }
};//</function definition :: req.validate>

return req;
};
@@ -68,49 +69,49 @@ module.exports = function (req, res) {



/**
* Constructs an E_INVALID_PARAMS error.
* @constructor
*/
function E_INVALID_PARAMS (opts) {
this.invalidParams = opts.invalidParams;
this.route = opts.route;
this.method = opts.method;
this.usage = opts.usage;

// Generate stack trace
var e = new Error();
this.stack = e.stack;
}
E_INVALID_PARAMS.prototype.code = 'E_INVALID_PARAMS';
E_INVALID_PARAMS.prototype.status = 400;

/**
* How this error is serialized when sent w/ `res.json()`
* @return {Object}
*/
E_INVALID_PARAMS.prototype.toJSON = function () {
return _.map(this.invalidParams, function (invalidParam) {
return {
parameter: invalidParam.property,
error: invalidParam.message,
expectedType: invalidParam.rule,
actualType: invalidParam.actualType,
data: invalidParam.data
};
});
};

/**
* How this error appears when logged
* (or whenever util.inspect is called on it)
* @return {String}
*/
E_INVALID_PARAMS.prototype.inspect = function () {
var output = 'Invalid parameters sent to route: "'+this.method + ' ' + this.route+'"';
output += '\n';
output += _.map(this.invalidParams, function (invalidParam) {
return ' -> ' + invalidParam.message;
});
return output;
};
// /**
// * Constructs an E_INVALID_PARAMS error.
// * @constructor
// */
// function E_INVALID_PARAMS (opts) {
// this.invalidParams = opts.invalidParams;
// this.route = opts.route;
// this.method = opts.method;
// this.usage = opts.usage;

// // Generate stack trace
// var e = new Error();
// this.stack = e.stack;
// }
// E_INVALID_PARAMS.prototype.code = 'E_INVALID_PARAMS';
// E_INVALID_PARAMS.prototype.status = 400;

// /**
// * How this error is serialized when sent w/ `res.json()`
// * @return {Object}
// */
// E_INVALID_PARAMS.prototype.toJSON = function () {
// return _.map(this.invalidParams, function (invalidParam) {
// return {
// parameter: invalidParam.property,
// error: invalidParam.message,
// expectedType: invalidParam.rule,
// actualType: invalidParam.actualType,
// data: invalidParam.data
// };
// });
// };

// /**
// * How this error appears when logged
// * (or whenever util.inspect is called on it)
// * @return {String}
// */
// E_INVALID_PARAMS.prototype.inspect = function () {
// var output = 'Invalid parameters sent to route: "'+this.method + ' ' + this.route+'"';
// output += '\n';
// output += _.map(this.invalidParams, function (invalidParam) {
// return ' -> ' + invalidParam.message;
// });
// return output;
// };
Loading

0 comments on commit 68fa8ff

Please sign in to comment.