diff --git a/lib/__tests__/shellable.test.ts b/lib/__tests__/shellable.test.ts index 46d0771d..1402e601 100644 --- a/lib/__tests__/shellable.test.ts +++ b/lib/__tests__/shellable.test.ts @@ -1,7 +1,8 @@ import * as path from 'path'; import * as cdk from 'aws-cdk-lib'; import { Template, Match } from 'aws-cdk-lib/assertions'; -import { Shellable, ShellPlatform } from '../../lib'; +import * as codebuild from 'aws-cdk-lib/aws-codebuild'; +import { Shellable, ShellPlatform, WindowsPlatform } from '../../lib'; // tslint:disable:max-line-length @@ -588,3 +589,21 @@ test('can exclude files from scriptDirectory', () => { }, }); }); + + +test('WindowsPlatform installs node via chocolatey by default', () => { + const platform = new WindowsPlatform(codebuild.WindowsBuildImage.WIN_SERVER_CORE_2019_BASE); + + expect(platform.installCommands()).toEqual([ + 'Import-Module "C:\\ProgramData\\chocolatey\\helpers\\chocolateyProfile.psm1"', + 'C:\\ProgramData\\chocolatey\\bin\\choco.exe upgrade nodejs-lts -y', + ]); +}); + +test('WindowsPlatform can disable chocolatey node upgrade', () => { + const platform = new WindowsPlatform(codebuild.WindowsBuildImage.WIN_SERVER_CORE_2019_BASE, { + upgradeNodeWithChocolatey: false, + }); + + expect(platform.installCommands()).toBeUndefined(); +}); diff --git a/lib/shellable.ts b/lib/shellable.ts index 8cd5235d..24eba195 100644 --- a/lib/shellable.ts +++ b/lib/shellable.ts @@ -540,13 +540,35 @@ export class LinuxPlatform extends ShellPlatform { } } +/** + * Options for WindowsPlatform + */ +export interface WindowsPlatformOptions { + /** + * Whether to upgrade Node.js using Chocolatey during the install phase. + * + * @default true + */ + readonly upgradeNodeWithChocolatey?: boolean; +} + /** * A Windows Platform */ export class WindowsPlatform extends ShellPlatform { public readonly platformType = PlatformType.Windows; + private readonly upgradeNodeWithChocolatey: boolean; + + constructor(buildImage: cbuild.IBuildImage, options: WindowsPlatformOptions = {}) { + super(buildImage); + this.upgradeNodeWithChocolatey = options.upgradeNodeWithChocolatey ?? true; + } public installCommands(): string[] | undefined { + if (!this.upgradeNodeWithChocolatey) { + return undefined; + } + return [ // Update the image's nodejs to the latest LTS release. 'Import-Module "C:\\ProgramData\\chocolatey\\helpers\\chocolateyProfile.psm1"',