1
+ import { mkdtempSync , realpathSync } from 'fs' ;
2
+ import { tmpdir } from 'os' ;
3
+ import { delimiter , join } from 'path' ;
4
+ import * as semver from 'semver' ;
1
5
import { rimraf } from '../../utils/fs' ;
2
6
import { getActivePackageManager } from '../../utils/packages' ;
3
- import { ng , npm } from '../../utils/process' ;
7
+ import { exec , execAndWaitForOutputToMatch , ng , npm } from '../../utils/process' ;
4
8
import { isPrereleaseCli } from '../../utils/project' ;
5
- import { expectToFail } from '../../utils/utils' ;
6
9
7
- const warningText = ' npm version 7.5 .6 or higher is recommended' ;
10
+ const warningTextRe = / n p m v e r s i o n 7 \. 5 \ .6 o r h i g h e r i s r e c o m m e n d e d / ;
8
11
9
12
export default async function ( ) {
10
13
// Only relevant with npm as a package manager
@@ -24,61 +27,60 @@ export default async function () {
24
27
extraArgs . push ( '--next' ) ;
25
28
}
26
29
30
+ // Add the npmModuleDir to the PATH so the test-installed npm is used
31
+ process . env . PATH = join ( currentDirectory , 'node_modules' , '.bin' ) + delimiter + process . env . PATH ;
32
+
27
33
try {
28
- // Install version >=7.5.6
29
- await npm ( 'install' , '--global' , 'npm@>=7.5.6' ) ;
34
+ // Install and verify npm version >=7.5.6
35
+ await npm ( 'install' , 'npm@>=7.5.6' ) ;
36
+ const { stdout : npm75Version } = await exec ( 'npm' , '--version' ) ;
37
+ if ( ! semver . gte ( npm75Version , '7.5.6' ) ) {
38
+ throw new Error ( 'npm install >=7.5.6 failed' ) ;
39
+ }
30
40
31
41
// Ensure `ng update` does not show npm warning
32
42
const { stderr : stderrUpdate1 } = await ng ( 'update' , ...extraArgs ) ;
33
- if ( stderrUpdate1 . includes ( warningText ) ) {
43
+ if ( stderrUpdate1 . match ( warningTextRe ) ) {
34
44
throw new Error ( 'ng update expected to not show npm version warning.' ) ;
35
45
}
36
46
37
- // Install version <7.5.6
38
- await npm ( 'install' , '--global' , '[email protected] ' ) ;
47
+ // Install and verify npm version <7.5.6
48
+ await npm ( 'install' , '[email protected] ' ) ;
49
+ const { stdout : npm74Version } = await exec ( 'npm' , '--version' ) ;
50
+ if ( ! semver . eq ( npm74Version , '7.4.0' ) ) {
51
+ throw new Error ( 'npm install =7.4.0 failed' ) ;
52
+ }
39
53
40
54
// Ensure `ng add` shows npm warning
41
- const { stderr : stderrAdd } = await ng ( 'add' , '@angular/localize' ) ;
42
- if ( ! stderrAdd . includes ( warningText ) ) {
43
- throw new Error ( 'ng add expected to show npm version warning.' ) ;
44
- }
55
+ await execAndWaitForOutputToMatch ( 'ng' , [ 'add' , '@angular/localize' ] , warningTextRe ) ;
45
56
46
57
// Ensure `ng update` shows npm warning
47
- const { stderr : stderrUpdate2 } = await ng ( 'update' , ...extraArgs ) ;
48
- if ( ! stderrUpdate2 . includes ( warningText ) ) {
49
- throw new Error ( 'ng update expected to show npm version warning.' ) ;
50
- }
58
+ await execAndWaitForOutputToMatch ( 'ng' , [ 'update' , ...extraArgs ] , warningTextRe ) ;
51
59
52
60
// Ensure `ng build` executes successfully
53
61
const { stderr : stderrBuild } = await ng ( 'build' , '--configuration=development' ) ;
54
- if ( stderrBuild . includes ( warningText ) ) {
62
+ if ( stderrBuild . match ( warningTextRe ) ) {
55
63
throw new Error ( 'ng build expected to not show npm version warning.' ) ;
56
64
}
57
65
58
66
// Ensure `ng new` shows npm warning
59
67
// Must be outside the project for `ng new`
60
68
process . chdir ( '..' ) ;
61
- const { message : stderrNew } = await expectToFail ( ( ) => ng ( 'new' ) ) ;
62
- if ( ! stderrNew . includes ( warningText ) ) {
63
- throw new Error ( 'ng new expected to show npm version warning.' ) ;
64
- }
69
+ await execAndWaitForOutputToMatch ( 'ng' , [ 'new' ] , warningTextRe ) ;
65
70
66
71
// Ensure `ng new --package-manager=npm` shows npm warning
67
- const { message : stderrNewNpm } = await expectToFail ( ( ) => ng ( 'new' , '--package-manager=npm' ) ) ;
68
- if ( ! stderrNewNpm . includes ( warningText ) ) {
69
- throw new Error ( 'ng new expected to show npm version warning.' ) ;
70
- }
72
+ await execAndWaitForOutputToMatch ( 'ng' , [ 'new' , '--package-manager=npm' ] , warningTextRe ) ;
71
73
72
74
// Ensure `ng new --skip-install` executes successfully
73
75
const { stderr : stderrNewSkipInstall } = await ng ( 'new' , 'npm-seven-skip' , '--skip-install' ) ;
74
- if ( stderrNewSkipInstall . includes ( warningText ) ) {
76
+ if ( stderrNewSkipInstall . match ( warningTextRe ) ) {
75
77
throw new Error ( 'ng new --skip-install expected to not show npm version warning.' ) ;
76
78
}
77
79
78
80
// Ensure `ng new --package-manager=yarn` executes successfully
79
81
// Need an additional npmrc file since yarn does not use the NPM registry environment variable
80
82
const { stderr : stderrNewYarn } = await ng ( 'new' , 'npm-seven-yarn' , '--package-manager=yarn' ) ;
81
- if ( stderrNewYarn . includes ( warningText ) ) {
83
+ if ( stderrNewYarn . match ( warningTextRe ) ) {
82
84
throw new Error ( 'ng new --package-manager=yarn expected to not show npm version warning.' ) ;
83
85
}
84
86
} finally {
@@ -89,7 +91,7 @@ export default async function () {
89
91
// Change directory back
90
92
process . chdir ( currentDirectory ) ;
91
93
92
- // Reset version back to 6.x
93
- await npm ( 'install ' , '--global' , ' npm@6 ') ;
94
+ // Remove the locally installed npm
95
+ await npm ( 'uninstall ' , 'npm' ) ;
94
96
}
95
97
}
0 commit comments