forked from balderdashy/sails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsails-upgrade.js
222 lines (186 loc) · 8.56 KB
/
sails-upgrade.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* Module dependencies
*/
var path = require('path');
var chalk = require('chalk');
var _ = require('@sailshq/lodash');
var sailsgen = require('sails-generate');
var flaverr = require('flaverr');
var semver = require('semver');
var rconf = require('../lib/app/configuration/rc')();
/**
* `sails upgrade`
*
* Upgrade a pre v1.0.x app to Sails v1.0.x.
*
* ```
* # In the root directory of your Sails app:
* sails upgrade
* ```
*/
module.exports = function () {
if (!rconf.reportOnly) {
console.log(chalk.gray('Checking compatibility for Sails v1.0 upgrade...'));
try {
var packageJson;
try {
var pathToLocalPackageJson = path.resolve(process.cwd(), 'package.json');
packageJson = require(pathToLocalPackageJson);
} catch (e) {
switch (e.code) {
case 'MODULE_NOT_FOUND': throw flaverr('E_NO_PACKAGE_JSON', new Error('No package.json file. Are you sure you\'re in the root directory of a Sails app?'));
default: throw e;
}
}
if (_.isUndefined(packageJson.dependencies)) {
throw flaverr('E_NO_SAILS_DEP', new Error('This package.json file does not declare any dependencies. Are you sure you\'re in the root directory of a Sails app?'));
}
if (!_.isObject(packageJson.dependencies) || _.isArray(packageJson.dependencies)) {
throw flaverr('E_NO_SAILS_DEP', new Error('This package.json file has an invalid `dependencies` property -- should be a dictionary (plain JS object).'));
}
var sailsDepSVR = packageJson.dependencies.sails;
if (!sailsDepSVR) {
throw flaverr('E_NO_SAILS_DEP', new Error('This package.json file does not declare `sails` as a dependency. Are you sure you\'re in the root directory of a Sails app?'));
}
if (!semver.ltr('0.9.9999', sailsDepSVR)) {
throw flaverr('E_SAILS_DEP_DEFINITELY_TOO_OLD', new Error('this app depends on sails@'+sailsDepSVR+'.'));
}
if (!semver.ltr('0.11.9999', sailsDepSVR)) {
throw flaverr('E_SAILS_DEP_MIGHT_BE_TOO_OLD', new Error('this app depends on sails@'+sailsDepSVR+'.'));
}
// if (semver.ltr('0.12.9999', sailsDepSVR)) {
// throw flaverr('E_SAILS_DEP_IS_ALREADY_V1', new Error('this app already depends on sails@'+sailsDepSVR+'...'));
// }
console.log();
console.log('----------------------------------------------------');
console.log('This utility will kickstart the process of migrating');
console.log('this Sails v0.12.x app to Sails v1.');
console.log('----------------------------------------------------');
console.log();
} catch (e) {
switch (e.code) {
case 'E_SAILS_DEP_IS_ALREADY_V1':
console.log();
console.log('----------------------------------------------------');
console.log('This utility is designed to kickstart the process of');
console.log('migrating a '+chalk.bold('v0.12.x')+' app to Sails v1.');
console.log();
console.log(chalk.yellow.bold('But '+e.message));
console.log(chalk.reset('Maybe you already started upgrading it?'));
console.log(chalk.reset('If so, then please press CTRL+C to cancel now, or'));
console.log(chalk.reset('otherwise feel free to proceed with care-- this'));
console.log(chalk.reset('upgrade tool may still work partially as-is.'));
console.log(chalk.gray('For more help, visit '+chalk.underline('http://sailsjs.com/support')+'.'));
console.log('----------------------------------------------------');
console.log();
break;
case 'E_SAILS_DEP_MIGHT_BE_TOO_OLD':
console.log();
console.log('----------------------------------------------------');
console.log('This utility is designed to kickstart the process of');
console.log('migrating a '+chalk.bold('v0.12.x')+' app to Sails v1.');
console.log();
console.log(chalk.yellow.bold('But '+e.message));
console.log(chalk.reset('This upgrade tool may partially work as-is, but we recommend'));
console.log(chalk.reset('using the appropriate guide(s) to upgrade to Sails v0.12 first.'));
console.log(chalk.reset('See '+chalk.underline('http://sailsjs.com/upgrading')+' for details.'));
console.log(chalk.gray('(Press CTRL+C to cancel -- or proceed at your own risk!)'));
console.log('----------------------------------------------------');
console.log();
break;
case 'E_SAILS_DEP_DEFINITELY_TOO_OLD':
console.log('--');
console.log(chalk.red.bold('Well, '+e.message));
console.log(chalk.reset('It looks to be built for a version of Sails that is probably too'));
console.log(chalk.reset('old to work with this upgrade tool as-is. We recommend using'));
console.log(chalk.reset('the appropriate guide(s) to upgrade to Sails v0.12 first.'));
console.log(chalk.gray('For more assistance, visit '+chalk.underline('http://sailsjs.com/support')+' or, if'));
console.log(chalk.gray('you\'re using Sails Flagship, '+chalk.underline('https://flagship.sailsjs.com')+'.'));
return process.exit(1);
case 'E_NO_PACKAGE_JSON':
case 'E_NO_SAILS_DEP':
console.log('--');
console.log(chalk.red(e.message));
return process.exit(1);
default:
console.log('--');
console.log(chalk.bold('Oops, something unexpected happened:'));
console.log(chalk.red(e.stack));
console.log('--');
console.log('Please read the error message above and troubleshoot accordingly.');
console.log('(You can report suspected bugs at '+chalk.underline('http://sailsjs.com/bugs')+'.)');
return process.exit(1);
}
}
}
// Attempt to require the upgrade tool.
var generator;
try {
var requirePath = path.resolve(process.cwd(), 'node_modules/@sailshq/upgrade');
generator = require(requirePath);
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
console.log(chalk.blue.bold('Could not find the `@sailshq/upgrade` package in your local app folder.'));
console.log('Please run `npm install @sailshq/upgrade` and try again.');
console.log(chalk.gray('(Or just use the Sails v1.0.x upgrade guide on sailsjs.com.)'));
console.log();
return process.exit(1);
}//-•
// Some other unexpected error from within this package:
console.log(chalk.bold('Oops, something unexpected happened:'));
console.log(chalk.red(e.stack));
console.log('--');
console.log('Please report this bug at '+chalk.underline('https://flagship.sailsjs.com')+'.');
process.exit(1);
}//</catch>
// Build initial scope
var scope = {
rootPath: process.cwd(),
sailsRoot: path.resolve(__dirname, '..'),
generatorType: 'upgrade',
modules: { upgrade: generator },
};
// 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);
// 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;
return sailsgen(scope, {
// Handle unexpected errors.
error: function (err) {
console.log(chalk.bold('Oops, something unexpected happened:'));
console.log(chalk.red(err.stack));
console.log('--');
console.log('Please report this bug at '+chalk.underline('https://flagship.sailsjs.com')+'.');
return process.exit(1);
},//</on error :: sailsGen()>
// Attend to invalid usage.
invalid: function (err) {
// If this is an Error, don't bother logging the other details, just log the `.message`.
// (This is purely for readability.)
if (_.isError(err)) {
console.log(err.message);
}
else {
console.log(err);
}
console.log('--');
console.log('For assistance, visit '+chalk.underline('https://flagship.sailsjs.com')+'.');
return process.exit(1);
},//</on invalid :: sailsGen()>
success: function() {
// Good to go.
}
});
};