2020-07-31 03:51:41 +03:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-12-29 04:38:00 +03:00
|
|
|
// @ts-check
|
|
|
|
|
2020-07-31 03:51:41 +03:00
|
|
|
const path = require('path');
|
2021-01-06 02:25:25 +03:00
|
|
|
const fs = require('fs');
|
2021-01-08 02:00:04 +03:00
|
|
|
const Documentation = require('./documentation');
|
|
|
|
const { parseApi } = require('./api_parser');
|
2020-07-31 03:51:41 +03:00
|
|
|
const PROJECT_DIR = path.join(__dirname, '..', '..');
|
|
|
|
|
2020-12-27 01:31:41 +03:00
|
|
|
{
|
2021-01-08 02:00:04 +03:00
|
|
|
const documentation = parseApi(path.join(PROJECT_DIR, 'docs', 'src', 'api'));
|
|
|
|
documentation.setLinkRenderer(item => {
|
|
|
|
const { clazz, param, option } = item;
|
2020-12-29 23:12:46 +03:00
|
|
|
if (param)
|
|
|
|
return `\`${param}\``;
|
|
|
|
if (option)
|
|
|
|
return `\`${option}\``;
|
|
|
|
if (clazz)
|
|
|
|
return `\`${clazz.name}\``;
|
|
|
|
});
|
2021-01-08 02:00:04 +03:00
|
|
|
documentation.generateSourceCodeComments();
|
|
|
|
const result = serialize(documentation);
|
2021-01-06 02:25:25 +03:00
|
|
|
fs.writeFileSync(path.join(PROJECT_DIR, 'api.json'), JSON.stringify(result));
|
2020-12-27 01:31:41 +03:00
|
|
|
}
|
2020-07-31 03:51:41 +03:00
|
|
|
|
2020-12-29 23:12:46 +03:00
|
|
|
/**
|
|
|
|
* @param {Documentation} documentation
|
|
|
|
*/
|
2020-07-31 03:51:41 +03:00
|
|
|
function serialize(documentation) {
|
2021-01-05 20:42:49 +03:00
|
|
|
return documentation.classesArray.map(serializeClass);
|
2020-07-31 03:51:41 +03:00
|
|
|
}
|
|
|
|
|
2020-12-29 23:12:46 +03:00
|
|
|
/**
|
|
|
|
* @param {Documentation.Class} clazz
|
|
|
|
*/
|
2020-07-31 03:51:41 +03:00
|
|
|
function serializeClass(clazz) {
|
2021-03-02 00:00:01 +03:00
|
|
|
const result = { name: clazz.name, spec: clazz.spec };
|
2020-10-03 05:19:19 +03:00
|
|
|
if (clazz.extends)
|
|
|
|
result.extends = clazz.extends;
|
2021-01-09 02:00:14 +03:00
|
|
|
result.langs = clazz.langs;
|
2021-01-16 03:01:41 +03:00
|
|
|
if (result.langs && result.langs.types) {
|
|
|
|
for (const key in result.langs.types)
|
|
|
|
result.langs.types[key] = serializeType(result.langs.types[key]);
|
|
|
|
}
|
2020-11-03 05:31:32 +03:00
|
|
|
if (clazz.comment)
|
|
|
|
result.comment = clazz.comment;
|
2021-01-05 04:59:23 +03:00
|
|
|
result.members = clazz.membersArray.map(serializeMember);
|
2020-07-31 03:51:41 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-12-29 23:12:46 +03:00
|
|
|
/**
|
|
|
|
* @param {Documentation.Member} member
|
|
|
|
*/
|
2020-07-31 03:51:41 +03:00
|
|
|
function serializeMember(member) {
|
2020-12-29 23:12:46 +03:00
|
|
|
const result = /** @type {any} */ ({ ...member });
|
2020-09-29 23:48:24 +03:00
|
|
|
sanitize(result);
|
2021-01-05 04:59:23 +03:00
|
|
|
result.args = member.argsArray.map(serializeProperty);
|
2020-07-31 03:51:41 +03:00
|
|
|
if (member.type)
|
|
|
|
result.type = serializeType(member.type)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function serializeProperty(arg) {
|
|
|
|
const result = { ...arg };
|
2020-09-29 23:48:24 +03:00
|
|
|
sanitize(result);
|
2020-07-31 03:51:41 +03:00
|
|
|
if (arg.type)
|
|
|
|
result.type = serializeType(arg.type)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-09-29 23:48:24 +03:00
|
|
|
function sanitize(result) {
|
|
|
|
delete result.args;
|
|
|
|
delete result.argsArray;
|
2020-12-29 23:12:46 +03:00
|
|
|
delete result.clazz;
|
2021-01-30 03:02:17 +03:00
|
|
|
delete result.enclosingMethod;
|
2020-09-29 23:48:24 +03:00
|
|
|
}
|
|
|
|
|
2021-01-05 04:59:23 +03:00
|
|
|
/**
|
|
|
|
* @param {Documentation.Type} type
|
|
|
|
*/
|
2020-07-31 03:51:41 +03:00
|
|
|
function serializeType(type) {
|
2021-01-05 04:59:23 +03:00
|
|
|
/** @type {any} */
|
2020-07-31 03:51:41 +03:00
|
|
|
const result = { ...type };
|
2021-01-05 04:59:23 +03:00
|
|
|
if (type.properties)
|
|
|
|
result.properties = type.properties.map(serializeProperty);
|
|
|
|
if (type.union)
|
|
|
|
result.union = type.union.map(serializeType);
|
|
|
|
if (type.templates)
|
|
|
|
result.templates = type.templates.map(serializeType);
|
|
|
|
if (type.args)
|
|
|
|
result.args = type.args.map(serializeType);
|
|
|
|
if (type.returnType)
|
|
|
|
result.returnType = serializeType(type.returnType);
|
2020-07-31 03:51:41 +03:00
|
|
|
return result;
|
|
|
|
}
|