Skip to content

Commit

Permalink
fix(core): use withVerbose util
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Aug 20, 2024
1 parent 7e15838 commit 87ffb1b
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 77 deletions.
7 changes: 1 addition & 6 deletions packages/nx/src/command-line/add/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ import type { AddOptions } from './command-object';
import { normalizeVersionForNxJson } from '../init/implementation/dot-nx/add-nx-scripts';

export function addHandler(options: AddOptions): Promise<number> {
if (options.verbose) {
process.env.NX_VERBOSE_LOGGING = 'true';
}
const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';

return handleErrors(isVerbose, async () => {
return handleErrors(options.verbose, async () => {
output.addNewline();

const [pkgName, version] = parsePackageSpecifier(options.packageSpecifier);
Expand Down
9 changes: 2 additions & 7 deletions packages/nx/src/command-line/add/command-object.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandModule } from 'yargs';
import { withOverrides } from '../yargs-utils/shared-options';
import { withOverrides, withVerbose } from '../yargs-utils/shared-options';

export interface AddOptions {
packageSpecifier: string;
Expand All @@ -15,7 +15,7 @@ export const yargsAddCommand: CommandModule<
command: 'add <packageSpecifier>',
describe: 'Install a plugin and initialize it.',
builder: (yargs) =>
yargs
withVerbose(yargs)
.parserConfiguration({
'strip-dashed': true,
'unknown-options-as-args': true,
Expand All @@ -30,11 +30,6 @@ export const yargsAddCommand: CommandModule<
description:
'Update `package.json` scripts with inferred targets. Defaults to `true` when the package is a core Nx plugin',
})
.option('verbose', {
type: 'boolean',
description:
'Prints additional information about the commands (e.g., stack traces)',
})
.example(
'$0 add @nx/react',
'Install the latest version of the `@nx/react` package and run its `@nx/react:init` generator'
Expand Down
8 changes: 2 additions & 6 deletions packages/nx/src/command-line/generate/command-object.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CommandModule, Argv } from 'yargs';
import { getCwd } from '../../utils/path';
import { linkToNxDevAndExamples } from '../yargs-utils/documentation';
import { withVerbose } from '../yargs-utils/shared-options';

export const yargsGenerateCommand: CommandModule = {
command: 'generate <generator> [_..]',
Expand All @@ -19,7 +20,7 @@ export const yargsGenerateCommand: CommandModule = {
function withGenerateOptions(yargs: Argv) {
const generatorWillShowHelp =
process.argv[3] && !process.argv[3].startsWith('-');
const res = yargs
const res = withVerbose(yargs)
.positional('generator', {
describe: 'Name of the generator (e.g., @nx/js:library, library)',
type: 'string',
Expand All @@ -36,11 +37,6 @@ function withGenerateOptions(yargs: Argv) {
type: 'boolean',
default: true,
})
.option('verbose', {
describe:
'Prints additional information about the commands (e.g., stack traces)',
type: 'boolean',
})
.option('quiet', {
describe: 'Hides logs from tree operations (e.g. `CREATE package.json`)',
type: 'boolean',
Expand Down
9 changes: 3 additions & 6 deletions packages/nx/src/command-line/import/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ export const yargsImportCommand: CommandModule = {
'import'
),
handler: async (args) => {
const exitCode = await handleErrors(
(args.verbose as boolean) ?? process.env.NX_VERBOSE_LOGGING === 'true',
async () => {
return (await import('./import')).importHandler(args as any);
}
);
const exitCode = await handleErrors(args.verbose as boolean, async () => {
return (await import('./import')).importHandler(args as any);
});
process.exit(exitCode);
},
};
8 changes: 2 additions & 6 deletions packages/nx/src/command-line/release/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
withOutputStyleOption,
withOverrides,
withRunManyOptions,
withVerbose,
} from '../yargs-utils/shared-options';
import { VersionData } from './utils/shared';

Expand Down Expand Up @@ -102,7 +103,7 @@ export const yargsReleaseCommand: CommandModule<
describe:
'Orchestrate versioning and publishing of applications and libraries',
builder: (yargs) =>
yargs
withVerbose(yargs)
.command(releaseCommand)
.command(versionCommand)
.command(changelogCommand)
Expand Down Expand Up @@ -133,11 +134,6 @@ export const yargsReleaseCommand: CommandModule<
type: 'boolean',
default: false,
})
.option('verbose', {
type: 'boolean',
describe:
'Prints additional information about the commands (e.g., stack traces)',
})
// NOTE: The camel case format is required for the coerce() function to be called correctly. It still supports --print-config casing.
.option('printConfig', {
type: 'string',
Expand Down
8 changes: 2 additions & 6 deletions packages/nx/src/command-line/repair/command-object.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ArgumentsCamelCase, CommandModule } from 'yargs';
import { linkToNxDevAndExamples } from '../yargs-utils/documentation';
import { withVerbose } from '../yargs-utils/shared-options';

export const yargsRepairCommand: CommandModule = {
command: 'repair',
Expand All @@ -13,12 +14,7 @@ export const yargsRepairCommand: CommandModule = {
If your repository has only ever updated to newer versions of Nx with
\`nx migrate\`, running \`nx repair\` should do nothing.
`,
builder: (yargs) =>
linkToNxDevAndExamples(yargs, 'repair').option('verbose', {
type: 'boolean',
describe:
'Prints additional information about the commands (e.g., stack traces)',
}),
builder: (yargs) => linkToNxDevAndExamples(withVerbose(yargs), 'repair'),
handler: async (args: ArgumentsCamelCase<{ verbose: boolean }>) =>
process.exit(await (await import('./repair')).repair(args)),
};
8 changes: 2 additions & 6 deletions packages/nx/src/command-line/repair/repair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ export async function repair(
args: { verbose: boolean },
extraMigrations = [] as any[]
) {
if (args['verbose']) {
process.env.NX_VERBOSE_LOGGING = 'true';
}
const verbose = process.env.NX_VERBOSE_LOGGING === 'true';
return handleErrors(verbose, async () => {
return handleErrors(args.verbose, async () => {
const nxMigrations = Object.entries(migrationsJson.generators).reduce(
(agg, [name, migration]) => {
const skip = migration['x-repair-skip'];
Expand All @@ -33,7 +29,7 @@ export async function repair(
const migrationsThatMadeNoChanges = await executeMigrations(
process.cwd(),
migrations,
verbose,
args.verbose,
false,
''
);
Expand Down
22 changes: 8 additions & 14 deletions packages/nx/src/command-line/show/command-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,10 @@ const showProjectsCommand: CommandModule<NxShowArgs, ShowProjectsOptions> = {
'Show affected projects in the workspace, excluding end-to-end projects'
) as any,
handler: async (args) => {
const exitCode = await handleErrors(
args.verbose ?? process.env.NX_VERBOSE_LOGGING === 'true',
async () => {
const { showProjectsHandler } = await import('./projects');
await showProjectsHandler(args);
}
);
const exitCode = await handleErrors(args.verbose as boolean, async () => {
const { showProjectsHandler } = await import('./projects');
await showProjectsHandler(args);
});
process.exit(exitCode);
},
};
Expand Down Expand Up @@ -178,13 +175,10 @@ const showProjectCommand: CommandModule<NxShowArgs, ShowProjectOptions> = {
'View project information for my-app in the browser'
),
handler: async (args) => {
const exitCode = await handleErrors(
args.verbose ?? process.env.NX_VERBOSE_LOGGING === 'true',
async () => {
const { showProjectHandler } = await import('./project');
await showProjectHandler(args);
}
);
const exitCode = await handleErrors(args.verbose as boolean, async () => {
const { showProjectHandler } = await import('./project');
await showProjectHandler(args);
});
process.exit(exitCode);
},
};
15 changes: 3 additions & 12 deletions packages/nx/src/command-line/sync/command-object.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { CommandModule } from 'yargs';
import { withVerbose } from '../yargs-utils/shared-options';

export interface SyncArgs {
verbose?: boolean;
Expand All @@ -10,12 +11,7 @@ export const yargsSyncCommand: CommandModule<
> = {
command: 'sync',
describe: false,
builder: (yargs) =>
yargs.option('verbose', {
type: 'boolean',
description:
'Prints additional information about the commands (e.g., stack traces)',
}),
builder: (yargs) => withVerbose(yargs),
handler: async (args) => {
process.exit(await import('./sync').then((m) => m.syncHandler(args)));
},
Expand All @@ -27,12 +23,7 @@ export const yargsSyncCheckCommand: CommandModule<
> = {
command: 'sync:check',
describe: false,
builder: (yargs) =>
yargs.option('verbose', {
type: 'boolean',
description:
'Prints additional information about the commands (e.g., stack traces)',
}),
builder: (yargs) => withVerbose(yargs),
handler: async (args) => {
process.exit(
await import('./sync').then((m) =>
Expand Down
7 changes: 1 addition & 6 deletions packages/nx/src/command-line/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,7 @@ interface SyncOptions extends SyncArgs {
}

export function syncHandler(options: SyncOptions): Promise<number> {
if (options.verbose) {
process.env.NX_VERBOSE_LOGGING = 'true';
}
const isVerbose = process.env.NX_VERBOSE_LOGGING === 'true';

return handleErrors(isVerbose, async () => {
return handleErrors(options.verbose, async () => {
const projectGraph = await createProjectGraphAsync();
const syncGenerators = await collectAllRegisteredSyncGenerators(
projectGraph
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/watch/command-object.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Argv, CommandModule } from 'yargs';
import { WatchArguments } from './watch';
import { linkToNxDevAndExamples } from '../yargs-utils/documentation';
import { parseCSV } from '../yargs-utils/shared-options';
import { parseCSV, withVerbose } from '../yargs-utils/shared-options';

export const yargsWatchCommand: CommandModule = {
command: 'watch',
Expand All @@ -13,7 +13,7 @@ export const yargsWatchCommand: CommandModule = {
};

function withWatchOptions(yargs: Argv) {
return yargs
return withVerbose(yargs)
.parserConfiguration({
'strip-dashed': true,
'populate--': true,
Expand Down
1 change: 1 addition & 0 deletions packages/nx/src/command-line/yargs-utils/shared-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export function withVerbose<T>(yargs: Argv<T>) {
type: 'boolean',
})
.middleware((args) => {
args.verbose = process.env.NX_VERBOSE_LOGGING === 'true';
if (args.verbose) {
process.env.NX_VERBOSE_LOGGING = 'true';
}
Expand Down

0 comments on commit 87ffb1b

Please sign in to comment.