@@ -10,36 +10,53 @@ import * as path from 'path';
1010import * as fs from 'fs' ;
1111import lockfile from '@yarnpkg/lockfile' ;
1212import { parse as parseYaml } from 'yaml' ;
13- import {
14- ngDevNpmPackageName ,
15- workspaceRelativePackageJsonPath ,
16- workspaceRelativeYarnLockFilePath ,
17- } from './constants.js' ;
13+ import { ngDevNpmPackageName , workspaceRelativePackageJsonPath } from './constants.js' ;
1814import { Log } from './logging.js' ;
15+ import { tryGetPackageId } from '@pnpm/dependency-path' ;
1916
2017/**
2118 * Verifies that the `ng-dev` tool is up-to-date in the workspace. The check will compare
2219 * the local version of the tool against the requested version in the workspace lock file.
2320 *
2421 * 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.
2623 *
2724 * @returns a boolean indicating success or failure.
2825 */
2926export async function verifyNgDevToolIsUpToDate ( workspacePath : string ) : Promise < boolean > {
3027 // The placeholder will be replaced by the `pkg_npm` substitutions.
3128 const localVersion = `0.0.0-{SCM_HEAD_SHA}` ;
3229 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' ) ;
3432
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 ) {
3552 try {
3653 const packageJson = JSON . parse ( fs . readFileSync ( workspacePackageJsonFile , 'utf8' ) ) as any ;
3754 // If we are operating in the actual dev-infra repo, always return `true`.
3855 if ( packageJson . name === ngDevNpmPackageName ) {
3956 return true ;
4057 }
4158
42- const lockFileContent = fs . readFileSync ( workspaceDirLockFile , 'utf8' ) ;
59+ const lockFileContent = fs . readFileSync ( lockFilePath , 'utf8' ) ;
4360
4461 let lockFileObject : Record < string , { version : string } > ;
4562 try {
@@ -57,17 +74,33 @@ export async function verifyNgDevToolIsUpToDate(workspacePath: string): Promise<
5774 packageJson ?. dependencies ?. [ ngDevNpmPackageName ] ??
5875 packageJson ?. devDependencies ?. [ ngDevNpmPackageName ] ??
5976 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+ }
6183
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 ;
6790 }
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 ;
69102 } 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 ;
72105 }
73106}
0 commit comments