@@ -10,36 +10,53 @@ import * as path from 'path';
10
10
import * as fs from 'fs' ;
11
11
import lockfile from '@yarnpkg/lockfile' ;
12
12
import { parse as parseYaml } from 'yaml' ;
13
- import {
14
- ngDevNpmPackageName ,
15
- workspaceRelativePackageJsonPath ,
16
- workspaceRelativeYarnLockFilePath ,
17
- } from './constants.js' ;
13
+ import { ngDevNpmPackageName , workspaceRelativePackageJsonPath } from './constants.js' ;
18
14
import { Log } from './logging.js' ;
15
+ import { tryGetPackageId } from '@pnpm/dependency-path' ;
19
16
20
17
/**
21
18
* Verifies that the `ng-dev` tool is up-to-date in the workspace. The check will compare
22
19
* the local version of the tool against the requested version in the workspace lock file.
23
20
*
24
21
* This check is helpful ensuring that the caretaker does not accidentally run with an older
25
- * local version of `ng-dev` due to not running `yarn` after checking out new revisions.
22
+ * local version of `ng-dev` due to not running `yarn`/`pnpm` after checking out new revisions.
26
23
*
27
24
* @returns a boolean indicating success or failure.
28
25
*/
29
26
export async function verifyNgDevToolIsUpToDate ( workspacePath : string ) : Promise < boolean > {
30
27
// The placeholder will be replaced by the `pkg_npm` substitutions.
31
28
const localVersion = `0.0.0-{SCM_HEAD_SHA}` ;
32
29
const workspacePackageJsonFile = path . join ( workspacePath , workspaceRelativePackageJsonPath ) ;
33
- const workspaceDirLockFile = path . join ( workspacePath , workspaceRelativeYarnLockFilePath ) ;
30
+ const pnpmLockFile = path . join ( workspacePath , 'pnpm-lock.yaml' ) ;
31
+ const yarnLockFile = path . join ( workspacePath , 'yarn.lock' ) ;
34
32
33
+ // TODO: Clean up this logic when fully dropping Yarn
34
+ const isPnpmMigrated = fs . existsSync ( pnpmLockFile ) && ! fs . existsSync ( yarnLockFile ) ;
35
+ const expectedVersion = isPnpmMigrated
36
+ ? getExpectedVersionFromPnpmLock ( workspacePackageJsonFile , pnpmLockFile )
37
+ : getExpectedVersionFromYarnLock ( workspacePackageJsonFile , yarnLockFile ) ;
38
+
39
+ Log . debug ( `Expecting the following ng-dev version: ${ expectedVersion } ` ) ;
40
+
41
+ if ( localVersion !== expectedVersion ) {
42
+ Log . error ( ' ✘ Your locally installed version of the `ng-dev` tool is outdated and not' ) ;
43
+ Log . error ( ' matching with the version in the `package.json` file.' ) ;
44
+ Log . error ( ' Re-install the dependencies to ensure you are using the correct version.' ) ;
45
+ return false ;
46
+ }
47
+
48
+ return true ;
49
+ }
50
+
51
+ function getExpectedVersionFromYarnLock ( workspacePackageJsonFile : string , lockFilePath : string ) {
35
52
try {
36
53
const packageJson = JSON . parse ( fs . readFileSync ( workspacePackageJsonFile , 'utf8' ) ) as any ;
37
54
// If we are operating in the actual dev-infra repo, always return `true`.
38
55
if ( packageJson . name === ngDevNpmPackageName ) {
39
56
return true ;
40
57
}
41
58
42
- const lockFileContent = fs . readFileSync ( workspaceDirLockFile , 'utf8' ) ;
59
+ const lockFileContent = fs . readFileSync ( lockFilePath , 'utf8' ) ;
43
60
44
61
let lockFileObject : Record < string , { version : string } > ;
45
62
try {
@@ -57,17 +74,33 @@ export async function verifyNgDevToolIsUpToDate(workspacePath: string): Promise<
57
74
packageJson ?. dependencies ?. [ ngDevNpmPackageName ] ??
58
75
packageJson ?. devDependencies ?. [ ngDevNpmPackageName ] ??
59
76
packageJson ?. optionalDependencies ?. [ ngDevNpmPackageName ] ;
60
- const expectedVersion = lockFileObject [ `${ ngDevNpmPackageName } @${ devInfraPkgVersion } ` ] . version ;
77
+ return lockFileObject [ `${ ngDevNpmPackageName } @${ devInfraPkgVersion } ` ] . version ;
78
+ } catch ( e ) {
79
+ Log . debug ( 'Could not find expected ng-dev version from `yarn.lock` file:' , e ) ;
80
+ return null ;
81
+ }
82
+ }
61
83
62
- if ( localVersion !== expectedVersion ) {
63
- Log . error ( ' ✘ Your locally installed version of the `ng-dev` tool is outdated and not' ) ;
64
- Log . error ( ' matching with the version in the `package.json` file.' ) ;
65
- Log . error ( ' Re-install the dependencies to ensure you are using the correct version.' ) ;
66
- return false ;
84
+ function getExpectedVersionFromPnpmLock ( workspacePackageJsonFile : string , lockFilePath : string ) {
85
+ try {
86
+ const packageJson = JSON . parse ( fs . readFileSync ( workspacePackageJsonFile , 'utf8' ) ) as any ;
87
+ // If we are operating in the actual dev-infra repo, always return `true`.
88
+ if ( packageJson . name === ngDevNpmPackageName ) {
89
+ return true ;
67
90
}
68
- return true ;
91
+
92
+ const lockFileContent = fs . readFileSync ( lockFilePath , 'utf8' ) ;
93
+ const lockFile = parseYaml ( lockFileContent ) ;
94
+ const importers = lockFile [ 'importers' ] [ '.' ] ;
95
+ const depEntry =
96
+ importers . dependencies ?. [ '@angular/ng-dev' ] ??
97
+ importers . devDependencies ?. [ '@angular/ng-dev' ] ??
98
+ importers . optionalDependencies ?. [ '@angular/ng-dev' ] ;
99
+ const packageId = tryGetPackageId ( depEntry . version ) ;
100
+
101
+ return lockFile [ 'packages' ] [ `@angular/ng-dev@${ packageId } ` ] . version ;
69
102
} catch ( e ) {
70
- Log . error ( e ) ;
71
- return false ;
103
+ Log . debug ( 'Could not find expected ng-dev version from `pnpm-lock.yaml` file:' , e ) ;
104
+ return null ;
72
105
}
73
106
}
0 commit comments