UNPKG

3.66 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' || 'ENOENT') {
32 var versions = util.getInstalledVersions();
33 // set active version as latest installed version
34 if (getLatest) {
35 latest = versions[0];
36 installBin = util.getInstallBinary(opts, latest);
37 if (installBin) {
38 debug('making %s your active version, dir %s',latest,installBin);
39 util.writeVersion(latest);
40 console.log(chalk.yellow(latest)+" is now your active version");
41 }
42 // json output
43 } else if ('json' === util.parseOpts(opts).o) {
44 obj = util.getVersionJson(versions);
45 console.log(JSON.stringify(obj, null, '\t'));
46 // display installed versions
47 } else {
48 console.log(chalk.white.bold.underline('The following versions are available offline:\n'));
49 util.listVersions(opts, versions);
50 console.log('');
51 }
52 process.exit(0);
53 }
54 return callback(errorlib.createError('com.appcelerator.install.use.download.error',err.message||String(err)));
55 }
56 if (!result) { return callback(errorlib.createError('com.appcelerator.install.download.server.unavailable')); }
57 debug('versions returned from registry:', result);
58 if (result && result.key) {
59 result = result[result.key];
60 }
61 if (getLatest) {
62 if (!result.length) {
63 console.log(chalk.red('No versions are current deployed. Please check back in a few minutes.'));
64 process.exit(1);
65 }
66 var latest = result[0].version;
67 return use(opts, callback, latest);
68 }
69 var theversion = util.getActiveVersion();
70 // Is this JSON output ?
71 if ('json' === util.parseOpts(opts).o) {
72 obj = util.getVersionJson(result);
73 console.log(JSON.stringify(obj, null, '\t'));
74 } else if (result) {
75 console.log(chalk.white.bold.underline('The following versions are available:\n'));
76 util.listVersions(opts, result);
77 console.log('');
78 }
79 else {
80 console.log('No results returned. Make sure you are online.');
81 }
82 process.exit(0);
83 });
84 }
85 else {
86 var version = opts.version = wantVersion || args[1];
87 // see if we have this version
88 installBin = util.getInstallBinary(opts, version);
89 // we already have this version, so we just need to write our version file and exit
90 if (installBin && !opts.force) {
91 debug('making %s your active version, dir %s',version,installBin);
92 util.writeVersion(version);
93 console.log(chalk.yellow(version)+" is now your active version");
94 process.exit(0);
95 }
96 opts.use = true;
97 // otherwise, we didn't find it, fall through so we can install it
98 return callback();
99 }
100}
101
102module.exports = use;