forked from aws/aws-cdk-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion.ts
More file actions
130 lines (109 loc) · 4.27 KB
/
version.ts
File metadata and controls
130 lines (109 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* c8 ignore start */
import * as path from 'path';
import * as chalk from 'chalk';
import * as fs from 'fs-extra';
import * as semver from 'semver';
import { ToolkitError } from '../../../@aws-cdk/tmp-toolkit-helpers/src/api';
import { debug, info } from '../logging';
import { cdkCacheDir } from '../util';
import { cliRootDir } from './root-dir';
import { formatAsBanner } from './util/console-formatters';
import { execNpmView } from './util/npm';
const ONE_DAY_IN_SECONDS = 1 * 24 * 60 * 60;
const UPGRADE_DOCUMENTATION_LINKS: Record<number, string> = {
1: 'https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html',
};
export function displayVersion() {
return `${versionNumber()} (build ${commit()})`;
}
export function isDeveloperBuild(): boolean {
return versionNumber() === '0.0.0';
}
export function versionNumber(): string {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require(path.join(cliRootDir(), 'package.json')).version.replace(/\+[0-9a-f]+$/, '');
}
function commit(): string {
// eslint-disable-next-line @typescript-eslint/no-require-imports
return require(path.join(cliRootDir(), 'build-info.json')).commit;
}
export class VersionCheckTTL {
public static timestampFilePath(): string {
// Using the same path from account-cache.ts
return path.join(cdkCacheDir(), 'repo-version-ttl');
}
private readonly file: string;
// File modify times are accurate only to the second
private readonly ttlSecs: number;
constructor(file?: string, ttlSecs?: number) {
this.file = file || VersionCheckTTL.timestampFilePath();
try {
fs.mkdirsSync(path.dirname(this.file));
fs.accessSync(path.dirname(this.file), fs.constants.W_OK);
} catch {
throw new ToolkitError(`Directory (${path.dirname(this.file)}) is not writable.`);
}
this.ttlSecs = ttlSecs || ONE_DAY_IN_SECONDS;
}
public async hasExpired(): Promise<boolean> {
try {
const lastCheckTime = (await fs.stat(this.file)).mtimeMs;
const today = new Date().getTime();
if ((today - lastCheckTime) / 1000 > this.ttlSecs) { // convert ms to sec
return true;
}
return false;
} catch (err: any) {
if (err.code === 'ENOENT') {
return true;
} else {
throw err;
}
}
}
public async update(latestVersion?: string): Promise<void> {
if (!latestVersion) {
latestVersion = '';
}
await fs.writeFile(this.file, latestVersion);
}
}
// Export for unit testing only.
// Don't use directly, use displayVersionMessage() instead.
export async function getVersionMessages(currentVersion: string, cacheFile: VersionCheckTTL): Promise<string[]> {
if (!(await cacheFile.hasExpired())) {
return [];
}
const packageInfo = await execNpmView(currentVersion);
const latestVersion = packageInfo.latestVersion;
await cacheFile.update(JSON.stringify(packageInfo));
// If the latest version is the same as the current version, there is no need to display a message
if (semver.eq(latestVersion, currentVersion)) {
return [];
}
const versionMessage = [
packageInfo.deprecated ? `${chalk.red(packageInfo.deprecated as string)}` : undefined,
`Newer version of CDK is available [${chalk.green(latestVersion as string)}]`,
getMajorVersionUpgradeMessage(currentVersion),
'Upgrade recommended (npm install -g aws-cdk)',
].filter(Boolean) as string[];
return versionMessage;
}
function getMajorVersionUpgradeMessage(currentVersion: string): string | void {
const currentMajorVersion = semver.major(currentVersion);
if (UPGRADE_DOCUMENTATION_LINKS[currentMajorVersion]) {
return `Information about upgrading from version ${currentMajorVersion}.x to version ${currentMajorVersion + 1}.x is available here: ${UPGRADE_DOCUMENTATION_LINKS[currentMajorVersion]}`;
}
}
export async function displayVersionMessage(currentVersion = versionNumber(), versionCheckCache?: VersionCheckTTL): Promise<void> {
if (!process.stdout.isTTY || process.env.CDK_DISABLE_VERSION_CHECK) {
return;
}
try {
const versionMessages = await getVersionMessages(currentVersion, versionCheckCache ?? new VersionCheckTTL());
formatAsBanner(versionMessages).forEach(e => info(e));
} catch (err: any) {
debug(`Could not run version check - ${err.message}`);
}
}
/* c8 ignore stop */