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 83da81d
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 162 deletions.
2 changes: 2 additions & 0 deletions docs/generated/cli/create-nx-workspace.md
Expand Up @@ -19,6 +19,8 @@ Install `create-nx-workspace` globally to invoke the command directly, or use `n

### allPrompts

Default: `false`

Show all prompts

### appName
Expand Down
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')}:`,
};
12 changes: 11 additions & 1 deletion packages/create-nx-workspace/bin/package-manager.ts
@@ -1,4 +1,6 @@
import { execSync } from 'child_process';
import { existsSync } from 'fs';
import { join } from 'path';

/*
* Because we don't want to depend on @nrwl/workspace (to speed up the workspace creation)
Expand All @@ -9,6 +11,14 @@ export const packageManagerList = ['npm', 'yarn', 'pnpm'] as const;

export type PackageManager = typeof packageManagerList[number];

export function detectPackageManager(dir: string = ''): PackageManager {
return existsSync(join(dir, 'yarn.lock'))
? 'yarn'
: existsSync(join(dir, 'pnpm-lock.yaml'))
? 'pnpm'
: 'npm';
}

/**
* Returns commands for the package manager used in the workspace.
* By default, the package manager is derived based on the lock file,
Expand All @@ -22,7 +32,7 @@ export type PackageManager = typeof packageManagerList[number];
*
*/
export function getPackageManagerCommand(
packageManager: PackageManager = 'npm'
packageManager: PackageManager = detectPackageManager()
): {
install: string;
add: string;
Expand Down
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 83da81d

Please sign in to comment.