UNPKG

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