forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsails-new.js
107 lines (89 loc) · 2.89 KB
/
sails-new.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
/**
* Module dependencies
*/
var nodepath = require('path');
var _ = require('@sailshq/lodash');
var sailsgen = require('sails-generate');
var CaptainsLog = require('captains-log');
var package = require('../package.json');
var rconf = require('../lib/app/configuration/rc')();
/**
* `sails new`
*
* Generate a new Sails app.
*
* ```
* # In the current directory:
* sails new
* ```
*
* ```
* # As a new directory or within an existing directory:
* sails new foo
* ```
*
* @stability 3
* @see http://sailsjs.com/documentation/reference/command-line-interface/sails-new
* ------------------------------------------------------------------------
* This command builds `scope` for the generator by scooping up any available
* configuration using `rc` (merging config from env vars, CLI opts, and
* relevant `.sailsrc` files). Then it runs the `sails-generate-new`
* generator (https://github.com/balderdashy/sails-generate-new).
*/
module.exports = function () {
// Build initial scope
var scope = {
rootPath: process.cwd(),
modules: {},
sailsRoot: nodepath.resolve(__dirname, '..'),
sailsPackageJSON: package,
viewEngine: rconf.viewEngine
};
// Support --template option for backwards-compat.
if (!scope.viewEngine && rconf.template) {
scope.viewEngine = rconf.template;
}
// Mix-in rconf
_.merge(scope, rconf.generators);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// FUTURE: Verify that we can just do a top-level merge here,
// and then reference `scope.generators.modules` as needed
// (would be simpler- but would be a breaking change, though
// unlikely to affect most people. The same issue exists in
// other places where we read rconf and then call out to
// sails-generate)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
_.merge(scope, rconf);
// Get a temporary logger just for use in `sails new`.
// > This is so that logging levels are configurable, even when a
// > Sails app hasn't been loaded yet.
var log = CaptainsLog(rconf.log);
// Pass the original CLI arguments down to the generator
// (but first, remove commander's extra argument)
var cliArguments = Array.prototype.slice.call(arguments);
cliArguments.pop();
scope.args = cliArguments;
scope.generatorType = 'new';
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()>
success: function() {
// Good to go.
}
});
};