forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsails-generate.js
87 lines (65 loc) · 1.97 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
#!/usr/bin/env node
/**
* Module dependencies
*/
var package = require('../package.json')
, reportback = require('reportback')()
, _ = require('lodash')
, rconf = require('../lib/configuration/rc')
, 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 ( ) {
// Build initial scope
var scope = {
rootPath: process.cwd(),
modules: {},
sailsPackageJSON: package,
};
// Mix-in rc config
_.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)
var cliArguments = Array.prototype.slice.call(arguments);
cliArguments.pop();
scope.generatorType = cliArguments.shift();
scope.args = cliArguments;
// Create a new reportback
var cb = reportback.extend();
// Show usage if no generator type is defined
if (!scope.generatorType) {
return cb.log.error('Usage: sails generate [something]');
}
// Set the "invalid" exit to forward to "error"
cb.error = function(msg) {
var log = this.log || cb.log;
log.error(msg);
process.exit(1);
};
cb.invalid = 'error';
// If the generator type is "api", we currently treat it as a special case.
// (todo: pull this out into a simple generator)
if (scope.generatorType === 'api') {
if (scope.args.length === 0) {
return cb.error('Usage: sails generate api [api name]');
}
require('./_generate-api')(scope, cb);
}
// 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 );
}
};