Skip to content

Commit

Permalink
Merge pull request elastic#16 from spalger/migrate-to-docs-util-pkg
Browse files Browse the repository at this point in the history
migrate to docs-util package
  • Loading branch information
stacey-gammon authored Feb 9, 2021
2 parents 256acf6 + ec3598d commit eed4042
Show file tree
Hide file tree
Showing 74 changed files with 111 additions and 92 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@
"@kbn/plugin-generator": "link:packages/kbn-plugin-generator",
"@kbn/plugin-helpers": "link:packages/kbn-plugin-helpers",
"@kbn/pm": "link:packages/kbn-pm",
"@kbn/release-notes": "link:packages/kbn-release-notes",
"@kbn/docs-utils": "link:packages/kbn-docs-utils",
"@kbn/storybook": "link:packages/kbn-storybook",
"@kbn/telemetry-tools": "link:packages/kbn-telemetry-tools",
"@kbn/test": "link:packages/kbn-test",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
module.exports = {
preset: '@kbn/test',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-release-notes'],
roots: ['<rootDir>/packages/kbn-docs-utils'],
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@kbn/release-notes",
"name": "@kbn/docs-utils",
"version": "1.0.0",
"license": "SSPL-1.0 OR Elastic License 2.0",
"private": "true",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ we determine the value of such a system and what kind of maintenance burder it w
To generate the docs run

```
yarn build:apidocs
node scripts/build_api_docs
```
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,69 @@
* Side Public License, v 1.
*/

import fs from 'fs';
import Fs from 'fs';
import Path from 'path';
import { REPO_ROOT } from '@kbn/dev-utils';
import { ToolingLog } from '@kbn/dev-utils';

import { REPO_ROOT, run } from '@kbn/dev-utils';
import { Project } from 'ts-morph';

import { getPluginApi } from './get_plugin_api';
import { writePluginDocs } from './mdx/write_plugin_mdx_docs';
import { findPlugins } from '../plugin_discovery';
import { ApiDeclaration, PluginApi } from './types';
import { mergeScopeApi } from './utils';
import { findPlugins } from './find_plugins';

export function runBuildApiDocsCli() {
run(
async ({ log }) => {
const project = getTsProject(REPO_ROOT);

const plugins = findPlugins();

const pluginInfos: Array<{
apiCount: number;
apiCountMissingComments: number;
plugin: string;
}> = [];

const outputFolder = Path.resolve(REPO_ROOT, 'api_docs');
if (!Fs.existsSync(outputFolder)) {
Fs.mkdirSync(outputFolder);
} else {
// Delete all files except the README that warns about the auto-generated nature of
// the folder.
const files = Fs.readdirSync(outputFolder);
files.forEach((file) => {
if (file.indexOf('README.md') < 0) {
Fs.rmSync(Path.resolve(outputFolder, file));
}
});
}

plugins.forEach((plugin) => {
const doc = getPluginApi(project, plugin, plugins, log);
const info = {
plugin: plugin.manifest.id,
apiCount: countApiForPlugin(doc),
apiCountMissingComments: countMissingCommentsApiForPlugin(doc),
};

if (info.apiCount > 0) {
writePluginDocs(outputFolder, doc, log);
pluginInfos.push(info);
}
});

const log = new ToolingLog({
level: 'debug',
writeTo: process.stdout,
});
// eslint-disable-next-line no-console
console.table(pluginInfos);
},
{
log: {
defaultLevel: 'debug',
},
}
);
}

function getTsProject(repoPath: string) {
const xpackTsConfig = `${repoPath}/x-pack/tsconfig.json`;
Expand All @@ -34,56 +82,6 @@ function getTsProject(repoPath: string) {
return project;
}

export function buildApiDocs() {
const project = getTsProject(REPO_ROOT);

const plugins = Array.from(
findPlugins({
oss: false,
examples: false,
// Use this to capture "core" as a plugin.
extraPluginScanDirs: [Path.resolve(REPO_ROOT, 'src')],
}).values()
);

const pluginInfos: Array<{
apiCount: number;
apiCountMissingComments: number;
plugin: string;
}> = [];

const outputFolder = Path.resolve(REPO_ROOT, 'api_docs');
if (!fs.existsSync(outputFolder)) {
fs.mkdirSync(outputFolder);
} else {
// Delete all files except the README that warns about the auto-generated nature of
// the folder.
const files = fs.readdirSync(outputFolder);
files.forEach((file) => {
if (file.indexOf('README.md') < 0) {
fs.rmSync(Path.resolve(outputFolder, file));
}
});
}

plugins.forEach((plugin) => {
const doc = getPluginApi(project, plugin, plugins, log);
const info = {
plugin: plugin.manifest.id,
apiCount: countApiForPlugin(doc),
apiCountMissingComments: countMissingCommentsApiForPlugin(doc),
};

if (info.apiCount > 0) {
writePluginDocs(outputFolder, doc, log);
pluginInfos.push(info);
}
});

// eslint-disable-next-line no-console
console.table(pluginInfos);
}

function countMissingCommentsApiForPlugin(doc: PluginApi) {
return (
mergeScopeApi(doc.client).reduce((sum, def) => {
Expand Down
25 changes: 25 additions & 0 deletions packages/kbn-docs-utils/src/api_docs/find_plugins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import Path from 'path';

import { getPluginSearchPaths } from '@kbn/config';
import { simpleKibanaPlatformPluginDiscovery, REPO_ROOT } from '@kbn/dev-utils';

export function findPlugins() {
const pluginSearchPaths = getPluginSearchPaths({
rootDir: REPO_ROOT,
oss: false,
examples: false,
});

return simpleKibanaPlatformPluginDiscovery(pluginSearchPaths, [
// discover "core" as a plugin
Path.resolve(REPO_ROOT, 'src/core'),
]);
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,4 @@
* Side Public License, v 1.
*/

import { buildApiDocs } from './build_api_docs';

buildApiDocs();
export * from './build_api_docs_cli';
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

import fs from 'fs';
import Path from 'path';

import { Project } from 'ts-morph';
import { REPO_ROOT } from '@kbn/utils';
import { ToolingLog, KibanaPlatformPlugin } from '@kbn/dev-utils';

import { writePluginDocs } from '../mdx/write_plugin_mdx_docs';
Expand Down Expand Up @@ -75,15 +75,7 @@ function fnIsCorrect(fn: ApiDeclaration | undefined) {
}

beforeAll(() => {
const tsConfigFilePath = Path.resolve(
REPO_ROOT,
'src',
'dev',
'build_api_docs',
'tests',
'src',
'tsconfig.json'
);
const tsConfigFilePath = Path.resolve(__dirname, '__fixtures__/src/tsconfig.json');
const project = new Project({
tsConfigFilePath,
});
Expand All @@ -95,7 +87,7 @@ beforeAll(() => {
const plugins: KibanaPlatformPlugin[] = [pluginA];

doc = getPluginApi(project, plugins[0], plugins, log);
mdxOutputFolder = Path.resolve(REPO_ROOT, 'src', 'dev', 'build_api_docs', 'tests', 'snapshots');
mdxOutputFolder = Path.resolve(__dirname, 'snapshots');
writePluginDocs(mdxOutputFolder, doc, log);
});

Expand Down Expand Up @@ -131,8 +123,8 @@ it('const exported from common folder is correct', () => {
const fooConst = doc.common.misc.find((c) => c.label === 'commonFoo');
expect(fooConst).toBeDefined();

expect(fooConst!.source.path.indexOf('src/plugin_a/common/foo/index.ts')).toBeGreaterThanOrEqual(
0
expect(fooConst!.source.path.replace(Path.sep, '/')).toContain(
'src/plugin_a/common/foo/index.ts'
);
expect(fooConst!.signature![0]).toBe('"COMMON VAR!"');
});
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@
* Side Public License, v 1.
*/

import { KibanaPlatformPlugin } from '@kbn/dev-utils';
import { findPlugins } from '../plugin_discovery';
import { findPlugins } from './find_plugins';
import { getPluginForPath, getServiceForPath } from './utils';

it('test getPluginForPath', () => {
const plugins: KibanaPlatformPlugin[] = Array.from(
findPlugins({
oss: false,
examples: false,
extraPluginScanDirs: [],
}).values()
);
const plugins = findPlugins();
expect(
getPluginForPath('/Users/auser/kibana/src/plugins/embeddable/public/service/file.ts', plugins)
).toBeDefined();
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions packages/kbn-docs-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

export * from './release_notes';
export * from './api_docs';
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
},
"include": [
"src/**/*"
],
"exclude": [
"**/__fixtures__/**/*"
]
}
2 changes: 1 addition & 1 deletion scripts/build_api_docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
*/

require('../src/setup_node_env');
require('../src/dev/build_api_docs/run');
require('@kbn/docs-utils').runBuildApiDocsCli();
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3445,6 +3445,10 @@
version "0.0.0"
uid ""

"@kbn/docs-utils@link:packages/kbn-docs-utils":
version "0.0.0"
uid ""

"@kbn/es-archiver@link:packages/kbn-es-archiver":
version "0.0.0"
uid ""
Expand Down Expand Up @@ -3501,10 +3505,6 @@
version "0.0.0"
uid ""

"@kbn/release-notes@link:packages/kbn-release-notes":
version "0.0.0"
uid ""

"@kbn/std@link:packages/kbn-std":
version "0.0.0"
uid ""
Expand Down

0 comments on commit eed4042

Please sign in to comment.