1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var util = require('./util'),
|
10 | errorlib = require('./error'),
|
11 | debug = require('debug')('appc:use'),
|
12 | chalk = require('chalk');
|
13 |
|
14 | function 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 |
|
27 | if (err.name === 'AppCError') {
|
28 | return callback(err);
|
29 | }
|
30 |
|
31 | if (err.code === 'ENOTFOUND' || 'ENOENT') {
|
32 | var versions = util.getInstalledVersions();
|
33 |
|
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 |
|
43 | } else if ('json' === util.parseOpts(opts).o) {
|
44 | obj = util.getVersionJson(versions);
|
45 | console.log(JSON.stringify(obj, null, '\t'));
|
46 |
|
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 |
|
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 |
|
88 | installBin = util.getInstallBinary(opts, version);
|
89 |
|
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 |
|
98 | return callback();
|
99 | }
|
100 | }
|
101 |
|
102 | module.exports = use;
|