@@ -10,36 +10,50 @@ 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' ;
1915
2016/**
2117 * Verifies that the `ng-dev` tool is up-to-date in the workspace. The check will compare
2218 * the local version of the tool against the requested version in the workspace lock file.
2319 *
2420 * 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.
21+ * local version of `ng-dev` due to not running `yarn`/`pnpm` after checking out new revisions.
2622 *
2723 * @returns a boolean indicating success or failure.
2824 */
2925export async function verifyNgDevToolIsUpToDate ( workspacePath : string ) : Promise < boolean > {
3026 // The placeholder will be replaced by the `pkg_npm` substitutions.
3127 const localVersion = `0.0.0-{SCM_HEAD_SHA}` ;
3228 const workspacePackageJsonFile = path . join ( workspacePath , workspaceRelativePackageJsonPath ) ;
33- const workspaceDirLockFile = path . join ( workspacePath , workspaceRelativeYarnLockFilePath ) ;
29+ const pnpmLockFile = path . join ( workspacePath , 'pnpm-lock.yaml' ) ;
30+ const yarnLockFile = path . join ( workspacePath , 'yarn.lock' ) ;
3431
32+ // TODO: Clean up this logic when fully dropping Yarn
33+ const isPnpmMigrated = fs . existsSync ( pnpmLockFile ) && ! fs . existsSync ( yarnLockFile ) ;
34+ const expectedVersion = isPnpmMigrated
35+ ? getExpectedVersionFromPnpmLock ( workspacePackageJsonFile , pnpmLockFile )
36+ : getExpectedVersionFromYarnLock ( workspacePackageJsonFile , yarnLockFile ) ;
37+
38+ if ( localVersion !== expectedVersion ) {
39+ Log . error ( ' ✘ Your locally installed version of the `ng-dev` tool is outdated and not' ) ;
40+ Log . error ( ' matching with the version in the `package.json` file.' ) ;
41+ Log . error ( ' Re-install the dependencies to ensure you are using the correct version.' ) ;
42+ return false ;
43+ }
44+
45+ return true ;
46+ }
47+
48+ function getExpectedVersionFromYarnLock ( workspacePackageJsonFile : string , lockFilePath : string ) {
3549 try {
3650 const packageJson = JSON . parse ( fs . readFileSync ( workspacePackageJsonFile , 'utf8' ) ) as any ;
3751 // If we are operating in the actual dev-infra repo, always return `true`.
3852 if ( packageJson . name === ngDevNpmPackageName ) {
3953 return true ;
4054 }
4155
42- const lockFileContent = fs . readFileSync ( workspaceDirLockFile , 'utf8' ) ;
56+ const lockFileContent = fs . readFileSync ( lockFilePath , 'utf8' ) ;
4357
4458 let lockFileObject : Record < string , { version : string } > ;
4559 try {
@@ -57,17 +71,27 @@ export async function verifyNgDevToolIsUpToDate(workspacePath: string): Promise<
5771 packageJson ?. dependencies ?. [ ngDevNpmPackageName ] ??
5872 packageJson ?. devDependencies ?. [ ngDevNpmPackageName ] ??
5973 packageJson ?. optionalDependencies ?. [ ngDevNpmPackageName ] ;
60- const expectedVersion = lockFileObject [ `${ ngDevNpmPackageName } @${ devInfraPkgVersion } ` ] . version ;
74+ return lockFileObject [ `${ ngDevNpmPackageName } @${ devInfraPkgVersion } ` ] . version ;
75+ } catch ( e ) {
76+ Log . debug ( 'Could not find expected ng-dev version from `yarn.lock` file:' , e ) ;
77+ return null ;
78+ }
79+ }
6180
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 ;
81+ function getExpectedVersionFromPnpmLock ( workspacePackageJsonFile : string , lockFilePath : string ) {
82+ try {
83+ const packageJson = JSON . parse ( fs . readFileSync ( workspacePackageJsonFile , 'utf8' ) ) as any ;
84+ // If we are operating in the actual dev-infra repo, always return `true`.
85+ if ( packageJson . name === ngDevNpmPackageName ) {
86+ return true ;
6787 }
68- return true ;
88+
89+ const lockFileContent = fs . readFileSync ( lockFilePath , 'utf8' ) ;
90+ const lockFile = parseYaml ( lockFileContent ) ;
91+ const ngDevVersionSpecifier = lockFile [ 'importers' ] [ '.' ] [ '@angular/ng-dev' ] ;
92+ return lockFile [ 'packages' ] [ `@angular/ng-dev@${ ngDevVersionSpecifier } ` ] . version ;
6993 } catch ( e ) {
70- Log . error ( e ) ;
71- return false ;
94+ Log . debug ( 'Could not find expected ng-dev version from `pnpm-lock.yaml` file:' , e ) ;
95+ return null ;
7296 }
7397}
0 commit comments