From 1d61d675e98f19de7c63a2fc7c4f59d4b387ae73 Mon Sep 17 00:00:00 2001 From: Jack Hsu Date: Wed, 11 Sep 2024 09:28:43 -0400 Subject: [PATCH] feat(core): import warns when source and destination directories are different --- packages/nx/src/command-line/import/import.ts | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/packages/nx/src/command-line/import/import.ts b/packages/nx/src/command-line/import/import.ts index 21d91e4cc7bd6..03423cd728f19 100644 --- a/packages/nx/src/command-line/import/import.ts +++ b/packages/nx/src/command-line/import/import.ts @@ -1,4 +1,4 @@ -import { dirname, join, relative, resolve } from 'path'; +import { dirname, isAbsolute, join, relative, resolve } from 'path'; import { minimatch } from 'minimatch'; import { existsSync, promises as fsp } from 'node:fs'; import * as chalk from 'chalk'; @@ -177,6 +177,13 @@ export async function importHandler(options: ImportOptions) { } const absSource = join(sourceTempRepoPath, source); + + if (isAbsolute(destination)) { + throw new Error( + `The destination directory must be a relative path in this repository.` + ); + } + const absDestination = join(process.cwd(), destination); const destinationGitClient = new GitRepository(process.cwd()); @@ -210,6 +217,8 @@ export async function importHandler(options: ImportOptions) { packageManager ); + const sourceIsNxWorkspace = existsSync(join(sourceGitClient.root, 'nx.json')); + const relativeDestination = relative( destinationGitClient.root, absDestination @@ -310,6 +319,19 @@ export async function importHandler(options: ImportOptions) { await warnOnMissingWorkspacesEntry(packageManager, pmc, relativeDestination); + if (source != destination) { + output.warn({ + title: `Check configuration files`, + bodyLines: [ + `The source directory (${source}) and destination directory (${destination}) are different.`, + `You may need to update configuration files to match the directory in this repository.`, + sourceIsNxWorkspace + ? `For example, path options in project.json such as "main", "tsConfig", and "outputPath" need to be updated.` + : `For example, relative paths in tsconfig.json and other tooling configuration files may need to be updated.`, + ], + }); + } + // When only a subdirectory is imported, there might be devDependencies in the root package.json file // that needs to be ported over as well. if (ref) {