From 8de36d7f14cb4c437d9bef5b4ad010e40b23c8ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Wed, 14 Aug 2024 16:27:49 +0200 Subject: [PATCH] fix(core): improve the dx of the sync commands and gracefully handle 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 ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes # --- packages/nx/src/command-line/sync/sync.ts | 26 +++++++++++ packages/nx/src/tasks-runner/run-command.ts | 52 +++++++++++---------- packages/nx/src/utils/sync-generators.ts | 1 - 3 files changed, 54 insertions(+), 25 deletions(-) diff --git a/packages/nx/src/command-line/sync/sync.ts b/packages/nx/src/command-line/sync/sync.ts index 4ca84704a7..2416d8f9e2 100644 --- a/packages/nx/src/command-line/sync/sync.ts +++ b/packages/nx/src/command-line/sync/sync.ts @@ -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'; @@ -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; @@ -27,6 +29,17 @@ export function syncHandler(options: SyncOptions): Promise { 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; } @@ -39,8 +52,21 @@ export function syncHandler(options: SyncOptions): Promise { 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; }); } diff --git a/packages/nx/src/tasks-runner/run-command.ts b/packages/nx/src/tasks-runner/run-command.ts index 222fa3f745..4229be93c5 100644 --- a/packages/nx/src/tasks-runner/run-command.ts +++ b/packages/nx/src/tasks-runner/run-command.ts @@ -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.'; @@ -344,30 +344,34 @@ Please make sure to commit the changes to your repository.`); } async function promptForApplyingSyncGeneratorChanges(): Promise { - 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) { diff --git a/packages/nx/src/utils/sync-generators.ts b/packages/nx/src/utils/sync-generators.ts index e6598caeed..93a64bad26 100644 --- a/packages/nx/src/utils/sync-generators.ts +++ b/packages/nx/src/utils/sync-generators.ts @@ -182,7 +182,6 @@ export function syncGeneratorResultsToMessageLines( if (result.outOfSyncMessage) { messageLines.push(result.outOfSyncMessage); } - messageLines.push(''); } return messageLines;