@@ -2,12 +2,14 @@ import * as fs from 'fs'
22import * as path from 'path'
33import * as mime from 'mime-types'
44
5+ import * as cloudfront from '@aws-sdk/client-cloudfront'
56import * as aws from '@pulumi/aws'
67import * as pulumi from '@pulumi/pulumi'
7- import { local } from '@pulumi/command'
88
99import { NameRegister } from '../utils.js'
1010
11+ const pulumiConfig = new pulumi . Config ( 'aws' )
12+
1113const nameRegister = NameRegister . getInstance ( )
1214let registerName = ( name : string ) : string => {
1315 return nameRegister . registerName ( name )
@@ -401,85 +403,40 @@ export function getDomainAndSubdomain(domain: string): {
401403 }
402404}
403405
404- export function buildInvalidator (
405- distribution : aws . cloudfront . Distribution ,
406- staticPath : string ,
407- prerenderedPath : string ,
408- ) {
409- interface PathHashResourceInputs {
410- path : pulumi . Input < string >
411- }
412-
413- interface PathHashInputs {
414- path : string
415- }
416-
417- interface PathHashOutputs {
418- hash : string
406+ // Source: https://www.pulumi.com/blog/next-level-iac-pulumi-runtime-logic/
407+ export function createInvalidation ( id : string ) {
408+ // Only invalidate after a deployment.
409+ if ( pulumi . runtime . isDryRun ( ) ) {
410+ console . log ( 'This is a Pulumi preview, so skipping cache invalidation.' )
411+ return
419412 }
420413
421- const pathHashProvider : pulumi . dynamic . ResourceProvider = {
422- async create ( inputs : PathHashInputs ) {
423- const folderHash = await import ( 'folder-hash' )
424- const pathHash = await folderHash . hashElement ( inputs . path )
425- return { id : inputs . path , outs : { hash : pathHash . toString ( ) } }
426- } ,
427- async diff (
428- id : string ,
429- previousOutput : PathHashOutputs ,
430- news : PathHashInputs ,
431- ) : Promise < pulumi . dynamic . DiffResult > {
432- const replaces : string [ ] = [ ]
433- let changes = true
434-
435- const oldHash = previousOutput . hash
436- const folderHash = await import ( 'folder-hash' )
437- const newHash = await folderHash . hashElement ( news . path )
438-
439- if ( oldHash === newHash . toString ( ) ) {
440- changes = false
441- }
442-
443- return {
444- deleteBeforeReplace : false ,
445- replaces : replaces ,
446- changes : changes ,
447- }
448- } ,
449- async update ( id , olds : PathHashInputs , news : PathHashInputs ) {
450- const folderHash = await import ( 'folder-hash' )
451- const pathHash = await folderHash . hashElement ( news . path )
452- return { outs : { hash : pathHash . toString ( ) } }
453- } ,
454- }
455-
456- class PathHash extends pulumi . dynamic . Resource {
457- public readonly hash ! : pulumi . Output < string >
458- constructor (
459- name : string ,
460- args : PathHashResourceInputs ,
461- opts ?: pulumi . CustomResourceOptions ,
462- ) {
463- super ( pathHashProvider , name , { hash : undefined , ...args } , opts )
464- }
465- }
466-
467- let staticHash = new PathHash ( registerName ( 'StaticHash' ) , {
468- path : staticPath ,
469- } )
414+ const region = pulumiConfig . require ( 'region' )
415+
416+ process . on ( 'beforeExit' , ( ) => {
417+ const client = new cloudfront . CloudFrontClient ( { region } )
418+ const command = new cloudfront . CreateInvalidationCommand ( {
419+ DistributionId : id ,
420+ InvalidationBatch : {
421+ CallerReference : `invalidation-${ Date . now ( ) } ` ,
422+ Paths : {
423+ Quantity : 1 ,
424+ Items : [ '/*' ] ,
425+ } ,
426+ } ,
427+ } )
470428
471- let prerenderedHash = new PathHash ( registerName ( 'PrerenderedHash' ) , {
472- path : prerenderedPath ! ,
429+ client
430+ . send ( command )
431+ . then ( ( result ) => {
432+ console . log (
433+ `Invalidation status for ${ id } : ${ result . Invalidation ?. Status } .` ,
434+ )
435+ process . exit ( 0 )
436+ } )
437+ . catch ( ( error ) => {
438+ console . error ( error )
439+ process . exit ( 1 )
440+ } )
473441 } )
474-
475- const invalidationCommand = new local . Command (
476- registerName ( 'Invalidate' ) ,
477- {
478- create : pulumi . interpolate `aws cloudfront create-invalidation --distribution-id ${ distribution . id } --paths /\*` ,
479- triggers : [ staticHash . hash , prerenderedHash . hash ] ,
480- } ,
481- {
482- dependsOn : [ distribution ] ,
483- } ,
484- )
485442}
0 commit comments