-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathjson-raw_generator.js
More file actions
320 lines (300 loc) · 8.14 KB
/
json-raw_generator.js
File metadata and controls
320 lines (300 loc) · 8.14 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/**
* Copyright (c) 2015-2017 Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License.
*
* Script to convert data to JSON format for third-party extensions
*/
'use strict';
const common = require('../lib/common.js');
const assert = common.assertObjectKey;
let doc = {};
// eslint-disable-next-line security/detect-child-process
const exec = require('child_process').execSync;
/**
* Export deprecated field
* @param {object} api api object
* @return {object|null}
*/
function exportDeprecated(api) {
if ('deprecated' in api && api.deprecated) {
return {
notes: api.deprecated.notes || '',
since: api.deprecated.since,
removed: api.deprecated.removed
};
}
return null;
}
/**
* Export summary field
* @param {object} api api object
* @return {string} HTML-ified summary text
*/
function exportSummary(api) {
let rv = '';
if ('summary' in api && api.summary) {
rv = api.summary;
}
return rv;
}
/**
* Export examples field
* @param {object} api api object
* @return {object[]}
*/
function exportExamples(api) {
const rv = [];
if ('examples' in api && api.examples.length > 0) {
api.examples.forEach(function (example) {
const code = example.example;
rv.push({ description: example.title, code: code });
});
}
return rv;
}
/**
* Export method parameters or event properties field
* @param {object[]} apis api tree
* @param {string} type type name
* @return {object[]}
*/
function exportParams(apis, type) {
const rv = [];
if (apis) {
apis.forEach(function (member) {
const annotatedMember = {};
annotatedMember.name = member.name;
if (assert(member, 'deprecated')) {
annotatedMember.deprecated = exportDeprecated(member);
}
annotatedMember.summary = exportSummary(member);
if (assert(member, 'description')) {
annotatedMember.description = exportDescription(member);
}
annotatedMember.type = member.type || 'String';
if (type === 'parameters') {
// optional/repeatable are false if omitted
if (assert(member, 'optional') && member.optional) {
annotatedMember.optional = member.optional;
}
if (assert(member, 'repeatable') && member.repeatable) {
annotatedMember.repeatable = member.repeatable;
}
}
rv.push(annotatedMember);
});
}
return rv;
}
/**
* Export description field
* @param {object} api api object
* @return {string|null} HTML-ified description
*/
function exportDescription(api) {
if ('description' in api && api.description) {
return api.description;
}
return null;
}
/**
* Export returns field
* @param {object} api api object
* @return {object|object[]}
*/
function exportReturnTypes(api) {
const rv = [];
if (assert(api, 'returns')) {
if (!Array.isArray(api.returns)) {
api.returns = [ api.returns ];
}
api.returns.forEach(function (ret) {
const x = {};
if (assert(ret, 'summary')) {
x.summary = ret.summary;
}
x.type = ret.type;
rv.push(x);
});
} else {
rv.push({ type: 'void' });
}
if (rv.length === 1) {
return rv[0];
}
return rv;
}
/**
* Export since field
* @param {object} api api object
* @return {object[]}
*/
function exportPlatforms(api) {
const rv = [];
for (const platform in api.since) {
rv.push({
since: api.since[platform],
name: platform
});
}
return rv;
}
/**
* Export members API
* @param {object} api api object
* @param {string} type type name
* @return {object[]}
*/
function exportAPIs(api, type) {
var rv = [];
var x = 0;
var member = {};
var annotatedMember = {};
if (type in api) {
for (x = 0; x < api[type].length; x++) {
member = api[type][x];
if (member.__hide) {
continue;
}
if (member.__accessor) {
continue;
}
annotatedMember.name = member.name;
if (assert(member, 'deprecated')) {
annotatedMember.deprecated = exportDeprecated(member);
}
annotatedMember.summary = exportSummary(member);
if (assert(member, 'description')) {
annotatedMember.description = exportDescription(member);
}
annotatedMember.platforms = exportPlatforms(member);
if (member.__inherits !== api.name) {
annotatedMember.inherits = member.__inherits;
}
switch (type) {
case 'events':
if (member.properties) {
if ('Titanium.Event' in doc) {
member.properties = member.properties.concat(doc['Titanium.Event'].properties);
}
annotatedMember.properties = exportParams(member.properties, 'properties');
}
break;
case 'methods':
if (assert(member, 'examples')) {
annotatedMember.examples = exportExamples(member);
}
if (assert(member, 'parameters')) {
annotatedMember.parameters = exportParams(member.parameters, 'parameters');
}
annotatedMember.returns = exportReturnTypes(member);
break;
case 'properties':
if (assert(member, 'examples')) {
annotatedMember.examples = exportExamples(member);
}
annotatedMember.type = member.type || 'String';
if (assert(member, 'availability')) {
annotatedMember.availability = member.availability;
}
if (assert(member, 'default')) {
annotatedMember['default'] = member['default'];
}
if (assert(member, 'optional')) {
annotatedMember.optional = member.optional;
}
if (assert(member, 'permission')) {
annotatedMember.permission = member.permission;
}
if (assert(member, 'value')) {
annotatedMember.value = member.value;
}
if (assert(member, 'constants')) {
annotatedMember.constants = member.constants;
}
}
rv.push(annotatedMember);
member = annotatedMember = {};
}
}
return rv;
}
const REPO_URLS = new Map();
/**
* Convert a filepath on disk to the URL of the repo on github for that file
* @param {string} filepath file path (should be root of git clone)
* @returns {string}
*/
function getRepoUrl(filepath) {
if (REPO_URLS.has(filepath)) {
return REPO_URLS.get(filepath);
}
const stdout = exec('git config --get remote.origin.url', { cwd: filepath }).toString().trim();
const m = stdout.match(/^(git@|https:\/\/)github.com[:/]([\w-]+)\/([\w_\-.]+?)(?:\.git)?$/);
if (!m) {
console.log(`Unable to pull github org/repo from url: ${stdout}`);
const result = 'https://github.com/tidev/titanium-sdk/edit/main/';
REPO_URLS.set(filepath, result);
return result;
}
const org = m[2];
const repo = m[3];
const result = `https://github.com/${org}/${repo}/edit/master/`;
REPO_URLS.set(filepath, result);
return result;
}
function editUrl(api) {
const file = api.__file;
// we need to strip path to remove al the way up to /apidoc to get the relative path
const apiDocIndex = file.indexOf('apidoc/');
const relPath = file.slice(apiDocIndex);
const basePath = file.slice(0, apiDocIndex);
const repoUrl = getRepoUrl(basePath);
return `${repoUrl}${relPath}`;
}
/**
* Annotate JSON data for consumption by third-party tools
* @param {object[]} apis api tree
* @return {object[]}
*/
exports.exportData = function exportJSON(apis) {
const rv = {};
doc = apis; // TODO make doc a field on a type, rather than this weird file-global!
common.log(common.LOG_INFO, 'JSON-RAW generator starting...');
for (const className in apis) {
const cls = apis[className];
const annotatedClass = {
name: cls.name,
summary: exportSummary(cls),
extends: cls['extends'] || 'Object',
platforms: exportPlatforms(cls),
type: cls.__subtype || 'object',
editUrl: editUrl(cls)
};
// Avoid setting null/empty array values - trims down large filesize
if (assert(cls, 'deprecated')) {
annotatedClass.deprecated = exportDeprecated(cls);
}
if (assert(cls, 'description')) {
annotatedClass.description = exportDescription(cls);
}
if (assert(cls, 'events')) {
annotatedClass.events = exportAPIs(cls, 'events');
}
if (assert(cls, 'examples')) {
annotatedClass.examples = exportExamples(cls);
}
if (assert(cls, 'methods')) {
annotatedClass.methods = exportAPIs(cls, 'methods');
}
if (assert(cls, 'properties')) {
annotatedClass.properties = exportAPIs(cls, 'properties');
}
if (~[ 'proxy', 'view' ].indexOf(annotatedClass.type)) {
annotatedClass.subtype = annotatedClass.type;
annotatedClass.type = 'object';
}
rv[className] = annotatedClass;
}
return rv;
};