Skip to content

Commit

Permalink
fix(core): re-enable CRA migration to Vite
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Feb 18, 2025
1 parent 3d63da2 commit 4c05e63
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,6 @@ function createTempWorkspace(options: NormalizedOptions) {

output.log({ title: '🧹 Clearing unused files' });

cpSync(
join('temp-workspace', 'apps', options.reactAppName, 'project.json'),
'project.json',
{ recursive: true }
);
rmSync(join('temp-workspace', 'apps', options.reactAppName), {
recursive: true,
force: true,
Expand Down Expand Up @@ -255,7 +250,6 @@ function moveFilesToTempWorkspace(options: NormalizedOptions) {

copyPackageJsonDepsFromTempWorkspace();
const requiredCraFiles = [
'project.json',
'package.json',
'src',
'public',
Expand Down
16 changes: 16 additions & 0 deletions packages/nx/src/command-line/init/implementation/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join } from 'path';

import { NxJsonConfiguration } from '../../../config/nx-json';
import {
directoryExists,
fileExists,
readJsonFile,
writeJsonFile,
Expand Down Expand Up @@ -336,3 +337,18 @@ export function isMonorepo(packageJson: PackageJson) {

return false;
}

export function isCRA(packageJson: PackageJson) {
const combinedDependencies = {
...packageJson.dependencies,
...packageJson.devDependencies,
};
return (
// Required dependencies for CRA projects
combinedDependencies['react'] &&
combinedDependencies['react-dom'] &&
combinedDependencies['react-scripts'] &&
directoryExists('src') &&
directoryExists('public')
);
}
22 changes: 19 additions & 3 deletions packages/nx/src/command-line/init/init-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import { readJsonFile } from '../../utils/fileutils';
import { getPackageNameFromImportPath } from '../../utils/get-package-name-from-import-path';
import { output } from '../../utils/output';
import { PackageJson } from '../../utils/package-json';
import { getPackageManagerCommand } from '../../utils/package-manager';
import {
detectPackageManager,
getPackageManagerCommand,
} from '../../utils/package-manager';
import { nxVersion } from '../../utils/versions';
import { globWithWorkspaceContextSync } from '../../utils/workspace-context';
import { connectExistingRepoToNxCloudPrompt } from '../connect/connect-to-nx-cloud';
Expand All @@ -24,10 +27,12 @@ import { generateDotNxSetup } from './implementation/dot-nx/add-nx-scripts';
import {
createNxJsonFile,
initCloud,
isCRA,
isMonorepo,
printFinalMessage,
updateGitIgnore,
} from './implementation/utils';
import { addNxToCraRepo } from './implementation/react';

export interface InitArgs {
interactive: boolean;
Expand Down Expand Up @@ -85,6 +90,7 @@ export async function initHandler(options: InitArgs): Promise<void> {
const packageJson: PackageJson = readJsonFile('package.json');
const _isTurborepo = existsSync('turbo.json');
const _isMonorepo = isMonorepo(packageJson);
const _isCRA = isCRA(packageJson);

const learnMoreLink = _isTurborepo
? 'https://nx.dev/recipes/adopting-nx/from-turborepo'
Expand All @@ -108,7 +114,18 @@ export async function initHandler(options: InitArgs): Promise<void> {
return;
}

if (_isMonorepo) {
const pmc = getPackageManagerCommand();

if (_isCRA) {
await addNxToCraRepo({
addE2e: false,
force: false,
vite: true,
integrated: false,
interactive: options.interactive,
nxCloud: false,
});
} else if (_isMonorepo) {
await addNxToMonorepo({
interactive: options.interactive,
nxCloud: false,
Expand All @@ -125,7 +142,6 @@ export async function initHandler(options: InitArgs): Promise<void> {
(options.interactive ? await connectExistingRepoToNxCloudPrompt() : false);

const repoRoot = process.cwd();
const pmc = getPackageManagerCommand();

createNxJsonFile(repoRoot, [], [], {});
updateGitIgnore(repoRoot);
Expand Down

0 comments on commit 4c05e63

Please sign in to comment.