From 153451f32b88bb37fcb8137802a0f94db813f8e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leosvel=20P=C3=A9rez=20Espinosa?= Date: Fri, 27 Sep 2024 20:14:35 +0200 Subject: [PATCH] fix(js): improve typescript-sync generator messaging (#28162) ## Current Behavior ## Expected Behavior ## Related Issue(s) Fixes # --- .../typescript-sync/typescript-sync.spec.ts | 4 ++-- .../typescript-sync/typescript-sync.ts | 19 +++++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/js/src/generators/typescript-sync/typescript-sync.spec.ts b/packages/js/src/generators/typescript-sync/typescript-sync.spec.ts index a01f3ca306bc7..69bb2b140a53f 100644 --- a/packages/js/src/generators/typescript-sync/typescript-sync.spec.ts +++ b/packages/js/src/generators/typescript-sync/typescript-sync.spec.ts @@ -92,7 +92,7 @@ describe('syncGenerator()', () => { writeJson(tree, 'nx.json', nxJson); await expect(syncGenerator(tree)).rejects.toMatchInlineSnapshot( - `[Error: The @nx/js/typescript plugin must be added to the "plugins" array in nx.json before syncing tsconfigs]` + `[SyncError: The "@nx/js/typescript" plugin is not registered]` ); }); @@ -100,7 +100,7 @@ describe('syncGenerator()', () => { tree.delete('tsconfig.json'); await expect(syncGenerator(tree)).rejects.toMatchInlineSnapshot( - `[Error: A "tsconfig.json" file must exist in the workspace root in order to use this sync generator.]` + `[SyncError: Missing root "tsconfig.json"]` ); }); diff --git a/packages/js/src/generators/typescript-sync/typescript-sync.ts b/packages/js/src/generators/typescript-sync/typescript-sync.ts index 75469689a4f41..08ed49fb49029 100644 --- a/packages/js/src/generators/typescript-sync/typescript-sync.ts +++ b/packages/js/src/generators/typescript-sync/typescript-sync.ts @@ -13,7 +13,10 @@ import { import ignore from 'ignore'; import { applyEdits, modify } from 'jsonc-parser'; import { dirname, normalize, relative } from 'node:path/posix'; -import type { SyncGeneratorResult } from 'nx/src/utils/sync-generators'; +import { + SyncError, + type SyncGeneratorResult, +} from 'nx/src/utils/sync-generators'; import * as ts from 'typescript'; import { PLUGIN_NAME, @@ -63,17 +66,17 @@ export async function syncGenerator(tree: Tree): Promise { } ); if (!tscPluginConfig) { - throw new Error( - `The ${PLUGIN_NAME} plugin must be added to the "plugins" array in nx.json before syncing tsconfigs` - ); + throw new SyncError(`The "${PLUGIN_NAME}" plugin is not registered`, [ + `The "${PLUGIN_NAME}" plugin must be added to the "plugins" array in "nx.json" in order to sync the project graph information to the TypeScript configuration files.`, + ]); } // Root tsconfig containing project references for the whole workspace const rootTsconfigPath = 'tsconfig.json'; if (!tree.exists(rootTsconfigPath)) { - throw new Error( - `A "tsconfig.json" file must exist in the workspace root in order to use this sync generator.` - ); + throw new SyncError('Missing root "tsconfig.json"', [ + `A "tsconfig.json" file must exist in the workspace root in order to sync the project graph information to the TypeScript configuration files.`, + ]); } const rawTsconfigContentsCache = new Map(); @@ -254,7 +257,7 @@ export async function syncGenerator(tree: Tree): Promise { return { outOfSyncMessage: - 'Based on the workspace project graph, some TypeScript configuration files are missing project references to the projects they depend on.', + 'Based on the workspace project graph, some TypeScript configuration files are missing project references to the projects they depend on or contain outdated project references.', }; } }