feat(core): import warns when source and destination directories are different (#27875)

This PR adds a warning when the user choose different source and
destination roots. This is a problem for Nx workspaces using path
options in `project.json`, and possibly other config files such as
`tsconfig.json`, `jest.config.ts`, etc.

Note: Also included a guard that the destination directory isn't an
absolute path like `/tmp/foo`, because the behavior will not work as
expected.

The message when the source is an Nx workspace:
<img width="1392" alt="Screenshot 2024-09-11 at 9 32 54 AM"
src="https://github.com/user-attachments/assets/c8ebedba-fd66-4dbf-ada9-eacf86bd67fc">


The message when the source is not an Nx workspace:
<img width="1392" alt="Screenshot 2024-09-11 at 9 30 44 AM"
src="https://github.com/user-attachments/assets/ade9fbd1-4d5d-4d0c-93f6-eaad176af333">
This commit is contained in:
Jack Hsu 2024-09-11 15:27:55 -04:00 committed by GitHub
parent 24edc5ad99
commit 8b177bd60e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,4 @@
import { dirname, join, relative, resolve } from 'path'; import { dirname, isAbsolute, join, relative, resolve } from 'path';
import { minimatch } from 'minimatch'; import { minimatch } from 'minimatch';
import { existsSync, promises as fsp } from 'node:fs'; import { existsSync, promises as fsp } from 'node:fs';
import * as chalk from 'chalk'; import * as chalk from 'chalk';
@ -177,6 +177,13 @@ export async function importHandler(options: ImportOptions) {
} }
const absSource = join(sourceTempRepoPath, source); 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 absDestination = join(process.cwd(), destination);
const destinationGitClient = new GitRepository(process.cwd()); const destinationGitClient = new GitRepository(process.cwd());
@ -210,6 +217,8 @@ export async function importHandler(options: ImportOptions) {
packageManager packageManager
); );
const sourceIsNxWorkspace = existsSync(join(sourceGitClient.root, 'nx.json'));
const relativeDestination = relative( const relativeDestination = relative(
destinationGitClient.root, destinationGitClient.root,
absDestination absDestination
@ -310,6 +319,19 @@ export async function importHandler(options: ImportOptions) {
await warnOnMissingWorkspacesEntry(packageManager, pmc, relativeDestination); 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 // When only a subdirectory is imported, there might be devDependencies in the root package.json file
// that needs to be ported over as well. // that needs to be ported over as well.
if (ref) { if (ref) {