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
@@ -25,60 +28,60 @@ export default async function () {
25
28
}
26
29
27
30
try {
28
- // Install version >=7.5.6
29
- await npm ( 'install' , '--global' , 'npm@>=7.5.6' ) ;
31
+ // Add the npmModuleDir to the PATH so the test-installed npm is used
32
+ process . env . PATH =
33
+ join ( currentDirectory , 'node_modules' , '.bin' ) + delimiter + process . env . PATH ;
34
+
35
+ // Install and verify npm version >=7.5.6
36
+ await npm ( 'install' , 'npm@>=7.5.6' ) ;
37
+ const { stdout : npm75Version } = await exec ( 'npm' , '--version' ) ;
38
+ if ( ! semver . gte ( npm75Version , '7.5.6' ) ) {
39
+ throw new Error ( 'npm install >=7.5.6 failed' ) ;
40
+ }
30
41
31
42
// Ensure `ng update` does not show npm warning
32
43
const { stderr : stderrUpdate1 } = await ng ( 'update' , ...extraArgs ) ;
33
- if ( stderrUpdate1 . includes ( warningText ) ) {
44
+ if ( stderrUpdate1 . match ( warningTextRe ) ) {
34
45
throw new Error ( 'ng update expected to not show npm version warning.' ) ;
35
46
}
36
47
37
- // Install version <7.5.6
38
- await npm ( 'install' , '--global' , '[email protected] ' ) ;
48
+ // Install and verify npm version <7.5.6
49
+ await npm ( 'install' , '[email protected] ' ) ;
50
+ const { stdout : npm74Version } = await exec ( 'npm' , '--version' ) ;
51
+ if ( ! semver . eq ( npm74Version , '7.4.0' ) ) {
52
+ throw new Error ( 'npm install =7.4.0 failed' ) ;
53
+ }
39
54
40
55
// 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
- }
56
+ await execAndWaitForOutputToMatch ( 'ng' , [ 'add' , '@angular/localize' ] , warningTextRe ) ;
45
57
46
58
// 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
- }
59
+ await execAndWaitForOutputToMatch ( 'ng' , [ 'update' , ...extraArgs ] , warningTextRe ) ;
51
60
52
61
// Ensure `ng build` executes successfully
53
62
const { stderr : stderrBuild } = await ng ( 'build' , '--configuration=development' ) ;
54
- if ( stderrBuild . includes ( warningText ) ) {
63
+ if ( stderrBuild . match ( warningTextRe ) ) {
55
64
throw new Error ( 'ng build expected to not show npm version warning.' ) ;
56
65
}
57
66
58
67
// Ensure `ng new` shows npm warning
59
68
// Must be outside the project for `ng new`
60
69
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
- }
70
+ await execAndWaitForOutputToMatch ( 'ng' , [ 'new' ] , warningTextRe ) ;
65
71
66
72
// 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
- }
73
+ await execAndWaitForOutputToMatch ( 'ng' , [ 'new' , '--package-manager=npm' ] , warningTextRe ) ;
71
74
72
75
// Ensure `ng new --skip-install` executes successfully
73
76
const { stderr : stderrNewSkipInstall } = await ng ( 'new' , 'npm-seven-skip' , '--skip-install' ) ;
74
- if ( stderrNewSkipInstall . includes ( warningText ) ) {
77
+ if ( stderrNewSkipInstall . match ( warningTextRe ) ) {
75
78
throw new Error ( 'ng new --skip-install expected to not show npm version warning.' ) ;
76
79
}
77
80
78
81
// Ensure `ng new --package-manager=yarn` executes successfully
79
82
// Need an additional npmrc file since yarn does not use the NPM registry environment variable
80
83
const { stderr : stderrNewYarn } = await ng ( 'new' , 'npm-seven-yarn' , '--package-manager=yarn' ) ;
81
- if ( stderrNewYarn . includes ( warningText ) ) {
84
+ if ( stderrNewYarn . match ( warningTextRe ) ) {
82
85
throw new Error ( 'ng new --package-manager=yarn expected to not show npm version warning.' ) ;
83
86
}
84
87
} finally {
@@ -89,7 +92,7 @@ export default async function () {
89
92
// Change directory back
90
93
process . chdir ( currentDirectory ) ;
91
94
92
- // Reset version back to 6.x
93
- await npm ( 'install ' , '--global' , ' npm@6 ') ;
95
+ // Remove the locally installed npm
96
+ await npm ( 'uninstall ' , 'npm' ) ;
94
97
}
95
98
}
0 commit comments