[docs] rewrite docs cli to show logs and use modern apis (#117767)

Co-authored-by: spalger <spalger@users.noreply.github.com>
This commit is contained in:
Spencer 2021-11-06 19:31:22 -06:00 committed by GitHub
parent 27f127d581
commit 5a4a38148d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 59 deletions

View file

@ -7,4 +7,4 @@
*/
require('../src/setup_node_env');
require('../src/docs/cli');
require('../src/dev/run_build_docs_cli').runBuildDocsCli();

View file

@ -0,0 +1,64 @@
/*
* 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 dedent from 'dedent';
import { run, REPO_ROOT, createFailError } from '@kbn/dev-utils';
const DEFAULT_DOC_REPO_PATH = Path.resolve(REPO_ROOT, '..', 'docs');
const rel = (path: string) => Path.relative(process.cwd(), path);
export function runBuildDocsCli() {
run(
async ({ flags, procRunner }) => {
const docRepoPath =
typeof flags.docrepo === 'string' && flags.docrepo
? Path.resolve(process.cwd(), flags.docrepo)
: DEFAULT_DOC_REPO_PATH;
try {
await procRunner.run('build_docs', {
cmd: rel(Path.resolve(docRepoPath, 'build_docs')),
args: [
['--doc', rel(Path.resolve(REPO_ROOT, 'docs/index.asciidoc'))],
['--chunk', '1'],
flags.open ? ['--open'] : [],
].flat(),
cwd: REPO_ROOT,
wait: true,
});
} catch (error) {
if (error.code === 'ENOENT') {
throw createFailError(dedent`
Unable to run "build_docs" script from docs repo.
Does it exist at [${rel(docRepoPath)}]?
Do you need to pass --docrepo to specify the correct path or clone it there?
`);
}
throw error;
}
},
{
description: 'Build the docs and serve them from a docker container',
flags: {
string: ['docrepo'],
boolean: ['open'],
default: {
docrepo: DEFAULT_DOC_REPO_PATH,
},
help: `
--docrepo [path] Path to the doc repo, defaults to ${rel(DEFAULT_DOC_REPO_PATH)}
--open Automatically open the built docs in your default browser after building
`,
},
}
);
}

View file

@ -1,30 +0,0 @@
/*
* 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 { execFileSync } from 'child_process';
import { Command } from 'commander';
import { defaultDocsRepoPath, buildDocsScript, buildDocsArgs } from './docs_repo';
const cmd = new Command('node scripts/docs');
cmd
.option('--docrepo [path]', 'local path to the docs repo', defaultDocsRepoPath())
.option('--open', 'open the docs in the browser', false)
.parse(process.argv);
try {
execFileSync(buildDocsScript(cmd), buildDocsArgs(cmd));
} catch (err) {
if (err.code === 'ENOENT') {
console.error(`elastic/docs repo must be cloned to ${cmd.docrepo}`);
} else {
console.error(err.stack);
}
process.exit(1);
}

View file

@ -1,28 +0,0 @@
/*
* 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 { resolve } from 'path';
const kibanaDir = resolve(__dirname, '..', '..');
export function buildDocsScript(cmd) {
return resolve(process.cwd(), cmd.docrepo, 'build_docs');
}
export function buildDocsArgs(cmd) {
const docsIndexFile = resolve(kibanaDir, 'docs', 'index.asciidoc');
let args = ['--doc', docsIndexFile, '--direct_html', '--chunk=1'];
if (cmd.open) {
args = [...args, '--open'];
}
return args;
}
export function defaultDocsRepoPath() {
return resolve(kibanaDir, '..', 'docs');
}