Skip to content

Commit

Permalink
fix(core): improve the dx of the sync commands and gracefully handle …
Browse files Browse the repository at this point in the history
…exiting the prompt when running tasks (#27418)

- Update the `nx sync` output when there are changes to sync (currently
there's no output)
- Update the `nx sync` and `nx sync:check` output when there are no
changes to sync (currently there's no output)
- Handle exiting the prompt for applying sync generator changes when
running tasks

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
  • Loading branch information
leosvelperez committed Aug 14, 2024
1 parent ab162eb commit 8de36d7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
26 changes: 26 additions & 0 deletions packages/nx/src/command-line/sync/sync.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as ora from 'ora';
import { createProjectGraphAsync } from '../../project-graph/project-graph';
import { output } from '../../utils/output';
import { handleErrors } from '../../utils/params';
Expand All @@ -8,6 +9,7 @@ import {
syncGeneratorResultsToMessageLines,
} from '../../utils/sync-generators';
import type { SyncArgs } from './command-object';
import chalk = require('chalk');

interface SyncOptions extends SyncArgs {
check?: boolean;
Expand All @@ -27,6 +29,17 @@ export function syncHandler(options: SyncOptions): Promise<number> {
const results = await getSyncGeneratorChanges(syncGenerators);

if (!results.length) {
output.success({
title: options.check
? 'The workspace is up to date'
: 'The workspace is already up to date',
bodyLines: syncGenerators.map(
(generator) =>
`The ${chalk.bold(
generator
)} sync generator didn't identify any files in the workspace that are out of sync.`
),
});
return 0;
}

Expand All @@ -39,8 +52,21 @@ export function syncHandler(options: SyncOptions): Promise<number> {
return 1;
}

output.warn({
title: `The workspace is out of sync`,
bodyLines: syncGeneratorResultsToMessageLines(results),
});

const spinner = ora('Syncing the workspace...');
spinner.start();

await flushSyncGeneratorChanges(results);

spinner.succeed(`The workspace was synced successfully!
Please make sure to commit the changes to your repository.
`);

return 0;
});
}
52 changes: 28 additions & 24 deletions packages/nx/src/tasks-runner/run-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ async function ensureWorkspaceIsInSyncAndGetGraphs(
}

const outOfSyncTitle = 'The workspace is out of sync';
const resultBodyLines = syncGeneratorResultsToMessageLines(results);
const resultBodyLines = [...syncGeneratorResultsToMessageLines(results), ''];
const fixMessage =
'You can manually run `nx sync` to update your workspace or you can set `sync.applyChanges` to `true` in your `nx.json` to apply the changes automatically when running tasks.';
const willErrorOnCiMessage = 'Please note that this will be an error on CI.';
Expand Down Expand Up @@ -344,30 +344,34 @@ Please make sure to commit the changes to your repository.`);
}

async function promptForApplyingSyncGeneratorChanges(): Promise<boolean> {
const promptConfig = {
name: 'applyChanges',
type: 'select',
message:
'Would you like to sync the changes to get your worskpace up to date?',
choices: [
{
name: 'yes',
message: 'Yes, sync the changes and run the tasks',
},
{
name: 'no',
message: 'No, run the tasks without syncing the changes',
},
],
footer: () =>
chalk.dim(
'\nYou can skip this prompt by setting the `sync.applyChanges` option in your `nx.json`.'
),
};
try {
const promptConfig = {
name: 'applyChanges',
type: 'select',
message:
'Would you like to sync the changes to get your worskpace up to date?',
choices: [
{
name: 'yes',
message: 'Yes, sync the changes and run the tasks',
},
{
name: 'no',
message: 'No, run the tasks without syncing the changes',
},
],
footer: () =>
chalk.dim(
'\nYou can skip this prompt by setting the `sync.applyChanges` option in your `nx.json`.'
),
};

return await prompt<{ applyChanges: 'yes' | 'no' }>([promptConfig]).then(
({ applyChanges }) => applyChanges === 'yes'
);
return await prompt<{ applyChanges: 'yes' | 'no' }>([promptConfig]).then(
({ applyChanges }) => applyChanges === 'yes'
);
} catch {
process.exit(1);
}
}

function setEnvVarsBasedOnArgs(nxArgs: NxArgs, loadDotEnvFiles: boolean) {
Expand Down
1 change: 0 additions & 1 deletion packages/nx/src/utils/sync-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ export function syncGeneratorResultsToMessageLines(
if (result.outOfSyncMessage) {
messageLines.push(result.outOfSyncMessage);
}
messageLines.push('');
}

return messageLines;
Expand Down

0 comments on commit 8de36d7

Please sign in to comment.