-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(toolkit): show when new version is available (#2484)
Check, once a day, if a newer CDK version available in npm and announce it's availability at the end of a significant command. TESTING: * New unit tests for version.ts * Downgraded version number in package.json and verified that the expected message is printed. * Verified that the file cache throttles the check to run only once per day. Closes #297
- v2.173.2
- v2.173.1
- v2.173.0
- v2.172.0
- v2.171.1
- v2.171.0
- v2.170.0
- v2.169.0
- v2.168.0
- v2.167.2
- v2.167.1
- v2.167.0
- v2.166.0
- v2.165.0
- v2.164.1
- v2.164.0
- v2.163.1
- v2.163.0
- v2.162.1
- v2.162.0
- v2.161.1
- v2.161.0
- v2.160.0
- v2.159.1
- v2.159.0
- v2.158.0
- v2.157.0
- v2.156.0
- v2.155.0
- v2.154.1
- v2.154.0
- v2.153.0
- v2.152.0
- v2.151.1
- v2.151.0
- v2.150.0
- v2.149.0
- v2.148.1
- v2.148.0
- v2.147.3
- v2.147.2
- v2.147.1
- v2.147.0
- v2.146.0
- v2.145.0
- v2.144.0
- v2.143.1
- v2.143.0
- v2.142.1
- v2.142.0
- v2.141.0
- v2.140.0
- v2.139.1
- v2.139.0
- v2.138.0
- v2.137.0
- v2.136.1
- v2.136.0
- v2.135.0
- v2.134.0
- v2.133.0
- v2.132.1
- v2.132.0
- v2.131.0
- v2.130.0
- v2.129.0
- v2.128.0
- v2.127.0
- v2.126.0
- v2.125.0
- v2.124.0
- v2.123.0
- v2.122.0
- v2.121.1
- v2.121.0
- v2.120.0
- v2.119.0
- v2.118.0
- v2.117.0
- v2.116.1
- v2.116.0
- v2.115.0
- v2.114.1
- v2.114.0
- v2.113.0
- v2.112.0
- v2.111.0
- v2.110.1
- v2.110.0
- v2.109.0
- v2.108.1
- v2.108.0
- v2.107.0
- v2.106.1
- v2.106.0
- v2.105.0
- v2.104.0
- v2.103.1
- v2.103.0
- v2.102.1
- v2.102.0
- v2.101.1
- v2.101.0
- v2.100.0
- v2.99.1
- v2.99.0
- v2.98.0
- v2.97.1
- v2.97.0
- v2.96.2
- v2.96.1
- v2.96.0
- v2.95.1
- v2.95.0
- v2.94.0
- v2.93.0
- v2.92.0
- v2.91.0
- v2.90.0
- v2.89.0
- v2.88.0
- v2.87.0
- v2.86.0
- v2.85.0
- v2.84.0
- v2.83.1
- v2.83.0
- v2.82.0
- v2.81.0
- v2.80.0
- v2.79.1
- v2.79.0
- v2.78.0
- v2.77.0
- v2.76.0
- v2.75.1
- v2.75.0
- v2.74.0
- v2.73.0
- v2.72.1
- v2.72.0
- v2.71.0
- v2.70.0
- v2.69.0
- v2.68.0
- v2.67.0
- v2.66.1
- v2.66.0
- v2.65.0
- v2.64.0
- v2.63.2
- v2.63.1
- v2.63.0
- v2.62.2
- v2.62.1
- v2.62.0
- v2.61.1
- v2.61.0
- v2.60.0
- v2.59.0
- v2.58.1
- v2.58.0
- v2.57.0
- v2.56.1
- v2.56.0
- v2.55.1
- v2.55.0
- v2.54.0
- v2.53.0
- v2.52.1
- v2.52.0
- v2.51.1
- v2.51.0
- v2.50.0
- v2.49.1
- v2.49.0
- v2.48.0
- v2.47.0
- v2.46.0
- v2.45.0
- v2.44.0
- v2.43.1
- v2.43.0
- v2.42.1
- v2.42.0
- v2.41.0
- v2.40.0
- v2.39.1
- v2.39.0
- v2.38.1
- v2.38.0
- v2.37.1
- v2.37.0
- v2.36.0
- v2.35.0
- v2.34.2
- v2.34.1
- v2.34.0
- v2.33.0
- v2.32.1
- v2.32.0
- v2.31.2
- v2.31.1
- v2.31.0
- v2.30.0
- v2.29.1
- v2.29.0
- v2.28.1
- v2.28.0
- v2.27.0
- v2.26.0
- v2.25.0
- v2.24.1
- v2.24.0
- v2.23.0
- v2.22.0
- v2.21.1
- v2.21.0
- v2.20.0
- v2.19.0
- v2.18.0
- v2.17.0
- v2.16.0
- v2.15.0
- v2.14.0
- v2.13.0
- v2.12.0
- v2.11.0
- v2.10.0
- v2.9.0
- v2.8.0
- v2.7.0
- v2.6.0
- v2.5.0
- v2.4.0
- v2.3.0
- v2.2.0
- v2.1.0
- v2.0.0
- v2.0.0-rc.33
- v2.0.0-rc.32
- v2.0.0-rc.31
- v2.0.0-rc.30
- v2.0.0-rc.29
- v2.0.0-rc.28
- v2.0.0-rc.27
- v2.0.0-rc.26
- v2.0.0-rc.25
- v2.0.0-rc.24
- v2.0.0-rc.23
- v2.0.0-rc.22
- v2.0.0-rc.21
- v2.0.0-rc.20
- v2.0.0-rc.19
- v2.0.0-rc.18
- v2.0.0-rc.17
- v2.0.0-rc.16
- v2.0.0-rc.15
- v2.0.0-rc.14
- v2.0.0-rc.13
- v2.0.0-rc.12
- v2.0.0-rc.11
- v2.0.0-rc.10
- v2.0.0-rc.9
- v2.0.0-rc.8
- v2.0.0-rc.7
- v2.0.0-rc.6
- v2.0.0-rc.5
- v2.0.0-rc.4
- v2.0.0-rc.3
- v2.0.0-rc.1
- v2.0.0-alpha.14
- v2.0.0-alpha.13
- v2.0.0-alpha.12
- v2.0.0-alpha.11
- v2.0.0-alpha.10
- v2.0.0-alpha.6
- v2.0.0-alpha.5
- v2.0.0-alpha.4
- v2.0.0-alpha.3
- v2.0.0-alpha.2
- v2.0.0-alpha.1
- v2.0.0-alpha.0
- v1.204.0
- v1.203.0
- v1.202.0
- v1.201.0
- v1.200.0
- v1.199.0
- v1.198.1
- v1.198.0
- v1.197.0
- v1.196.0
- v1.195.0
- v1.194.0
- v1.193.0
- v1.192.0
- v1.191.0
- v1.190.0
- v1.189.0
- v1.188.0
- v1.187.0
- v1.186.1
- v1.186.0
- v1.185.0
- v1.184.1
- v1.184.0
- v1.183.0
- v1.182.0
- v1.181.1
- v1.181.0
- v1.180.0
- v1.179.0
- v1.178.0
- v1.177.0
- v1.176.0
- v1.175.0
- v1.174.0
- v1.173.0
- v1.172.0
- v1.171.0
- v1.170.1
- v1.170.0
- v1.169.0
- v1.168.0
- v1.167.0
- v1.166.1
- v1.165.0
- v1.164.0
- v1.163.2
- v1.163.1
- v1.163.0
- v1.162.0
- v1.161.0
- v1.160.0
- v1.159.0
- v1.158.0
- v1.157.0
- v1.156.1
- v1.156.0
- v1.155.0
- v1.154.0
- v1.153.1
- v1.153.0
- v1.152.0
- v1.151.0
- v1.150.0
- v1.149.0
- v1.148.0
- v1.147.0
- v1.146.0
- v1.145.0
- v1.144.0
- v1.143.0
- v1.142.0
- v1.141.0
- v1.140.0
- v1.139.0
- v1.138.2
- v1.138.1
- v1.138.0
- v1.137.0
- v1.136.0
- v1.135.0
- v1.134.0
- v1.133.0
- v1.132.0
- v1.131.0
- v1.130.0
- v1.129.0
- v1.128.0
- v1.127.0
- v1.126.0
- v1.125.0
- v1.124.0
- v1.123.0
- v1.122.0
- v1.121.0
- v1.120.0
- v1.119.0
- v1.118.0
- v1.117.0
- v1.116.0
- v1.115.0
- v1.114.0
- v1.113.0
- v1.112.0
- v1.111.0
- v1.110.1
- v1.110.0
- v1.109.0
- v1.108.1
- v1.108.0
- v1.107.0
- v1.106.1
- v1.106.0
- v1.105.0
- v1.104.0
- v1.103.0
- v1.102.0
- v1.101.0
- v1.100.0
- v1.99.0
- v1.98.0
- v1.97.0
- v1.96.0
- v1.95.2
- v1.95.1
- v1.95.0
- v1.94.1
- v1.94.0
- v1.93.0
- v1.92.0
- v1.91.0
- v1.90.1
- v1.90.0
- v1.89.0
- v1.88.0
- v1.87.1
- v1.87.0
- v1.86.0
- v1.85.0
- v1.84.0
- v1.83.0
- v1.82.0
- v1.81.0
- v1.80.0
- v1.79.0
- v1.78.0
- v1.77.0
- v1.76.0
- v1.75.0
- v1.74.0
- v1.73.0
- v1.72.0
- v1.71.0
- v1.70.0
- v1.69.0
- v1.68.0
- v1.67.0
- v1.66.0
- v1.65.0
- v1.64.1
- v1.64.0
- v1.63.0
- v1.62.0
- v1.61.1
- v1.61.0
- v1.60.0
- v1.59.0
- v1.58.0
- v1.57.0
- v1.56.0
- v1.55.0
- v1.54.0
- v1.53.0
- v1.52.0
- v1.51.0
- v1.50.0
- v1.49.1
- v1.49.0
- v1.48.0
- v1.47.1
- v1.47.0
- v1.46.0
- v1.45.0
- v1.44.0
- v1.43.0
- v1.42.1
- v1.42.0
- v1.41.0
- v1.40.0
- v1.39.0
- v1.38.0
- v1.37.0
- v1.36.1
- v1.36.0
- v1.35.0
- v1.34.1
- v1.34.0
- v1.33.1
- v1.33.0
- v1.32.2
- v1.32.1
- v1.32.0
- v1.31.0
- v1.30.0
- v1.29.0
- v1.28.0
- v1.27.0
- v1.26.0
- v1.25.0
- v1.24.0
- v1.23.0
- v1.22.0
- v1.21.1
- v1.21.0
- v1.20.0
- v1.19.0
- v1.18.0
- v1.17.1
- v1.17.0
- v1.16.3
- v1.16.2
- v1.16.1
- v1.16.0
- v1.15.0
- v1.14.0
- v1.13.1
- v1.13.0
- v1.12.0
- v1.11.0
- v1.10.1
- v1.10.0
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.1
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- v0.39.0
- v0.38.0
- v0.37.0
- v0.36.2
- v0.36.1
- v0.36.0
- v0.35.0
- v0.34.0
- v0.33.0
- v0.32.0
Showing
9 changed files
with
288 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import colors = require('colors/safe'); | ||
|
||
/** | ||
* Returns a set of strings when printed on the console produces a banner msg. The message is in the following format - | ||
* ******************** | ||
* *** msg line x *** | ||
* *** msg line xyz *** | ||
* ******************** | ||
* | ||
* Spec: | ||
* - The width of every line is equal, dictated by the longest message string | ||
* - The first and last lines are '*'s for the full length of the line | ||
* - Each line in between is prepended with '*** ' and appended with ' ***' | ||
* - The text is indented left, i.e. whitespace is right-padded when the length is shorter than the longest. | ||
* | ||
* @param msgs array of strings containing the message lines to be printed in the banner. Returns empty string if array | ||
* is empty. | ||
* @returns array of strings containing the message formatted as a banner | ||
*/ | ||
export function formatAsBanner(msgs: string[]): string[] { | ||
const printLen = (str: string) => colors.strip(str).length; | ||
|
||
if (msgs.length === 0) { | ||
return []; | ||
} | ||
|
||
const leftPad = '*** '; | ||
const rightPad = ' ***'; | ||
const bannerWidth = printLen(leftPad) + printLen(rightPad) + | ||
msgs.reduce((acc, msg) => Math.max(acc, printLen(msg)), 0); | ||
|
||
const bannerLines: string[] = []; | ||
bannerLines.push('*'.repeat(bannerWidth)); | ||
|
||
// Improvement: If any 'msg' is wider than the terminal width, wrap message across lines. | ||
msgs.forEach((msg) => { | ||
const padding = ' '.repeat(bannerWidth - (printLen(msg) + printLen(leftPad) + printLen(rightPad))); | ||
bannerLines.push(''.concat(leftPad, msg, padding, rightPad)); | ||
}); | ||
|
||
bannerLines.push('*'.repeat(bannerWidth)); | ||
return bannerLines; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { exec as _exec } from 'child_process'; | ||
import colors = require('colors/safe'); | ||
import { close as _close, open as _open, stat as _stat } from 'fs'; | ||
import semver = require('semver'); | ||
import { promisify } from 'util'; | ||
import { debug, print, warning } from '../lib/logging'; | ||
import { formatAsBanner } from '../lib/util/console-formatters'; | ||
|
||
const ONE_DAY_IN_SECONDS = 1 * 24 * 60 * 60; | ||
|
||
const close = promisify(_close); | ||
const exec = promisify(_exec); | ||
const open = promisify(_open); | ||
const stat = promisify(_stat); | ||
|
||
export const DISPLAY_VERSION = `${versionNumber()} (build ${commit()})`; | ||
|
||
function versionNumber(): string { | ||
return require('../package.json').version.replace(/\+[0-9a-f]+$/, ''); | ||
} | ||
|
||
function commit(): string { | ||
return require('../build-info.json').commit; | ||
} | ||
|
||
export class TimestampFile { | ||
private readonly file: string; | ||
|
||
// File modify times are accurate only till the second, hence using seconds as precision | ||
private readonly ttlSecs: number; | ||
|
||
constructor(file: string, ttlSecs: number) { | ||
this.file = file; | ||
this.ttlSecs = ttlSecs; | ||
} | ||
|
||
public async hasExpired(): Promise<boolean> { | ||
try { | ||
const lastCheckTime = (await stat(this.file)).mtimeMs; | ||
const today = new Date().getTime(); | ||
|
||
if ((today - lastCheckTime) / 1000 > this.ttlSecs) { // convert ms to secs | ||
return true; | ||
} | ||
return false; | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
return true; | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
public async update(): Promise<void> { | ||
const fd = await open(this.file, 'w'); | ||
await close(fd); | ||
} | ||
} | ||
|
||
// Export for unit testing only. | ||
// Don't use directly, use displayVersionMessage() instead. | ||
export async function latestVersionIfHigher(currentVersion: string, cacheFile: TimestampFile): Promise<string | null> { | ||
if (!(await cacheFile.hasExpired())) { | ||
return null; | ||
} | ||
|
||
const { stdout, stderr } = await exec(`npm view aws-cdk version`); | ||
if (stderr && stderr.trim().length > 0) { | ||
debug(`The 'npm view' command generated an error stream with content [${stderr.trim()}]`); | ||
} | ||
const latestVersion = stdout.trim(); | ||
if (!semver.valid(latestVersion)) { | ||
throw new Error(`npm returned an invalid semver ${latestVersion}`); | ||
} | ||
const isNewer = semver.gt(latestVersion, currentVersion); | ||
await cacheFile.update(); | ||
|
||
if (isNewer) { | ||
return latestVersion; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
const versionCheckCache = new TimestampFile(`${__dirname}/../.LAST_VERSION_CHECK`, ONE_DAY_IN_SECONDS); | ||
|
||
export async function displayVersionMessage(): Promise<void> { | ||
if (!process.stdout.isTTY) { | ||
return; | ||
} | ||
|
||
try { | ||
const laterVersion = await latestVersionIfHigher(versionNumber(), versionCheckCache); | ||
if (laterVersion) { | ||
const bannerMsg = formatAsBanner([ | ||
`Newer version of CDK is available [${colors.green(laterVersion as string)}]`, | ||
`Upgrade recommended`, | ||
]); | ||
bannerMsg.forEach((e) => print(e)); | ||
} | ||
} catch (err) { | ||
warning(`Could not run version check due to error ${err.message}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Test } from 'nodeunit'; | ||
import { setTimeout as _setTimeout } from 'timers'; | ||
import { promisify } from 'util'; | ||
import { latestVersionIfHigher, TimestampFile } from '../lib/version'; | ||
|
||
const setTimeout = promisify(_setTimeout); | ||
|
||
function tmpfile(): string { | ||
return `/tmp/version-${Math.floor(Math.random() * 10000)}`; | ||
} | ||
|
||
export = { | ||
async 'cache file responds correctly when file is not present'(test: Test) { | ||
const cache = new TimestampFile(tmpfile(), 1); | ||
test.strictEqual(await cache.hasExpired(), true); | ||
test.done(); | ||
}, | ||
|
||
async 'cache file honours the specified TTL'(test: Test) { | ||
const cache = new TimestampFile(tmpfile(), 1); | ||
await cache.update(); | ||
test.strictEqual(await cache.hasExpired(), false); | ||
await setTimeout(1000); // 1 sec in ms | ||
test.strictEqual(await cache.hasExpired(), true); | ||
test.done(); | ||
}, | ||
|
||
async 'Skip version check if cache has not expired'(test: Test) { | ||
const cache = new TimestampFile(tmpfile(), 100); | ||
await cache.update(); | ||
test.equal(await latestVersionIfHigher('0.0.0', cache), null); | ||
test.done(); | ||
}, | ||
|
||
async 'Return later version when exists & skip recent re-check'(test: Test) { | ||
const cache = new TimestampFile(tmpfile(), 100); | ||
const result = await latestVersionIfHigher('0.0.0', cache); | ||
test.notEqual(result, null); | ||
test.ok((result as string).length > 0); | ||
|
||
const result2 = await latestVersionIfHigher('0.0.0', cache); | ||
test.equal(result2, null); | ||
test.done(); | ||
}, | ||
|
||
async 'Return null if version is higher than npm'(test: Test) { | ||
const cache = new TimestampFile(tmpfile(), 100); | ||
const result = await latestVersionIfHigher('100.100.100', cache); | ||
test.equal(result, null); | ||
test.done(); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import colors = require('colors/safe'); | ||
import { Test } from 'nodeunit'; | ||
import { formatAsBanner } from '../../lib/util/console-formatters'; | ||
|
||
function reportBanners(actual: string[], expected: string[]): string { | ||
return 'Assertion failed.\n' + | ||
'Expected banner: \n' + expected.join('\n') + '\n' + | ||
'Actual banner: \n' + actual.join('\n'); | ||
} | ||
|
||
export = { | ||
'no banner on empty msg list'(test: Test) { | ||
test.strictEqual(formatAsBanner([]).length, 0); | ||
test.done(); | ||
}, | ||
|
||
'banner works as expected'(test: Test) { | ||
const msgs = [ 'msg1', 'msg2' ]; | ||
const expected = [ | ||
'************', | ||
'*** msg1 ***', | ||
'*** msg2 ***', | ||
'************' | ||
]; | ||
|
||
const actual = formatAsBanner(msgs); | ||
|
||
test.strictEqual(formatAsBanner(msgs).length, expected.length, reportBanners(actual, expected)); | ||
for (let i = 0; i < expected.length; i++) { | ||
test.strictEqual(actual[i], expected[i], reportBanners(actual, expected)); | ||
} | ||
test.done(); | ||
}, | ||
|
||
'banner works for formatted msgs'(test: Test) { | ||
const msgs = [ | ||
'hello msg1', | ||
colors.yellow('hello msg2'), | ||
colors.bold('hello msg3'), | ||
]; | ||
const expected = [ | ||
'******************', | ||
'*** hello msg1 ***', | ||
`*** ${colors.yellow('hello msg2')} ***`, | ||
`*** ${colors.bold('hello msg3')} ***`, | ||
'******************', | ||
]; | ||
|
||
const actual = formatAsBanner(msgs); | ||
|
||
test.strictEqual(formatAsBanner(msgs).length, expected.length, reportBanners(actual, expected)); | ||
for (let i = 0; i < expected.length; i++) { | ||
test.strictEqual(actual[i], expected[i], reportBanners(actual, expected)); | ||
} | ||
|
||
test.done(); | ||
} | ||
}; |