forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsails-generate.js
107 lines (80 loc) · 2.6 KB
/
sails-generate.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
/**
* Module dependencies
*/
var package = require('../package.json')
, reportback = require('reportback')()
, rc = require('rc')
, _ = require('lodash')
, async = require('async')
, sailsgen = require('sails-generate');
/**
* `sails generate`
*
* Generate module(s) for the app in our working directory.
* Internally, uses ejs for rendering the various module templates.
*/
module.exports = function ( ) {
// Get CLI configuration
var config = rc('sails');
// Build initial scope
var scope = {
rootPath: process.cwd(),
modules: {},
sailsPackageJSON: package,
};
// Mix-in rc config
_.merge(scope, config.generators);
// TODO: just do a top-level merge and reference
// `scope.generators.modules` as needed (simpler)
_.merge(scope, config);
// Get command-line arguments
var cliArguments = Array.prototype.slice.call(arguments);
// Remove commander's extra argument
cliArguments.pop();
// Peel off the generatorType and the rest of the args
scope.generatorType = cliArguments.shift();
scope.args = cliArguments;
// Create a new reportback
var cb = reportback.extend();
// Set the "invalid" exit to forward to "error"
cb.invalid = 'error';
// If the generator type is "api", run both the controller and model generators
// and add in a couple extra log messages.
if (scope.generatorType == 'api') {
// 'api' only takes one argument, so ignore the rest
cliArguments = [cliArguments[0]];
// 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) {
cb.log.info('REST API will be available next time you lift the server.');
cb.log.info('(@ `/' + cliArguments[0] + '` with default settings)');
}
});
// 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(generator) {
var _scope = _.extend(_.cloneDeep(scope), {generatorType: generator});
return function(_cb) {
sailsgen (_scope, {
success: function() {
cb.log.info('Generated a new '+_scope.generatorType+' `'+_scope.id+'` at '+_scope.destDir+_scope.globalID+'.js!');
return _cb();
},
error: function(err) {
cb.error(err);
return _cb(err);
}
});
}
}
}
// Otherwise just run whichever generator was requested.
else {
cb.success = function() {
cb.log.info('Generated a new '+scope.generatorType+' `'+scope.id+'` at '+scope.destDir+scope.globalID+'.js!');
}
return sailsgen( scope, cb );
}
};