forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocs.js
More file actions
175 lines (150 loc) · 4.47 KB
/
docs.js
File metadata and controls
175 lines (150 loc) · 4.47 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var fs = require('fs'),
path = require('path'),
argv = require('optimist').argv,
Q = require('q'),
prompt = require('prompt'),
Task = require('./task').Task,
IonicStats = require('./stats').IonicStats,
_ = require('underscore'),
IonicProject = require('./project'),
IonicInfo = require('./info').IonicTask,
IonicDocs = require('./resources/docs.json'),
COLUMN_LIMIT = 3;
var Docs = module.exports;
var IonicTask = function() {};
IonicTask.prototype = new Task();
function sanitizeUrl(url) {
return url.replace(/\$/g, '%24');
}
Docs.list = function list() {
try {
var Table = require('cli-table');
var docsList = IonicDocs.api;
// var i = 0;
console.log('Help Topics'.yellow);
_.each(docsList, function(doc) {
var table = new Table({ head: [doc.id] });
var count = 0;
var row = [];
_.each(doc.docs, function(docItem) {
var addDoc = docItem.replace('$', '');
if(count >= COLUMN_LIMIT) {
table.push(row);
row = [];
count = 0;
}
row.push(addDoc);
count++;
});
table.push(row);
console.log(table.toString());
});
} catch (ex) {
console.log('Error listing docs:', ex);
}
console.log('Type "ionic docs <docname>" to open the help document for that doc.'.blue.bold);
};
Docs.openDefault = function openDefault() {
var Info = require('ionic-app-lib').info;
var envInfo = Info.gatherInfo();
var ionicVersion = envInfo.ionic;
var url = 'http://ionicframework.com/docs/';
if (ionicVersion) {
url += ionicVersion + '/api';
console.log('Ionic version:', ionicVersion);
}
return require('open')(sanitizeUrl(url));
};
Docs.lookUpCommand = function lookUpCommand(helpDoc) {
// console.log('Going thru doc commands', helpDoc)
//Go through all the different help topics
var docsList = IonicDocs.api;
helpDoc = helpDoc.replace(/-[a-zA-Z]/g, function(match) {
return match[1].toUpperCase();
});
var docs = _.map(docsList, function(doc) {
return _.map(doc.docs, function(docItem) {
return {
topic: doc.id,
doc: docItem
};
});
});
docs = _.flatten(docs);
var match = _.find(docs, { doc: helpDoc }) || _.find(docs, { doc: '$' + helpDoc});
if (match) {
return openDoc(match.topic, match.doc);
} else if (helpDoc.length >= 3) {
var lowerHelp = helpDoc.toLowerCase();
var matches = docs
.filter(function(item) {
var lowerDoc = item.doc.toLowerCase();
return lowerDoc.indexOf(lowerHelp) > -1 ||
lowerHelp.indexOf(lowerDoc) > -1;
})
.sort(function(a,b) {
return a.doc.length < b.doc.length ? -1 : 1;
})
.slice(0,3);
if (matches.length === 0) return console.log('No matching docs found for "' + helpDoc.cyan + '".');
prompt.message = 'Did you mean ';
if (matches.length > 1) {
prompt.message += matches.map(function(item, i) {
var str = '';
if (i > 0) {
if (i === matches.length - 1) {
str = ' or ';
} else {
str = ', ';
}
}
str += '(' + (i + 1 + '').cyan + ') ' + item.doc;
return str;
}).join('');
} else {
prompt.message += matches[0].doc;
}
prompt.message += '?';
prompt.start();
prompt.get({
name: 'choice',
default: matches.length === 1 ? 'yes' : '1',
}, function(err, result) {
var num;
if (matches.length == 1) {
if (result.choice.match(/^y/i, result.choice.trim())) {
num = 1;
}
} else {
num = parseInt(result.choice.trim());
}
if (!isNaN(num) && num > 0 && num <= matches.length) {
num--; // chioce (1-based) to index (0-based)
return openDoc(matches[num].topic, matches[num].doc);
}
});
}
function openDoc(topic, doc) {
var url = sanitizeUrl('http://ionicframework.com/docs/api/' + topic + '/' + doc);
console.log('Opening Ionic document:'.green.bold, url.green.bold);
return require('open')(url);
}
};
IonicTask.prototype.run = function run(ionic) {
var self = this;
this.ionic = ionic;
var docCmd = argv._[1];
// console.log('docCmd', docCmd);
if(docCmd == 'ls') {
Docs.list();
} else if (!docCmd) {
// console.log('openDefault')
Docs.openDefault();
} else {
// console.log('look up command', docCmd)
//Look up what it was
Docs.lookUpCommand(docCmd);
}
IonicStats.t();
};
exports.IonicTask = IonicTask;