Skip to content

Commit b0dea6d

Browse files
authored
feat(scripts): add docs-json script (danielsogl#2947)
1 parent 7e090cc commit b0dea6d

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ node_modules/
44
.tmp
55
aot/
66
scripts/ionic-native-bower
7+
scripts/docs-json/*.json
78
dist/
89
src/@ionic-native/plugins/**/ngx
910
*.d.ts

package-lock.json

+90
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"npmpub": "ts-node -P scripts/tsconfig.json scripts/tasks/publish",
1818
"lint": "gulp lint",
1919
"readmes": "gulp readmes",
20+
"docs-json": "ts-node -P scripts/tsconfig.json scripts/docs-json",
2021
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
2122
"postchangelog": "git commit -am \"chore(): update changelog\"",
2223
"shipit": "npm run build && npm run readmes && npm run npmpub"
@@ -54,6 +55,7 @@
5455
"ts-node": "^8.0.2",
5556
"tslint": "^5.12.1",
5657
"tslint-ionic-rules": "0.0.21",
58+
"typedoc": "^0.14.2",
5759
"typescript": "3.2.4",
5860
"uglifyjs-webpack-plugin": "^2.1.1",
5961
"unminified-webpack-plugin": "^2.0.0",

scripts/docs-json/index.ts

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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);

scripts/docs-json/readme.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# docs-json
2+
3+
This script reads and generates [typedoc](https://github.com/TypeStrong/typedoc) data for each of the plugins in `src/@ionic-native/plugins`. That data is then formatted and output as `plugins.json` in this directory.

0 commit comments

Comments
 (0)