8
8
9
9
/* eslint-disable no-console */
10
10
11
- import { tags } from '@angular-devkit/core ' ;
11
+ import { createRequire } from 'node:module ' ;
12
12
import { SemVer , satisfies } from 'semver' ;
13
13
14
14
export function assertCompatibleAngularVersion ( projectRoot : string ) : void | never {
15
15
let angularCliPkgJson ;
16
16
let angularPkgJson ;
17
- const resolveOptions = { paths : [ projectRoot ] } ;
17
+
18
+ // Create a custom require function for ESM compliance.
19
+ // NOTE: The trailing slash is significant.
20
+ const projectRequire = createRequire ( projectRoot + '/' ) ;
18
21
19
22
try {
20
- const angularPackagePath = require . resolve ( '@angular/core/package.json' , resolveOptions ) ;
23
+ const angularPackagePath = projectRequire . resolve ( '@angular/core/package.json' ) ;
21
24
22
- angularPkgJson = require ( angularPackagePath ) ;
25
+ angularPkgJson = projectRequire ( angularPackagePath ) ;
23
26
} catch {
24
- console . error ( tags . stripIndents `
25
- You seem to not be depending on "@angular/core". This is an error.
26
- ` ) ;
27
+ console . error ( 'You seem to not be depending on "@angular/core". This is an error.' ) ;
27
28
28
29
process . exit ( 2 ) ;
29
30
}
30
31
31
32
if ( ! ( angularPkgJson && angularPkgJson [ 'version' ] ) ) {
32
- console . error ( tags . stripIndents `
33
- Cannot determine versions of "@angular/core".
34
- This likely means your local installation is broken. Please reinstall your packages.
35
- ` ) ;
33
+ console . error (
34
+ ' Cannot determine versions of "@angular/core".\n' +
35
+ ' This likely means your local installation is broken. Please reinstall your packages.' ,
36
+ ) ;
36
37
37
38
process . exit ( 2 ) ;
38
39
}
39
40
40
41
try {
41
- const angularCliPkgPath = require . resolve ( '@angular/cli/package.json' , resolveOptions ) ;
42
- angularCliPkgJson = require ( angularCliPkgPath ) ;
42
+ const angularCliPkgPath = projectRequire . resolve ( '@angular/cli/package.json' ) ;
43
+ angularCliPkgJson = projectRequire ( angularCliPkgPath ) ;
43
44
if ( ! ( angularCliPkgJson && angularCliPkgJson [ 'version' ] ) ) {
44
45
return ;
45
46
}
@@ -55,19 +56,16 @@ export function assertCompatibleAngularVersion(projectRoot: string): void | neve
55
56
return ;
56
57
}
57
58
58
- const supportedAngularSemver =
59
- require ( '../../package.json' ) [ 'peerDependencies' ] [ '@angular/compiler-cli' ] ;
59
+ const supportedAngularSemver = projectRequire ( '@angular-devkit/build-angular/package.json' ) [
60
+ 'peerDependencies'
61
+ ] [ '@angular/compiler-cli' ] ;
60
62
const angularVersion = new SemVer ( angularPkgJson [ 'version' ] ) ;
61
63
62
64
if ( ! satisfies ( angularVersion , supportedAngularSemver , { includePrerelease : true } ) ) {
63
65
console . error (
64
- tags . stripIndents `
65
- This version of CLI is only compatible with Angular versions ${ supportedAngularSemver } ,
66
- but Angular version ${ angularVersion } was found instead.
67
-
68
- Please visit the link below to find instructions on how to update Angular.
69
- https://update.angular.io/
70
- ` + '\n' ,
66
+ `This version of CLI is only compatible with Angular versions ${ supportedAngularSemver } ,\n` +
67
+ `but Angular version ${ angularVersion } was found instead.\n` +
68
+ 'Please visit the link below to find instructions on how to update Angular.\nhttps://update.angular.io/' ,
71
69
) ;
72
70
73
71
process . exit ( 3 ) ;
0 commit comments