Skip to content

Commit

Permalink
Improvements to Sails CLI + wired in the upgrade checker.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Mar 3, 2017
1 parent 7c64e0c commit 62aecf6
Show file tree
Hide file tree
Showing 3 changed files with 289 additions and 3 deletions.
221 changes: 221 additions & 0 deletions bin/sails-upgrade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/**
* 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 () {

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(chalk.gray('--'));
console.log(chalk.gray('Note: upgrade tool is currently available only in Sails Flagship.'));
console.log(chalk.gray('Configure your account: '+chalk.underline('https://flagship.sailsjs.com/settings')+''));
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.
}
});
};
65 changes: 62 additions & 3 deletions bin/sails-www.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
* Module dependencies
*/

var nodepath = require('path');
var path = require('path');
var _ = require('@sailshq/lodash');
var CaptainsLog = require('captains-log');
var Process = require('machinepack-process');
var chalk = require('chalk');
var flaverr = require('flaverr');
var rconf = require('../lib/app/configuration/rc')();



/**
* `sails www`
*
Expand All @@ -20,10 +21,68 @@ var rconf = require('../lib/app/configuration/rc')();
*/

module.exports = function() {

// Check compatibility
try {
var pathToLocalPackageJson = path.resolve(process.cwd(), 'package.json');
var packageJson;
try {
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.\nAre you sure you\'re in the root directory of a Sails app?'));
}

var shGruntDepSVR = packageJson.dependencies['sails-hook-grunt'];
if (!shGruntDepSVR) {
throw flaverr('E_NO_SH_GRUNT_DEP', new Error('This app\'s package.json file does not declare `sails-hook-grunt` as a dependency.\nAre you sure this is a Sails v1.0 app that is using Grunt?'));
}

} catch (e) {
switch (e.code) {
case 'E_NO_PACKAGE_JSON':
case 'E_NO_SAILS_DEP':
console.log('--');
console.log(chalk.red(e.message));
return process.exit(1);
case 'E_NO_SH_GRUNT_DEP':
console.log('--');
console.log(chalk.red(e.message));
console.log(chalk.gray('(Maybe try running `npm install sails-hook-grunt --save`?)'));
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);
}
}



var log = CaptainsLog(rconf.log);

// The destination path.
var wwwPath = nodepath.resolve(process.cwd(), './www');
var wwwPath = path.resolve(process.cwd(), './www');

// Determine the appropriate Grunt task to run based on `process.env.NODE_ENV`, `rconf.prod`, and `rconf.environment`.
var overrideGruntTask;
Expand Down
6 changes: 6 additions & 0 deletions bin/sails.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ cmd.description('');
cmd.usage('[something]');
cmd.action(require('./sails-generate'));

// $ sails upgrade
cmd = program.command('upgrade');
cmd.unknownOption = NOOP;
cmd.description('');
cmd.action(require('./sails-upgrade'));

// $ sails deploy
cmd = program.command('deploy');
// cmd.option('--dry');
Expand Down

0 comments on commit 62aecf6

Please sign in to comment.