diff --git a/packages/nx/src/command-line/sync/sync.ts b/packages/nx/src/command-line/sync/sync.ts index 4ca84704a7632..2416d8f9e20c4 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 222fa3f745207..4229be93c553a 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 e6598caeedf87..93a64bad26097 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;