forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_generate-api.js
46 lines (40 loc) · 1.32 KB
/
_generate-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Module dependencies
*/
var async = require('async')
, sailsgen = require('sails-generate')
, STRINGFILE = require('sails-stringfile')
, _ = require('lodash');
//
// Run both the controller and model generators and add in a couple
// of extra log messages.
// (todo: pull this out into a simple generator)
module.exports = function generateAPI (scope, cb) {
console.log();
// Create the controller and model
async.parallel(_.map(['controller', 'model'], subGen), function(err) {
// As long as there were no errors, add some logs about how to call the new API
if (!err) {
console.log();
cb.log.info('REST API generated @ ' + ('http://localhost:1337/' + scope.args[0]).underline);
cb.log.info('and will be available the next time you run `sails lift`.');
}
});
// Create a function suitable for use with async.parallel to run a generator.
// Uses "cb.log" b/c it has that nice log format.
function subGen(generatorType) {
var _scope = _.extend({}, _.cloneDeep(scope), {generatorType: generatorType});
return function(_cb) {
sailsgen (_scope, {
success: function() {
cb.log('Generated a new '+_scope.generatorType+' `'+_scope.id+'` at '+_scope.destDir+_scope.globalID+'.js!');
return _cb();
},
error: function(err) {
cb.error(err);
return _cb(err);
}
});
};
}
};