forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsails-generate.js
134 lines (104 loc) · 3.56 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Module dependencies
*/
var util = require('util');
var path = require('path');
var assert = require('assert');
var _ = require('@sailshq/lodash');
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.
*
* @see http://sailsjs.com/docs/reference/command-line-interface/sails-generate
*/
module.exports = function () {
// Build initial scope for our call to sails-generate.
var scope = {
rootPath: process.cwd(),
sailsRoot: path.resolve(__dirname, '..'),
modules: {},
sailsPackageJSON: package,
};
// 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);
_.merge(scope, rconf);
// 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;
// If no generator type was defined, then log the expected usage.
if (!scope.generatorType) {
console.log('Usage: sails generate [something]');
return;
}
assert(arguments.length === (scope.args.length + 2), new Error('Consistency violation: Should have trimmed exactly two args.'));
// Call out to `sails-generate`.
return sailsGen(scope, {
// Handle unexpected errors.
error: function (err) {
log.error(err);
return process.exit(1);
},//</on error :: sailsGen()>
// 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 this isn't the "new" generator, and we're not explicitly
// asked not to, output a final success message.
if (scope.generatorType !== 'new' && !scope.suppressFinalLog) {
log.info(util.format(
'Created a new %s%s%s!',
scope.generatorType, humanizedId, humanizedPath
));
}
}//</on success :: sailsGen()>
});//</sailsGen()>
};