Skip to content

Commit

Permalink
fix(core): migrate to yargs 17.x
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Mar 29, 2022
1 parent bab515e commit 7586cc6
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 199 deletions.
4 changes: 0 additions & 4 deletions docs/generated/cli/affected-apps.md
Expand Up @@ -69,8 +69,6 @@ Show help

### ~~only-failed~~

Default: `false`

**Deprecated:** The command to rerun failed projects will appear if projects fail. This now does nothing and will be removed in v15.

Isolate projects which previously failed
Expand All @@ -85,8 +83,6 @@ This is the name of the tasks runner configured in nx.json

### skip-nx-cache

Default: `false`

Rerun the tasks even when the results are available in the cache

### uncommitted
Expand Down
6 changes: 0 additions & 6 deletions docs/generated/cli/affected-graph.md
Expand Up @@ -103,8 +103,6 @@ Bind the project graph server to a specific ip address.

### ~~only-failed~~

Default: `false`

**Deprecated:** The command to rerun failed projects will appear if projects fail. This now does nothing and will be removed in v15.

Isolate projects which previously failed
Expand All @@ -125,8 +123,6 @@ This is the name of the tasks runner configured in nx.json

### skip-nx-cache

Default: `false`

Rerun the tasks even when the results are available in the cache

### uncommitted
Expand All @@ -147,6 +143,4 @@ Show version number

### watch

Default: `false`

Watch for changes to project graph and update in-browser
4 changes: 0 additions & 4 deletions docs/generated/cli/affected-libs.md
Expand Up @@ -69,8 +69,6 @@ Show help

### ~~only-failed~~

Default: `false`

**Deprecated:** The command to rerun failed projects will appear if projects fail. This now does nothing and will be removed in v15.

Isolate projects which previously failed
Expand All @@ -85,8 +83,6 @@ This is the name of the tasks runner configured in nx.json

### skip-nx-cache

Default: `false`

Rerun the tasks even when the results are available in the cache

### uncommitted
Expand Down
4 changes: 0 additions & 4 deletions docs/generated/cli/affected.md
Expand Up @@ -87,8 +87,6 @@ Show help

### ~~only-failed~~

Default: `false`

**Deprecated:** The command to rerun failed projects will appear if projects fail. This now does nothing and will be removed in v15.

Isolate projects which previously failed
Expand All @@ -103,8 +101,6 @@ This is the name of the tasks runner configured in nx.json

### skip-nx-cache

Default: `false`

Rerun the tasks even when the results are available in the cache

### target
Expand Down
4 changes: 0 additions & 4 deletions docs/generated/cli/format-check.md
Expand Up @@ -53,8 +53,6 @@ Format only libraries and applications files.

### ~~only-failed~~

Default: `false`

**Deprecated:** The command to rerun failed projects will appear if projects fail. This now does nothing and will be removed in v15.

Isolate projects which previously failed
Expand All @@ -69,8 +67,6 @@ This is the name of the tasks runner configured in nx.json

### skip-nx-cache

Default: `false`

Rerun the tasks even when the results are available in the cache

### uncommitted
Expand Down
4 changes: 0 additions & 4 deletions docs/generated/cli/format-write.md
Expand Up @@ -53,8 +53,6 @@ Format only libraries and applications files.

### ~~only-failed~~

Default: `false`

**Deprecated:** The command to rerun failed projects will appear if projects fail. This now does nothing and will be removed in v15.

Isolate projects which previously failed
Expand All @@ -69,8 +67,6 @@ This is the name of the tasks runner configured in nx.json

### skip-nx-cache

Default: `false`

Rerun the tasks even when the results are available in the cache

### uncommitted
Expand Down
2 changes: 0 additions & 2 deletions docs/generated/cli/graph.md
Expand Up @@ -107,6 +107,4 @@ Show version number

### watch

Default: `false`

Watch for changes to project graph and update in-browser
4 changes: 0 additions & 4 deletions docs/generated/cli/print-affected.md
Expand Up @@ -81,8 +81,6 @@ Show help

### ~~only-failed~~

Default: `false`

**Deprecated:** The command to rerun failed projects will appear if projects fail. This now does nothing and will be removed in v15.

