Skip to content

Commit 077262f

Browse files
authored
chore: automatically age out old TypeScript versions from testing (#31960)
In our integ tests, we were testing TypeScript versions from 3.9 up to the latest version, which recently broke because of a change to modernize the init templates. We should up this range to a recent version; the common support lifetime of a TypeScript version is the one on `DefinitelyTyped`, the types registry for TypeScript packages. They only target TS versions less than 2 years old, which at the time of this PR is 4.9 and higher. Encode that policy into code automatically, so that we don't have to manually keep this minimum version up-to-date. This currently ages out the following versions: ``` ✕ typescript 3.9 init app ✕ typescript 4.0 init app ✕ typescript 4.1 init app ✕ typescript 4.2 init app ✕ typescript 4.3 init app ✕ typescript 4.4 init app ✕ typescript 4.5 init app ✕ typescript 4.6 init app ✕ typescript 4.7 init app ✕ typescript 4.8 init app ------------------------------8< cut line ----- ✕ typescript 4.9 init app ✓ typescript 5.0 init app ✓ typescript 5.1 init app ✓ typescript 5.2 init app ✓ typescript 5.3 init app ✓ typescript 5.4 init app ✓ typescript 5.5 init app ✓ typescript 5.6 init app ``` Unfortunately not enough to save the TypeScript template modernization change entirely... but at least it's an improvement. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 6f106c7 commit 077262f

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

packages/@aws-cdk-testing/cli-integ/lib/npm.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,32 @@ export function typescriptVersionsSync(): string[] {
1010

1111
const versions: string[] = JSON.parse(stdout);
1212
return Array.from(new Set(versions.map(v => v.split('.').slice(0, 2).join('.'))));
13-
}
13+
}
14+
15+
/**
16+
* Use NPM preinstalled on the machine to query publish times of versions
17+
*/
18+
export function typescriptVersionsYoungerThanDaysSync(days: number, versions: string[]): string[] {
19+
const { stdout } = spawnSync('npm', ['--silent', 'view', 'typescript', 'time', '--json'], { encoding: 'utf-8' });
20+
const versionTsMap: Record<string, string> = JSON.parse(stdout);
21+
22+
const cutoffDate = new Date(Date.now() - (days * 24 * 3600 * 1000));
23+
const cutoffDateS = cutoffDate.toISOString();
24+
25+
const recentVersions = Object.entries(versionTsMap)
26+
.filter(([_, dateS]) => dateS > cutoffDateS)
27+
.map(([v]) => v);
28+
29+
// Input versions are of the form 3.9, 5.2, etc.
30+
// Actual versions are of the form `3.9.15`, `5.3.0-dev.20511311`.
31+
// Return only 2-digit versions for which there is a non-prerelease version in the set of recentVersions
32+
// So a 2-digit versions that is followed by `.<digits>` until the end of the string.
33+
return versions.filter((twoV) => {
34+
const re = new RegExp(`^${reQuote(twoV)}\\.\\d+$`);
35+
return recentVersions.some(fullV => fullV.match(re));
36+
});
37+
}
38+
39+
function reQuote(str: string): string {
40+
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
41+
}

packages/@aws-cdk-testing/cli-integ/tests/init-typescript-app/init-typescript-app.integtest.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { promises as fs } from 'fs';
22
import * as path from 'path';
33
import { integTest, withTemporaryDirectory, ShellHelper, withPackages, TemporaryDirectoryContext } from '../../lib';
4-
import { typescriptVersionsSync } from '../../lib/npm';
4+
import { typescriptVersionsSync, typescriptVersionsYoungerThanDaysSync } from '../../lib/npm';
55

66
['app', 'sample-app'].forEach(template => {
77
integTest(`typescript init ${template}`, withTemporaryDirectory(withPackages(async (context) => {
@@ -19,10 +19,18 @@ import { typescriptVersionsSync } from '../../lib/npm';
1919
})));
2020
});
2121

22+
// Same as https://github.com/DefinitelyTyped/DefinitelyTyped?tab=readme-ov-file#support-window
23+
const TYPESCRIPT_VERSION_AGE_DAYS = 2 * 365;
24+
25+
const TYPESCRIPT_VERSIONS = typescriptVersionsYoungerThanDaysSync(TYPESCRIPT_VERSION_AGE_DAYS, typescriptVersionsSync());
26+
27+
// eslint-disable-next-line no-console
28+
console.log(TYPESCRIPT_VERSIONS);
29+
2230
/**
2331
* Test our generated code with various versions of TypeScript
2432
*/
25-
typescriptVersionsSync().forEach(tsVersion => {
33+
TYPESCRIPT_VERSIONS.forEach(tsVersion => {
2634
integTest(`typescript ${tsVersion} init app`, withTemporaryDirectory(withPackages(async (context) => {
2735
const shell = ShellHelper.fromContext(context);
2836
await context.packages.makeCliAvailable();

0 commit comments

Comments
 (0)