Skip to content

Commit

Permalink
feat(core): add docs generation
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Mar 28, 2022
1 parent 7a1549e commit 5a96054
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 7 deletions.
5 changes: 5 additions & 0 deletions docs/map.json
Expand Up @@ -360,6 +360,11 @@
"name": "CLI",
"id": "cli",
"itemList": [
{
"name": "create-nx-workspace",
"id": "create-nx-workspace",
"file": "generated/cli/create-nx-workspace"
},
{
"name": "generate",
"id": "generate",
Expand Down
9 changes: 3 additions & 6 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Expand Up @@ -115,16 +115,15 @@ const cliVersion = 'NX_VERSION';
const nxVersion = 'NX_VERSION';
const prettierVersion = 'PRETTIER_VERSION';

export const commandsObject: yargs.Argv<Arguments> = yargs(
process.argv.slice(2)
)
export const commandsObject: yargs.Argv<Arguments> = yargs
.wrap(yargs.terminalWidth())
.parserConfiguration({
'strip-dashed': true,
'dot-notation': false,
})
.strict()
.command(
// this is the default and only command
'$0 [name] [options]',
'✨ Create a new Nx workspace ✨',
(yargs) =>
Expand Down Expand Up @@ -190,11 +189,9 @@ export const commandsObject: yargs.Argv<Arguments> = yargs(
},
[getConfiguration]
)
.help('help', 'Show this help')
.help('help')
.version(nxVersion);

commandsObject.argv;

async function main(parsedArgs: yargs.Arguments<Arguments>) {
const {
name,
Expand Down
3 changes: 3 additions & 0 deletions packages/create-nx-workspace/bin/index.ts
@@ -0,0 +1,3 @@
import { commandsObject } from './create-nx-workspace';

commandsObject.argv;
2 changes: 1 addition & 1 deletion packages/create-nx-workspace/package.json
Expand Up @@ -19,7 +19,7 @@
"CLI"
],
"bin": {
"create-nx-workspace": "./bin/create-nx-workspace.js"
"create-nx-workspace": "./bin/index.js"
},
"author": "Victor Savkin",
"license": "MIT",
Expand Down
2 changes: 2 additions & 0 deletions scripts/documentation/documentation.ts
@@ -1,6 +1,7 @@
import * as chalk from 'chalk';
import { execSync } from 'child_process';
import { generateCLIDocumentation } from './generate-cli-data';
import { generateCNWocumentation } from './generate-cnw-documentation';
import { generateDevkitDocumentation } from './generate-devkit-documentation';
import { generatePackageSchemas } from './package-schemas/generatePackageSchemas';

Expand All @@ -10,6 +11,7 @@ async function generate() {
generatePackageSchemas();
generateDevkitDocumentation();
await generateCLIDocumentation();
await generateCNWocumentation();

console.log(`\n${chalk.green('✓')} Generated Documentation\n`);
} catch (e) {
Expand Down
121 changes: 121 additions & 0 deletions scripts/documentation/generate-cnw-documentation.ts
@@ -0,0 +1,121 @@
import * as chalk from 'chalk';
import { join } from 'path';
import { dedent } from 'tslint/lib/utils';
import {
formatDeprecated,
sortAlphabeticallyFunction,
generateMarkdownFile,
} from './utils';
const importFresh = require('import-fresh');

export async function generateCNWocumentation() {
process.env.NX_GENERATE_DOCS_PROCESS = 'true';

console.log(
`\n${chalk.blue(
'i'
)} Generating Documentation for Create Nx Workspace Command`
);

const { commandsObject } = importFresh(
'../../packages/create-nx-workspace/bin/create-nx-workspace'
);

const commandsOutputDirectory = join(
__dirname,
'../../docs/',
'generated',
'cli'
);

const command = commandsObject.getCommandInstance().getCommandHandlers()[
'$0'
];
const parsedCommand = await parseCommand(command);
const markdown = generateMarkdown(parsedCommand);
generateMarkdownFile(commandsOutputDirectory, markdown);

delete process.env.NX_GENERATE_DOCS_PROCESS;

console.log(
`${chalk.green(
'✓'
)} Generated Documentation for Create Nx Workspace Command`
);
}

interface ParsedCommandOption {
name: string;
description: string;
default: string;
deprecated: boolean | string;
}

interface ParsedCommand {
commandString: string;
description: string;
options?: Array<ParsedCommandOption>;
}

async function parseCommand(command: any): Promise<ParsedCommand> {
const builder = await command.builder(importFresh('yargs')().resetOptions());
const builderDescriptions = builder.getUsageInstance().getDescriptions();
const builderDefaultOptions = builder.getOptions().default;
const builderAutomatedOptions = builder.getOptions().defaultDescription;
const builderDeprecatedOptions = builder.getDeprecatedOptions();

return {
description: command.description,
commandString: command.original.replace('$0', 'create-nx-workspace'),
options:
Object.keys(builderDescriptions).map((key) => ({
name: key,
description: builderDescriptions[key]
? builderDescriptions[key].replace('__yargsString__:', '')
: '',
default: builderDefaultOptions[key] || builderAutomatedOptions[key],
deprecated: builderDeprecatedOptions[key],
})) || null,
};
}

function generateMarkdown(command: ParsedCommand) {
let template = dedent`
---
title: "create-nx-workspace - CLI command"
description: "${command.description}"
---
# create-nx-workspace
${command.description}
## Usage
\`\`\`bash
${command.commandString}
\`\`\`
Install \`create-nx-workspace\` globally to invoke the command directly, or use \`npx create-nx-workspace\`, \`yarn create nx-workspace\`, or \`pnpx create-nx-workspace\`.\n`;

template += '\n## Options';
command.options
.sort((a, b) => sortAlphabeticallyFunction(a.name, b.name))
.forEach((option) => {
template += dedent`
### ${option.deprecated ? `~~${option.name}~~` : option.name}
${
option.default === undefined || option.default === ''
? ''
: `Default: \`${option.default}\`\n`
}
`;
template += dedent`
${formatDeprecated(option.description, option.deprecated)}
`;
});

return {
name: 'create-nx-workspace',
template,
};
}

0 comments on commit 5a96054

Please sign in to comment.