forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsails-generate.js
106 lines (82 loc) · 2.4 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 _ = require('lodash');
var util = require('util');
var path = require('path');
var async = require('async');
var reportback = require('reportback')();
var sailsgen = require('sails-generate');
var package = require('../package.json');
var rconf = require('../lib/app/configuration/rc');
/**
* `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(),
sailsRoot: path.resolve(__dirname, '..'),
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';
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
));
}
};
return sailsgen(scope, cb);
};