Skip to content

Commit 7fa1ab5

Browse files
committed
feat(ng-dev): create a check that ng-dev is up to date
Whenever ng-dev is run we now check to ensure that the ng-dev script that is running matches the version found in the pnpm lockfile. Upon failing this check, a non-blocking warning message is logged to the console.
1 parent 4a9ebbb commit 7fa1ab5

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

ng-dev/cli.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {buildPullapproveParser} from './pullapprove/cli.js';
1919
import {buildReleaseParser} from './release/cli.js';
2020
import {tsCircularDependenciesBuilder} from './ts-circular-dependencies/index.js';
2121
import {captureLogOutputForCommand} from './utils/logging.js';
22+
import {ngDevVersionMiddleware} from './utils/version-check.js';
2223
import {buildAuthParser} from './auth/cli.js';
2324
import {buildPerfParser} from './perf/cli.js';
2425
import {buildAiParser} from './ai/cli.js';
@@ -28,7 +29,7 @@ runParserWithCompletedFunctions((yargs: Argv) => {
2829
process.exitCode = 0;
2930
return yargs
3031
.scriptName('ng-dev')
31-
.middleware(captureLogOutputForCommand, true)
32+
.middleware([captureLogOutputForCommand, ngDevVersionMiddleware], true)
3233
.demandCommand()
3334
.recommendCommands()
3435
.command('auth <command>', false, buildAuthParser)

ng-dev/utils/version-check.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ import {parse as parseYaml} from 'yaml';
1313
import {ngDevNpmPackageName, workspaceRelativePackageJsonPath} from './constants.js';
1414
import {Log} from './logging.js';
1515
import {tryGetPackageId} from '@pnpm/dependency-path';
16+
import {determineRepoBaseDirFromCwd} from './repo-directory.js';
17+
18+
/** Whether ngDevVersionMiddleware verification has already occured. */
19+
let verified = false;
20+
export async function ngDevVersionMiddleware() {
21+
// TODO(josephperrott): remove this guard against running multiple times after
22+
// https://github.com/yargs/yargs/issues/2223 is fixed
23+
if (verified) {
24+
return;
25+
}
26+
// TODO(josephperrott): Create an enforcement configuration option.
27+
await verifyNgDevToolIsUpToDate(determineRepoBaseDirFromCwd());
28+
verified = true;
29+
}
1630

1731
/**
1832
* Verifies that the `ng-dev` tool is up-to-date in the workspace. The check will compare
@@ -26,6 +40,10 @@ import {tryGetPackageId} from '@pnpm/dependency-path';
2640
export async function verifyNgDevToolIsUpToDate(workspacePath: string): Promise<boolean> {
2741
// The placeholder will be replaced by the `pkg_npm` substitutions.
2842
const localVersion = `0.0.0-{SCM_HEAD_SHA}`;
43+
if (localVersion === ('0.0.0-{{BUILD_SCM_COMMIT_SHA}}' as string)) {
44+
Log.debug('Skipping ng-dev version check as this is a locally generated version.');
45+
return true;
46+
}
2947
const workspacePackageJsonFile = path.join(workspacePath, workspaceRelativePackageJsonPath);
3048
const pnpmLockFile = path.join(workspacePath, 'pnpm-lock.yaml');
3149
const yarnLockFile = path.join(workspacePath, 'yarn.lock');
@@ -36,12 +54,14 @@ export async function verifyNgDevToolIsUpToDate(workspacePath: string): Promise<
3654
? getExpectedVersionFromPnpmLock(workspacePackageJsonFile, pnpmLockFile)
3755
: getExpectedVersionFromYarnLock(workspacePackageJsonFile, yarnLockFile);
3856

39-
Log.debug(`Expecting the following ng-dev version: ${expectedVersion}`);
57+
Log.debug('Checking ng-dev version in lockfile and in the running script:');
58+
Log.debug(` Local: ${localVersion}`);
59+
Log.debug(` Expected: ${expectedVersion}`);
4060

4161
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.');
62+
Log.warn(' Your locally installed version of the `ng-dev` tool is outdated and not');
63+
Log.warn(' matching with the version in the `package.json` file.');
64+
Log.warn(' Re-install the dependencies to ensure you are using the correct version.');
4565
return false;
4666
}
4767

0 commit comments

Comments
 (0)