CDK: Stay Evergreen ☘️ Check Your Package Versions and Auto-Update

Here's a little TypeScript function in CDK to help you stay up to date with your packages. I'm a fan of Snyk and CDK but it can be applied to any package of course:

function snykUpgradeVersionMessage(installedSnykVersion = devDependencies.snyk) {

    const latestSnykVersion = exec('npm view snyk version', (error: { message: any; }, 
        stdout: any) => {(`${ stdout.trimRight }`);
        const snykLatest = (`[ ${ stdout.trimRight() } ]`);

        // upgrade available message
        const versionUpgradeMessage = console_formatters_1.formatAsBanner(
        [ chalk.green('Newer version of SNYK is available: ' + (`[ ${ stdout.trimRight() } ]`)),
          chalk.green('Upgrade recommended: (npm install -g snyk@latest)') ]);

        // upgrade package message
        const packageUpgradeMessage = console_formatters_1.formatAsBanner(
        [ chalk.green('Your SNYK package will now be upgraded to --> ' + 
        (`[ ${ stdout.trimRight() } ]`)) ]);

        // upgrade the package
        try {
            const packageUpgrade = exec(
            'npm install --save-dev --save-exact --no-fundsnyk@latest',
            (error: { message: any; }, stdout: any) => {(`${ stdout.trimRight }`);

            const packageUpgradeDate = console_formatters_1.formatAsBanner([
            chalk.green(`[ ${ stdout.trimRight().trimLeft() } ]`),
            chalk.green('Package successful updated at: ' + 
            new Date().toLocaleString('en-US', {timeZone: 'Europe/Brussels'})),
            chalk.green('Test your package for compatibility issues or breaking changes!'),
            chalk.green('Revert the changes by running [ npm uninstall ]') ]);

             // no upgrade needed message
             const noVersionUpgradeMessage = console_formatters_1.formatAsBanner(
             [ 
               chalk.green('The latest version of the SNYK package is: ' + `${ snykLatest }`),
               chalk.green('You have the latest SNYK version installed: ' + 
               `[ ${ installedSnykVersion } ]`) ]);

                if (latestSnykVersion >= installedSnykVersion) {
                    return noVersionUpgradeMessage.forEach((e: any) => logging_1.print(e));

                } else {
                    return [ versionUpgradeMessage.forEach((e: any) => logging_1.print(e)),
                             packageUpgradeMessage.forEach((e: any) => logging_1.print(e)),
                             packageUpgrade,
                             packageUpgradeDate.forEach((e: any) => logging_1.print(e)) ];
                }
                });

                } catch (e) {
                  throw new Error(
                  chalk.red('Upgrade failed: 
                  check your credentials and check if your logged in to Artifactory!'));
                }
             });
           }

Stay Safe! ☘️