1- import { detectPackageManager } from '@nrwl/tao/src/shared/package-manager' ;
2-
1+ import { tmpProjPath } from '@nrwl/nx-plugin/testing' ;
32import { ChildProcess , exec , execSync } from 'child_process' ;
43import {
54 copySync ,
@@ -17,155 +16,23 @@ import * as path from 'path';
1716import { dirSync } from 'tmp' ;
1817
1918import isCI = require( 'is-ci' ) ;
19+ import { workspaceConfigName } from 'nx/src/config/workspaces' ;
20+ import { detectPackageManager } from '@nrwl/devkit' ;
2021interface RunCmdOpts {
2122 silenceError ?: boolean ;
2223 env ?: Record < string , string > | NodeJS . ProcessEnv ;
2324 cwd ?: string ;
2425 silent ?: boolean ;
2526}
2627
27- export function currentCli ( ) {
28- return process . env . SELECTED_CLI ?? 'nx' ;
29- }
30-
31- export const e2eRoot = isCI ? dirSync ( { prefix : 'nx-e2e-' } ) . name : `./tmp` ;
32- export const e2eCwd = `${ e2eRoot } /${ currentCli ( ) } ` ;
33- ensureDirSync ( e2eCwd ) ;
34-
35- let projName : string ;
36-
3728export function uniq ( prefix : string ) {
3829 return `${ prefix } ${ Math . floor ( Math . random ( ) * 10000000 ) } ` ;
3930}
4031
41- export function workspaceConfigName ( ) {
42- return currentCli ( ) === 'angular' ? 'angular.json' : 'workspace.json' ;
43- }
44-
45- export function updateWorkspaceConfig (
46- callback : ( json : { [ key : string ] : any } ) => Object ,
47- ) {
48- const file = workspaceConfigName ( ) ;
49- updateFile ( file , JSON . stringify ( callback ( readJson ( file ) ) , null , 2 ) ) ;
50- }
51-
52- export function runCreateWorkspace (
53- name : string ,
54- {
55- preset,
56- appName,
57- style,
58- base,
59- packageManager,
60- cli,
61- extraArgs,
62- } : {
63- preset : string ;
64- appName ?: string ;
65- style ?: string ;
66- base ?: string ;
67- packageManager ?: 'npm' | 'yarn' | 'pnpm' ;
68- cli ?: string ;
69- extraArgs ?: string ;
70- } ,
71- ) {
72- projName = name ;
73-
74- const pm = getPackageManagerCommand ( { packageManager } ) ;
75-
76- const linterArg =
77- preset === 'angular' || preset === 'angular-nest' ? ' --linter=tslint' : '' ;
78- let command = `${ pm . createWorkspace } ${ name } --cli=${
79- cli || currentCli ( )
80- } --preset=${ preset } ${ linterArg } --no-nxCloud --no-interactive`;
81- if ( appName ) {
82- command += ` --appName=${ appName } ` ;
83- }
84- if ( style ) {
85- command += ` --style=${ style } ` ;
86- }
87-
88- if ( base ) {
89- command += ` --defaultBase="${ base } "` ;
90- }
91-
92- if ( packageManager ) {
93- command += ` --package-manager=${ packageManager } ` ;
94- }
95-
96- if ( extraArgs ) {
97- command += ` ${ extraArgs } ` ;
98- }
99-
100- const create = execSync ( command , {
101- cwd : e2eCwd ,
102- stdio : [ 0 , 1 , 2 ] ,
103- env : process . env ,
104- } ) ;
105- return create ? create . toString ( ) : '' ;
106- }
107-
108- export function packageInstall ( pkg : string , projName ?: string ) {
109- const cwd = projName ? `${ e2eCwd } /${ projName } ` : tmpProjPath ( ) ;
110- const pm = getPackageManagerCommand ( { path : cwd } ) ;
111- const install = execSync ( `${ pm . addDev } ${ pkg } ` , {
112- cwd,
113- // ...{ stdio: ['pipe', 'pipe', 'pipe'] },
114- ...{ stdio : [ 0 , 1 , 2 ] } ,
115- env : process . env ,
116- } ) ;
117- return install ? install . toString ( ) : '' ;
118- }
119-
120- export function runNgNew ( ) : string {
121- return execSync ( `../../node_modules/.bin/ng new proj --no-interactive` , {
122- cwd : e2eCwd ,
123- env : process . env ,
124- } ) . toString ( ) ;
125- }
126-
12732export function getSelectedPackageManager ( ) : 'npm' | 'yarn' | 'pnpm' {
12833 return process . env . SELECTED_PM as 'npm' | 'yarn' | 'pnpm' ;
12934}
13035
131- /**
132- * Sets up a new project in the temporary project path
133- * for the currently selected CLI.
134- */
135- export function newProject ( { name = uniq ( 'proj' ) } = { } ) : string {
136- const packageManager = getSelectedPackageManager ( ) ;
137-
138- try {
139- const useBackupProject = packageManager !== 'pnpm' ;
140- const projScope = useBackupProject ? 'proj' : name ;
141-
142- if ( ! useBackupProject || ! directoryExists ( tmpBackupProjPath ( ) ) ) {
143- runCreateWorkspace ( projScope , { preset : 'empty' , packageManager } ) ;
144-
145- // Temporary hack to prevent installing with `--frozen-lockfile`
146- if ( isCI && packageManager === 'pnpm' ) {
147- updateFile ( '.npmrc' , 'prefer-frozen-lockfile=false' ) ;
148- }
149-
150- const packages = [ `@nx-dotnet/core` , `@nx-dotnet/typescript` ] ;
151- packageInstall ( packages . join ( ` ` ) , projScope ) ;
152-
153- if ( useBackupProject ) {
154- moveSync ( `${ e2eCwd } /proj` , `${ tmpBackupProjPath ( ) } ` ) ;
155- }
156- }
157- projName = name ;
158- if ( useBackupProject ) {
159- copySync ( `${ tmpBackupProjPath ( ) } ` , `${ tmpProjPath ( ) } ` ) ;
160- }
161- return projScope ;
162- } catch ( e : any ) {
163- console . log ( `Failed to set up project for e2e tests.` ) ;
164- console . log ( e . message ) ;
165- throw e ;
166- }
167- }
168-
16936// Useful in order to cleanup space during CI to prevent `No space left on device` exceptions
17037export function removeProject ( { onlyOnCI = false } = { } ) {
17138 if ( onlyOnCI && ! isCI ) {
@@ -255,38 +122,6 @@ export function runCLIAsync(
255122 ) ;
256123}
257124
258- export function runNgAdd (
259- command ?: string ,
260- opts : RunCmdOpts = {
261- silenceError : false ,
262- env : process . env as Record < string , string > ,
263- cwd : tmpProjPath ( ) ,
264- } ,
265- ) : string {
266- try {
267- packageInstall ( '@nrwl/workspace' ) ;
268- return execSync (
269- `./node_modules/.bin/ng g @nrwl/workspace:ng-add ${ command } ` ,
270- {
271- cwd : tmpProjPath ( ) ,
272- env : opts . env as any ,
273- } ,
274- )
275- . toString ( )
276- . replace (
277- / [ \u001b \u009b ] [ [ ( ) # ; ? ] * (?: [ 0 - 9 ] { 1 , 4 } (?: ; [ 0 - 9 ] { 0 , 4 } ) * ) ? [ 0 - 9 A - O R Z c f - n q r y = > < ] / g,
278- '' ,
279- ) ;
280- } catch ( e : any ) {
281- if ( opts . silenceError ) {
282- return e . stdout . toString ( ) ;
283- } else {
284- console . log ( e . stdout . toString ( ) , e . stderr . toString ( ) ) ;
285- throw e ;
286- }
287- }
288- }
289-
290125export function runCLI (
291126 command ?: string ,
292127 opts : RunCmdOpts = {
@@ -354,7 +189,7 @@ export function runCommand(command: string): string {
354189 */
355190function setMaxWorkers ( ) {
356191 if ( isCI ) {
357- const workspaceFile = workspaceConfigName ( ) ;
192+ const workspaceFile = workspaceConfigName ( tmpProjPath ( ) ) ;
358193 const workspace = readJson ( workspaceFile ) ;
359194
360195 Object . keys ( workspace . projects ) . forEach ( ( appName ) => {
@@ -466,17 +301,9 @@ export function getSize(filePath: string): number {
466301 return statSync ( filePath ) . size ;
467302}
468303
469- export function tmpProjPath ( path ?: string ) {
470- return path ? `${ e2eCwd } /${ projName } /${ path } ` : `${ e2eCwd } /${ projName } ` ;
471- }
472-
473- function tmpBackupProjPath ( path ?: string ) {
474- return path ? `${ e2eCwd } /proj-backup/${ path } ` : `${ e2eCwd } /proj-backup` ;
475- }
476-
477304export function getPackageManagerCommand ( {
478- path = tmpProjPath ( ) ,
479- packageManager = detectPackageManager ( path ) ,
305+ p = tmpProjPath ( ) as string ,
306+ packageManager = detectPackageManager ( p ) ,
480307 scriptsPrependNodePath = true ,
481308} = { } ) : {
482309 createWorkspace : string ;
0 commit comments