1- #!/usr/bin/env node
2- import { fileExists } from '@nrwl/workspace/src/utilities/fileutils' ;
31import { execSync } from 'child_process' ;
2+ import { copySync , moveSync , removeSync , readdirSync } from 'fs-extra' ;
3+
4+ import { fileExists , readJsonFile } from 'nx/src/utils/fileutils' ;
5+ import { output } from 'nx/src/utils/output' ;
46import {
5- copySync ,
6- existsSync ,
7- moveSync ,
8- readJsonSync ,
9- removeSync ,
10- readdirSync ,
11- } from 'fs-extra' ;
12- import { output } from '@nrwl/devkit' ;
7+ detectPackageManager ,
8+ getPackageManagerCommand ,
9+ PackageManagerCommands ,
10+ } from 'nx/src/utils/package-manager' ;
1311
1412import { addCRAcracoScriptsToPackageJson } from './add-cra-commands-to-nx' ;
1513import { checkForUncommittedChanges } from './check-for-uncommitted-changes' ;
@@ -19,31 +17,15 @@ import { setupTsConfig } from './tsconfig-setup';
1917import { writeCracoConfig } from './write-craco-config' ;
2018import { cleanUpFiles } from './clean-up-files' ;
2119
22- let packageManager : string ;
23- function checkPackageManager ( ) {
24- packageManager = existsSync ( 'yarn.lock' )
25- ? 'yarn'
26- : existsSync ( 'pnpm-lock.yaml' )
27- ? 'pnpm'
28- : 'npm' ;
29- }
30-
31- function addDependency ( dep : string , dev ?: boolean ) {
20+ function addDependency ( pmc : PackageManagerCommands , dep : string ) {
3221 output . log ( { title : `📦 Adding dependency: ${ dep } ` } ) ;
33- if ( packageManager === 'yarn' ) {
34- execSync ( `yarn add ${ dev ? '-D ' : '' } ${ dep } ` , { stdio : [ 0 , 1 , 2 ] } ) ;
35- } else if ( packageManager === 'pnpm' ) {
36- execSync ( `pnpm i ${ dev ? '--save-dev ' : '' } ${ dep } ` , { stdio : [ 0 , 1 , 2 ] } ) ;
37- } else {
38- execSync ( `npm i --force ${ dev ? '--save-dev ' : '' } ${ dep } ` , {
39- stdio : [ 0 , 1 , 2 ] ,
40- } ) ;
41- }
22+ execSync ( `${ pmc . addDev } ${ dep } ` , { stdio : [ 0 , 1 , 2 ] } ) ;
4223}
4324
4425export async function createNxWorkspaceForReact ( options : Record < string , any > ) {
4526 checkForUncommittedChanges ( ) ;
46- checkPackageManager ( ) ;
27+ const packageManager = detectPackageManager ( ) ;
28+ const pmc = getPackageManagerCommand ( packageManager ) ;
4729
4830 output . log ( { title : '🐳 Nx initialization' } ) ;
4931
@@ -54,7 +36,7 @@ export async function createNxWorkspaceForReact(options: Record<string, any>) {
5436 }
5537
5638 const reactAppName = readNameFromPackageJson ( ) ;
57- const packageJson = readJsonSync ( 'package.json' ) ;
39+ const packageJson = readJsonFile ( 'package.json' ) ;
5840 const deps = {
5941 ...packageJson . dependencies ,
6042 ...packageJson . devDependencies ,
@@ -159,17 +141,15 @@ export async function createNxWorkspaceForReact(options: Record<string, any>) {
159141 title : '🧶 Adding npm packages to your new Nx workspace to support CRA' ,
160142 } ) ;
161143
162- addDependency ( 'react-scripts' , true ) ;
163- addDependency ( '@testing-library/jest-dom' , true ) ;
164- addDependency ( 'eslint-config-react-app' , true ) ;
165- addDependency ( '@craco/craco' , true ) ;
166- addDependency ( 'web-vitals' , true ) ;
167- addDependency ( 'jest-watch-typeahead' , true ) ; // Only for ts apps?
168- addDependency ( 'cross-env' , true ) ;
144+ addDependency ( pmc , 'react-scripts' ) ;
145+ addDependency ( pmc , '@testing-library/jest-dom' ) ;
146+ addDependency ( pmc , 'eslint-config-react-app' ) ;
147+ addDependency ( pmc , '@craco/craco' ) ;
148+ addDependency ( pmc , 'web-vitals' ) ;
149+ addDependency ( pmc , 'jest-watch-typeahead' ) ; // Only for ts apps?
150+ addDependency ( pmc , 'cross-env' ) ;
169151
170- output . log ( {
171- title : '🎉 Done!' ,
172- } ) ;
152+ output . log ( { title : '🎉 Done!' } ) ;
173153 output . note ( {
174154 title : 'First time using Nx? Check out this interactive Nx tutorial.' ,
175155 bodyLines : [
0 commit comments