all files / lib/ use.js

48.65% Statements 36/74
33.96% Branches 18/53
80% Functions 4/5
48.65% Lines 36/74
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                                                                                                                                                                                       
// jscs:disable jsDoc
/**
 * This code is closed source and Confidential and Proprietary to
 * Appcelerator, Inc. All Rights Reserved.  This code MUST not be
 * modified, copied or otherwise redistributed without express
 * written permission of Appcelerator. This file is licensed as
 * part of the Appcelerator Platform and governed under the terms
 * of the Appcelerator license agreement.
 */
var util = require('./util'),
	errorlib = require('./error'),
	debug = require('debug')('appc:use'),
	chalk = require('chalk');
 
function use(opts, callback, wantVersion) {
	var args = util.parseArgs(opts),
		obj,
		getLatest = !wantVersion && args.length > 1 && args[1] === 'latest';
 
	debug('use called with args %o, getLatest=%d', args, getLatest);
	Iif (args.length > 1 && !getLatest) {
		var version = opts.version = wantVersion || args[1];
		// see if we have this version
		var installBin = util.getInstallBinary(opts, version);
		// we already have this version, so we just need to write our version file and exit
		if (installBin && !opts.force) {
			debug('making %s your active version, dir %s', version, installBin);
			util.writeVersion(version);
			console.log(chalk.yellow(version) + ' is now your active version');
			process.exit(0);
		}
		opts.use = true;
		// otherwise, we didn't find it, fall through so we can install it
		return callback();
	}
 
	util.startSpinner();
	var latestUrl = util.makeURL(opts, '/api/appc/latest');
	util.requestJSON(latestUrl, function (err, latestVersion) {
		var apiPath = '/api/appc/list';
		Iif (opts.prerelease) {
			apiPath += '?prerelease=true';
		}
		var url = util.makeURL(opts, apiPath);
		util.requestJSON(url, function (err, result) {
			util.stopSpinner();
			Iif (err) {
				// if already an AppCError just return it
				if (err.name === 'AppCError') {
					return callback(err);
				}
				handleOffline(err, opts, getLatest);
				return callback(errorlib.createError('com.appcelerator.install.use.download.error', err.message || String(err)));
			}
			Iif (!result) {
				return callback(errorlib.createError('com.appcelerator.install.download.server.unavailable'));
			}
			debug('versions returned from registry:', result);
			Eif (result && result.key) {
				result = result[result.key];
			}
			opts.latest = findLatest(result, latestVersion);
			Iif (getLatest) {
				if (!result.length) {
					console.log(chalk.red('No versions are current deployed. Please check back in a few minutes.'));
					process.exit(1);
				}
				return use(opts, callback, opts.latest);
			}
			// Is this JSON output ?
			Iif ('json' === util.parseOpts(opts).o) {
				obj = util.getVersionJson(opts, result);
				console.log(JSON.stringify(obj, null, '\t'));
			} else Eif (result) {
				console.log(chalk.white.bold.underline('The following versions are available:\n'));
				util.listVersions(opts, result);
				console.log('');
			} else {
				console.log('No results returned. Make sure you are online.');
			}
			process.exit(0);
		});
	});
}
 
function handleOffline(err, opts, getLatest) {
	// looks like we are offline
	if (err.code === 'ENOTFOUND' || err.code === 'ENOENT') {
		var versions = util.getInstalledVersions();
		// set active version as latest installed version
		if (getLatest) {
			var latest = versions[0];
			var installBin = util.getInstallBinary(opts, latest);
			if (installBin) {
				debug('making %s your active version, dir %s', latest, installBin);
				util.writeVersion(latest);
				console.log(chalk.yellow(latest) + ' is now your active version');
			}
			// json output
		} else if ('json' === util.parseOpts(opts).o) {
			var obj = util.getVersionJson(versions);
			console.log(JSON.stringify(obj, null, '\t'));
			// display installed versions
		} else {
			console.log(chalk.white.bold.underline('The following versions are available offline:\n'));
			util.listVersions(opts, versions);
			console.log('');
		}
		process.exit(0);
	}
}
 
function findLatest(listResult, latestVerResult) {
	var latest = listResult[0] && listResult[0].version;
	// Fetch the details from latestVersion payload.
	Eif (latestVerResult) {
		Eif (latestVerResult.key) {
			latestVerResult = latestVerResult[latestVerResult.key];
		}
		Eif (latestVerResult.length > 0) {
			latest = latestVerResult[0].version;
		}
	}
	return latest;
}
 
module.exports = use;