UNPKG

4.59 kBJavaScriptView Raw
1// jscs:disable jsDoc
2/**
3 * This code is closed source and Confidential and Proprietary to
4 * Appcelerator, Inc. All Rights Reserved. This code MUST not be
5 * modified, copied or otherwise redistributed without express
6 * written permission of Appcelerator. This file is licensed as
7 * part of the Appcelerator Platform and governed under the terms
8 * of the Appcelerator license agreement.
9 */
10var util = require('./util'),
11 errorlib = require('./error'),
12 debug = require('debug')('appc:use'),
13 chalk = require('chalk');
14
15function use(opts, callback, wantVersion) {
16 var args = util.parseArgs(opts),
17 obj,
18 getLatest = !wantVersion && args.length > 1 && args[1] === 'latest';
19
20 debug('use called with args %o, getLatest=%d', args, getLatest);
21 if (args.length > 1 && !getLatest) {
22 var version = opts.version = wantVersion || args[1];
23 // see if we have this version
24 var installBin = util.getInstallBinary(opts, version);
25 // we already have this version, so we just need to write our version file and exit
26 if (installBin && !opts.force) {
27 debug('making %s your active version, dir %s', version, installBin);
28 util.writeVersion(version);
29 util.killDaemon(version, installBin);
30 console.log(chalk.yellow(version) + ' is now your active version');
31 process.exit(0);
32 }
33 opts.use = true;
34 // otherwise, we didn't find it, fall through so we can install it
35 return callback();
36 }
37
38 util.startSpinner();
39 var latestUrl = util.makeURL(opts, '/api/appc/latest');
40 util.requestJSON({ url: latestUrl }, function (err, latestVersion) {
41 if (err) {
42 if (err.name === 'AppCError') {
43 return callback(err);
44 }
45 handleOffline(err, opts, getLatest);
46 return callback(errorlib.createError('com.appcelerator.install.use.download.error', err.message || String(err)));
47 }
48
49 var apiPath = '/api/appc/list';
50 if (opts.prerelease) {
51 apiPath += '?prerelease=true';
52 }
53 var url = util.makeURL(opts, apiPath);
54 util.requestJSON({ url: url }, function (err, result) {
55 util.stopSpinner();
56 if (err) {
57 // if already an AppCError just return it
58 if (err.name === 'AppCError') {
59 return callback(err);
60 }
61 handleOffline(err, opts, getLatest);
62 return callback(errorlib.createError('com.appcelerator.install.use.download.error', err.message || String(err)));
63 }
64 if (!result) {
65 return callback(errorlib.createError('com.appcelerator.install.download.server.unavailable'));
66 }
67 debug('versions returned from registry:', result);
68 if (result && result.key) {
69 result = result[result.key];
70 }
71 opts.latest = findLatest(result, latestVersion);
72 if (getLatest) {
73 if (!result.length) {
74 console.log(chalk.red('No versions are current deployed. Please check back in a few minutes.'));
75 process.exit(1);
76 }
77 return use(opts, callback, opts.latest);
78 }
79 // Is this JSON output ?
80 if (opts.output === 'json') {
81 obj = util.getVersionJson(opts, result);
82 console.log(JSON.stringify(obj, null, '\t'));
83 } else if (result) {
84 console.log(chalk.white.bold.underline('The following versions are available:\n'));
85 util.listVersions(opts, result);
86 console.log('');
87 } else {
88 console.log('No results returned. Make sure you are online.');
89 }
90 process.exit(0);
91 });
92 });
93}
94
95function handleOffline(err, opts, getLatest) {
96 // looks like we are offline
97 if (err.code === 'ENOTFOUND' || err.code === 'ENOENT') {
98 var versions = util.getInstalledVersions();
99 // set active version as latest installed version
100 if (getLatest) {
101 var latest = versions[0];
102 var installBin = util.getInstallBinary(opts, latest);
103 if (installBin) {
104 debug('making %s your active version, dir %s', latest, installBin);
105 util.writeVersion(latest);
106 console.log(chalk.yellow(latest) + ' is now your active version');
107 }
108 // json output
109 } else if (util.parseOpts(opts).o === 'json') {
110 var obj = util.getVersionJson(versions);
111 console.log(JSON.stringify(obj, null, '\t'));
112 // display installed versions
113 } else {
114 console.log(chalk.white.bold.underline('The following versions are available offline:\n'));
115 util.listVersions(opts, versions);
116 console.log('');
117 }
118 process.exit(0);
119 }
120}
121
122function findLatest(listResult, latestVerResult) {
123 var latest = listResult[0] && listResult[0].version;
124 // Fetch the details from latestVersion payload.
125 if (latestVerResult) {
126 if (latestVerResult.key) {
127 latestVerResult = latestVerResult[latestVerResult.key];
128 }
129 if (latestVerResult.length > 0) {
130 latest = latestVerResult[0].version;
131 }
132 }
133 return latest;
134}
135
136module.exports = use;