|
| 1 | +import * as fs from 'fs-extra'; |
| 2 | +import { Application } from 'typedoc'; |
| 3 | +import { basename, dirname, resolve } from 'path'; |
| 4 | +import { runInNewContext } from 'vm'; |
| 5 | + |
| 6 | +interface Plugin { |
| 7 | + packageName: string; |
| 8 | + displayName: string; |
| 9 | + description: string; |
| 10 | + platforms: string[]; |
| 11 | + usage: string; |
| 12 | + repo: string; |
| 13 | + cordovaPlugin: { |
| 14 | + name: string; |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +const rootDir = resolve(__dirname, '../..'); |
| 19 | +const typedocTmp = resolve(__dirname, 'typedoc.tmp.json'); |
| 20 | +const pluginsDir = resolve(rootDir, 'src/@ionic-native/plugins'); |
| 21 | +const typedoc = new Application({ |
| 22 | + mode: 'modules', |
| 23 | + tsconfig: resolve(rootDir, 'tsconfig.json'), |
| 24 | + ignoreCompilerErrors: true |
| 25 | +}); |
| 26 | + |
| 27 | +run(pluginsDir); |
| 28 | + |
| 29 | +async function run(pluginsDir: string) { |
| 30 | + const typedocData = await generateTypedoc(pluginsDir); |
| 31 | + const modules = typedocData.children.filter(isModule); |
| 32 | + const plugins = modules.filter(hasPlugin).map(processPlugin); |
| 33 | + await fs.outputJson(resolve(__dirname, 'plugins.json'), plugins, { |
| 34 | + spaces: 2 |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +async function generateTypedoc(root: string, outputPath = typedocTmp) { |
| 39 | + const pluginDirs = await fs.readdir(root); |
| 40 | + const paths = pluginDirs.map(dir => resolve(root, dir, 'index.ts')); |
| 41 | + typedoc.generateJson(paths, outputPath); |
| 42 | + return fs.readJson(outputPath); |
| 43 | +} |
| 44 | + |
| 45 | +function processPlugin(pluginModule): Plugin { |
| 46 | + const pluginClass = pluginModule.children.find(isPlugin); |
| 47 | + const decorator = getPluginDecorator(pluginClass); |
| 48 | + const packageName = `@ionic-native/${basename(dirname(pluginModule.originalName))}`; |
| 49 | + const displayName = getTag(pluginClass, 'name'); |
| 50 | + const usage = getTag(pluginClass, 'usage'); |
| 51 | + const description = getTag(pluginClass, 'description'); |
| 52 | + return { |
| 53 | + packageName, |
| 54 | + displayName, |
| 55 | + description, |
| 56 | + usage, |
| 57 | + platforms: decorator.platforms, |
| 58 | + repo: decorator.repo, |
| 59 | + cordovaPlugin: { |
| 60 | + name: decorator.plugin |
| 61 | + } |
| 62 | + }; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Typedoc only gives us the Plugin decorator internals |
| 67 | + * as a string. So, rather than try to parse that with a RegExp, |
| 68 | + * we evaluate it using Node's vm module. |
| 69 | + */ |
| 70 | +const getPluginDecorator = (child: any) => { |
| 71 | + if (isPlugin(child)) { |
| 72 | + const decorator = child.decorators.find(d => d.name === 'Plugin'); |
| 73 | + return runInNewContext(`(${decorator.arguments.config})`); |
| 74 | + } |
| 75 | +}; |
| 76 | + |
| 77 | +const getTag = (child: any, tagName: string): string => { |
| 78 | + if (hasTags(child)) { |
| 79 | + const tag = child.comment.tags.find(t => t.tag === tagName); |
| 80 | + if (tag) { |
| 81 | + return tag.text; |
| 82 | + } |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +const isModule = (child: any): boolean => |
| 87 | + child.kind === 1; |
| 88 | + |
| 89 | +const isClass = (child: any): boolean => |
| 90 | + child.kind === 128; |
| 91 | + |
| 92 | +const isPlugin = (child: any): boolean => |
| 93 | + isClass(child) && Array.isArray(child.decorators) && child.decorators.some(d => d.name === 'Plugin'); |
| 94 | + |
| 95 | +const hasPlugin = (child: any): boolean => |
| 96 | + child.children.some(isPlugin); |
| 97 | + |
| 98 | +const hasTags = (child: any): boolean => |
| 99 | + child.comment && Array.isArray(child.comment.tags); |
0 commit comments