Isolate projects which previously failed
Expand All @@ -97,8 +95,6 @@ Select the subset of the returned json document (e.g., --selected=projects)

### skip-nx-cache

Default: `false`

Rerun the tasks even when the results are available in the cache

### uncommitted
Expand Down
6 changes: 0 additions & 6 deletions docs/generated/cli/run-many.md
Expand Up @@ -57,8 +57,6 @@ Show help

### ~~only-failed~~

Default: `false`

**Deprecated:** The command to rerun failed projects will appear if projects fail. This now does nothing and will be removed in v15.

Only run the target on projects which previously failed
Expand All @@ -77,8 +75,6 @@ Override the tasks runner in `nx.json`

### skip-nx-cache

Default: `false`

Rerun the tasks even when the results are available in the cache

### target
Expand All @@ -95,8 +91,6 @@ Show version number

### ~~with-deps~~

Default: `false`

**Deprecated:** Configure target dependencies instead. It will be removed in v14.

Include dependencies of specified projects when computing what to run
18 changes: 9 additions & 9 deletions packages/create-nx-workspace/bin/create-nx-workspace.ts
Expand Up @@ -122,17 +122,15 @@ export const commandsObject: yargs.Argv<Arguments> = yargs
'strip-dashed': true,
'dot-notation': false,
})
.strict()
.command(
// this is the default and only command
'$0 [name] [options]',
'Create a new Nx workspace',
(yargs) =>
yargs
.positional('name', {
.option('name', {
describe: `Workspace name (e.g. org name)`,
type: 'string',
demandOption: true,
})
.option('preset', {
describe: `Customizes the initial content of your workspace. To build your own see https://nx.dev/nx-plugin/overview#preset`,
Expand Down Expand Up @@ -179,7 +177,7 @@ export const commandsObject: yargs.Argv<Arguments> = yargs
describe: `Default base to use for new projects`,
type: 'string',
}),
async (argv) => {
async (argv: yargs.ArgumentsCamelCase<Arguments>) => {
await main(argv).catch((error) => {
const { version } = require('../package.json');
output.error({
Expand All @@ -192,7 +190,7 @@ export const commandsObject: yargs.Argv<Arguments> = yargs
)
.help('help')
.updateLocale(yargsDecorator)
.version(nxVersion);
.version(nxVersion) as yargs.Argv<Arguments>;

async function main(parsedArgs: yargs.Arguments<Arguments>) {
const {
Expand Down Expand Up @@ -242,7 +240,9 @@ async function main(parsedArgs: yargs.Arguments<Arguments>) {
}
}

async function getConfiguration(argv: yargs.Arguments<Arguments>) {
async function getConfiguration(
argv: yargs.Arguments<Arguments>
): Promise<void> {
try {
let style, appName;

Expand All @@ -260,7 +260,7 @@ async function getConfiguration(argv: yargs.Arguments<Arguments>) {
const defaultBase = await determineDefaultBase(argv);
const nxCloud = await determineNxCloud(argv);

return {
Object.assign(argv, {
name,
preset,
appName,
Expand All @@ -269,7 +269,7 @@ async function getConfiguration(argv: yargs.Arguments<Arguments>) {
nxCloud,
packageManager,
defaultBase,
};
});
} catch (e) {
console.error(e);
process.exit(1);
Expand Down Expand Up @@ -751,7 +751,7 @@ function execAndWait(command: string, cwd: string) {
});
}

async function determineNxCloud(parsedArgs: any) {
async function determineNxCloud(parsedArgs: yargs.Arguments<Arguments>) {
if (parsedArgs.nxCloud === undefined) {
return enquirer
.prompt([
Expand Down
4 changes: 2 additions & 2 deletions packages/create-nx-workspace/bin/decorator.ts
@@ -1,6 +1,6 @@
import * as chalk from 'chalk';

export const yargsDecorator = {
'Options:': `${chalk.green('Options')}:`,
'Positionals:': `${chalk.green('Positionals')}:`,
'Options:': `${chalk.bold('Options')}:`,
'Positionals:': `${chalk.bold('Positionals')}:`,
};
14 changes: 12 additions & 2 deletions scripts/documentation/documentation.ts
@@ -1,5 +1,7 @@
import * as chalk from 'chalk';
import { execSync } from 'child_process';
import { removeSync } from 'fs-extra';
import { join } from 'path';
import { generateCLIDocumentation } from './generate-cli-data';
import { generateCNWocumentation } from './generate-cnw-documentation';
import { generateDevkitDocumentation } from './generate-devkit-documentation';
Expand All @@ -10,8 +12,16 @@ async function generate() {
console.log(`${chalk.blue('i')} Generating Documentation`);
generatePackageSchemas();
generateDevkitDocumentation();
await generateCNWocumentation();
await generateCLIDocumentation();

const commandsOutputDirectory = join(
__dirname,
'../../docs/',
'generated',
'cli'
);
removeSync(commandsOutputDirectory);
await generateCNWocumentation(commandsOutputDirectory);
await generateCLIDocumentation(commandsOutputDirectory);

console.log(`\n${chalk.green('✓')} Generated Documentation\n`);
} catch (e) {
Expand Down
87 changes: 9 additions & 78 deletions scripts/documentation/generate-cli-data.ts
@@ -1,16 +1,18 @@
import * as chalk from 'chalk';
import { readFileSync } from 'fs';
import { readJsonSync, removeSync } from 'fs-extra';
import { readJsonSync } from 'fs-extra';
import { join } from 'path';
import { dedent } from 'tslint/lib/utils';
import {
formatDeprecated,
generateMarkdownFile,
getCommands,
parseCommand,
ParsedCommand,
sortAlphabeticallyFunction,
} from './utils';
import { register as registerTsConfigPaths } from 'tsconfig-paths';

import { examples } from '../../packages/nx/src/command-line/examples';
import { dedent } from 'tslint/lib/utils';

const importFresh = require('import-fresh');

Expand All @@ -24,21 +26,9 @@ const sharedCommands = [
'test',
];

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

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

export async function generateCLIDocumentation() {
export async function generateCLIDocumentation(
commandsOutputDirectory: string
) {
/**
* For certain commands, they will output dynamic data at runtime in a real workspace,
* so we leverage an envrionment variable to inform the logic of the context that we
Expand All @@ -57,65 +47,6 @@ export async function generateCLIDocumentation() {
'../../packages/nx/src/command-line/nx-commands'
);

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

function getCommands(command) {
return command
.getInternalMethods()
.getCommandInstance()
.getCommandHandlers();
}
async function parseCommandInstance(
name: string,
command: any
): Promise<ParsedCommand> {
// It is not a function return a strip down version of the command
if (
!(
command.builder &&
command.builder.constructor &&
command.builder.call &&
command.builder.apply
)
) {
return {
name,
commandString: command.original,
description: command.description,
};
}
// Show all the options we can get from yargs
const builder = await command.builder(
importFresh('yargs')().getInternalMethods().reset()
);
const builderDescriptions = builder
.getInternalMethods()
.getUsageInstance()
.getDescriptions();
const builderDefaultOptions = builder.getOptions().default;
const builderDeprecatedOptions = builder.getDeprecatedOptions();
return {
name,
description: command.description,
commandString: command.original,
options:
Object.keys(builderDescriptions).map((key) => ({
name: key,
description: builderDescriptions[key]
? builderDescriptions[key].replace('__yargsString__:', '')
: '',
default: builderDefaultOptions[key],
deprecated: builderDeprecatedOptions[key],
})) || null,
};
}

function generateMarkdown(command: ParsedCommand) {
let template = dedent`
---
Expand Down Expand Up @@ -181,7 +112,7 @@ nx ${command.commandString}
Object.keys(nxCommands)
.filter((name) => !sharedCommands.includes(name))
.filter((name) => nxCommands[name].description)
.map((name) => parseCommandInstance(name, nxCommands[name]))
.map((name) => parseCommand(name, nxCommands[name]))
.map(async (command) => generateMarkdown(await command))
.map(async (templateObject) =>
generateMarkdownFile(commandsOutputDirectory, await templateObject)
Expand Down

0 comments on commit 7586cc6

Please sign in to comment.