@capacitor/docgen
Advanced tools
Comparing version 0.0.15 to 0.0.16
@@ -43,2 +43,3 @@ "use strict"; | ||
content = replaceMarkdownDocsIndex(content, data); | ||
content = replaceMarkdownDocsConfig(content, data); | ||
content = replaceMarkdownDocsApi(content, data); | ||
@@ -52,2 +53,4 @@ return content; | ||
const API_END = `</docgen-api>`; | ||
const CONFIG_START = `<docgen-config`; | ||
const CONFIG_END = `</docgen-config>`; | ||
const UPDATE_MSG = `<!--Update the source file JSDoc comments and rerun docgen to update the docs below-->`; | ||
@@ -68,2 +71,16 @@ function replaceMarkdownDocsIndex(content, data) { | ||
} | ||
function replaceMarkdownDocsConfig(content, data) { | ||
const startOuterIndex = content.indexOf(CONFIG_START); | ||
if (startOuterIndex > -1) { | ||
const endInnerIndex = content.indexOf(CONFIG_END); | ||
if (endInnerIndex > -1) { | ||
const inner = content.substring(startOuterIndex + CONFIG_START.length); | ||
const startInnerIndex = startOuterIndex + CONFIG_START.length + inner.indexOf('>') + 1; | ||
const start = content.substring(0, startInnerIndex); | ||
const end = content.substring(endInnerIndex); | ||
return `${start}\n${UPDATE_MSG}\n\n${markdownConfig(data)}\n\n${end}`; | ||
} | ||
} | ||
return content; | ||
} | ||
function replaceMarkdownDocsApi(content, data) { | ||
@@ -167,2 +184,53 @@ const startOuterIndex = content.indexOf(API_START); | ||
} | ||
function markdownConfig(data) { | ||
const o = []; | ||
if (data.pluginConfigs) { | ||
data.pluginConfigs.forEach(c => { | ||
o.push(configInterfaceTable(data, c)); | ||
o.push(buildExamples(c)); | ||
}); | ||
} | ||
return o.join('\n'); | ||
} | ||
function buildExamples(c) { | ||
const o = []; | ||
o.push(`### Examples`); | ||
o.push(``); | ||
o.push(`In \`capacitor.config.json\`:`); | ||
o.push(``); | ||
o.push(`\`\`\`json`); | ||
o.push(`{`); | ||
o.push(` "plugins": {`); | ||
o.push(` "${c.name}": {`); | ||
c.properties.forEach((p, i) => { | ||
var _a; | ||
o.push(` "${p.name}": ${(_a = p.tags.find(t => t.name === 'example')) === null || _a === void 0 ? void 0 : _a.text}${i === c.properties.length - 1 ? '' : ','}`); | ||
}); | ||
o.push(` }`); | ||
o.push(` }`); | ||
o.push(`}`); | ||
o.push(`\`\`\``); | ||
o.push(``); | ||
o.push(`In \`capacitor.config.ts\`:`); | ||
o.push(``); | ||
o.push(`\`\`\`ts`); | ||
o.push(`/// <reference types="@capacitor/${parse_1.slugify(c.name.replace(/([a-z])([A-Z])/g, '$1 $2'))}" />`); | ||
o.push(``); | ||
o.push(`import { CapacitorConfig } from '@capacitor/cli';`); | ||
o.push(``); | ||
o.push(`const config: CapacitorConfig = {`); | ||
o.push(` plugins: {`); | ||
o.push(` ${c.name}: {`); | ||
c.properties.forEach(p => { | ||
var _a; | ||
o.push(` ${p.name}: ${(_a = p.tags.find(t => t.name === 'example')) === null || _a === void 0 ? void 0 : _a.text},`); | ||
}); | ||
o.push(` },`); | ||
o.push(` },`); | ||
o.push(`};`); | ||
o.push(``); | ||
o.push(`export = config;`); | ||
o.push(`\`\`\``); | ||
return o.join('\n'); | ||
} | ||
function createMethodParamTable(data, parameters) { | ||
@@ -222,2 +290,27 @@ const t = new markdown_1.MarkdownTable(); | ||
} | ||
function configInterfaceTable(data, i) { | ||
const o = []; | ||
if (i.docs) { | ||
o.push(`${formatting_1.formatDescription(data, i.docs)}`); | ||
o.push(``); | ||
} | ||
if (i.properties.length > 0) { | ||
const t = new markdown_1.MarkdownTable(); | ||
t.addHeader([`Prop`, `Type`, `Description`, `Default`, `Since`]); | ||
i.properties.forEach(m => { | ||
const defaultValue = getTagText(m.tags, 'default'); | ||
t.addRow([ | ||
`**\`${m.name}\`**`, | ||
formatting_1.formatType(data, m.type).formatted, | ||
formatting_1.formatDescription(data, m.docs), | ||
defaultValue ? `<code>${defaultValue}</code>` : '', | ||
getTagText(m.tags, 'since'), | ||
]); | ||
}); | ||
t.removeEmptyColumns(); | ||
o.push(...t.toMarkdown()); | ||
o.push(``); | ||
} | ||
return o.join(`\n`); | ||
} | ||
function typeAliasTable(data, t) { | ||
@@ -224,0 +317,0 @@ const o = []; |
@@ -24,4 +24,5 @@ "use strict"; | ||
const typeAliases = []; | ||
const pluginConfigs = []; | ||
tsSourceFiles.forEach(tsSourceFile => { | ||
parseSourceFile(tsSourceFile, typeChecker, interfaces, typeAliases, enums); | ||
parseSourceFile(tsSourceFile, typeChecker, interfaces, typeAliases, enums, pluginConfigs); | ||
}); | ||
@@ -35,2 +36,3 @@ return (api) => { | ||
typeAliases: [], | ||
pluginConfigs, | ||
}; | ||
@@ -76,3 +78,3 @@ if (apiInterface) { | ||
} | ||
function parseSourceFile(tsSourceFile, typeChecker, interfaces, typeAliases, enums) { | ||
function parseSourceFile(tsSourceFile, typeChecker, interfaces, typeAliases, enums, pluginConfigs) { | ||
const statements = tsSourceFile.statements; | ||
@@ -82,2 +84,3 @@ const interfaceDeclarations = statements.filter(typescript_1.default.isInterfaceDeclaration); | ||
const enumDeclarations = statements.filter(typescript_1.default.isEnumDeclaration); | ||
const moduleDeclarations = statements.filter(typescript_1.default.isModuleDeclaration); | ||
interfaceDeclarations.forEach(interfaceDeclaration => { | ||
@@ -92,2 +95,7 @@ interfaces.push(getInterface(typeChecker, interfaceDeclaration)); | ||
}); | ||
moduleDeclarations | ||
.filter(m => { var _a; return ((_a = m === null || m === void 0 ? void 0 : m.name) === null || _a === void 0 ? void 0 : _a.text) === '@capacitor/cli'; }) | ||
.forEach(moduleDeclaration => { | ||
getPluginsConfig(typeChecker, moduleDeclaration, pluginConfigs); | ||
}); | ||
} | ||
@@ -257,2 +265,39 @@ function getInterface(typeChecker, node) { | ||
} | ||
function getPluginsConfig(typeChecker, moduleDeclaration, pluginConfigs) { | ||
const body = moduleDeclaration.body; | ||
if (!Array.isArray(body.statements)) { | ||
return; | ||
} | ||
const pluginConfigInterfaces = body.statements.filter((s) => { | ||
var _a; | ||
return ((_a = s === null || s === void 0 ? void 0 : s.name) === null || _a === void 0 ? void 0 : _a.text) === 'PluginsConfig' && | ||
Array.isArray(s === null || s === void 0 ? void 0 : s.members) && | ||
s.members.length > 0; | ||
}); | ||
pluginConfigInterfaces.forEach(pluginConfigInterface => { | ||
pluginConfigInterface.members | ||
.filter(typescript_1.default.isPropertySignature) | ||
.filter(p => (p === null || p === void 0 ? void 0 : p.type) && (p === null || p === void 0 ? void 0 : p.type).members) | ||
.forEach(properytSignature => { | ||
const typeLiteral = properytSignature.type; | ||
const nm = properytSignature.name.getText(); | ||
const symbol = typeChecker.getSymbolAtLocation(properytSignature.name); | ||
const docs = symbol ? serializeSymbol(typeChecker, symbol) : null; | ||
const i = { | ||
name: nm, | ||
slug: slugify(nm), | ||
properties: typeLiteral.members | ||
.filter(typescript_1.default.isPropertySignature) | ||
.map(propertySignature => { | ||
return getInterfaceProperty(typeChecker, propertySignature); | ||
}) | ||
.filter(p => p != null), | ||
docs: (docs === null || docs === void 0 ? void 0 : docs.docs) || '', | ||
}; | ||
if (i.properties.length > 0) { | ||
pluginConfigs.push(i); | ||
} | ||
}); | ||
}); | ||
} | ||
function typeToString(checker, type, typeNode) { | ||
@@ -259,0 +304,0 @@ if (typeNode && typescript_1.default.isTypeReferenceNode(typeNode)) { |
@@ -6,3 +6,10 @@ export interface DocsData { | ||
enums: DocsEnum[]; | ||
pluginConfigs: DocsConfigInterface[]; | ||
} | ||
export interface DocsConfigInterface { | ||
name: string; | ||
slug: string; | ||
properties: DocsInterfaceProperty[]; | ||
docs: string; | ||
} | ||
export interface DocsInterface { | ||
@@ -9,0 +16,0 @@ name: string; |
{ | ||
"name": "@capacitor/docgen", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"description": "Docs Readme Markdown and JSON Generator for Capacitor Plugins", | ||
@@ -33,9 +33,10 @@ "keywords": [ | ||
"dependencies": { | ||
"@types/node": "^14.14.22", | ||
"colorette": "^1.2.1", | ||
"@types/node": "^14.14.31", | ||
"colorette": "^1.2.2", | ||
"github-slugger": "^1.3.0", | ||
"minimist": "^1.2.5", | ||
"typescript": "^4.1.3" | ||
"typescript": "^4.2.2" | ||
}, | ||
"devDependencies": { | ||
"@capacitor/cli": "^3.0.0-beta.3", | ||
"@ionic/prettier-config": "^1.0.0", | ||
@@ -47,3 +48,3 @@ "@stencil/core": "^2.4.0", | ||
"jest": "^26.6.3", | ||
"np": "^7.2.0", | ||
"np": "^7.4.0", | ||
"prettier": "^2.2.1", | ||
@@ -50,0 +51,0 @@ "rimraf": "^3.0.2" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52392
1361
10
Updated@types/node@^14.14.31
Updatedcolorette@^1.2.2
Updatedtypescript@^4.2.2