Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): improve the dx of the sync commands and gracefully handle exiting the prompt when running tasks #27418

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (e) {
leosvelperez marked this conversation as resolved.
Show resolved Hide resolved
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