-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpublish.js
91 lines (74 loc) · 2.24 KB
/
publish.js
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
const fs = require( 'fs' ).promises;
const path = require( 'path' );
const CloudFront = require( 'aws-sdk/clients/cloudfront' );
const S3 = require( 'aws-sdk/clients/s3' );
const mime = require( 'mime-types' );
const {
cfDistributionId: DistributionId,
s3Bucket: Bucket,
} = require( './config' );
const cloudfront = new CloudFront();
const s3 = new S3();
const rootDir = path.join( __dirname, 'build' );
const invalidationParams = {
DistributionId,
InvalidationBatch: {
CallerReference: `wordpress-com-static-site-${new Date().getTime()}`,
Paths: {
Quantity: 1, // a wildcard counts as one path
Items: [
'/*',
],
},
},
};
const longLivedAssetExtensions = [ 'css', 'gif', 'jpg', 'jpeg', 'png', 'svg', 'woff', 'woff2' ];
function invalidate () {
if ( ! DistributionId ) {
console.log( `No CloudFront distribution defined in config, skipping invalidation...` );
return Promise.resolve();
}
console.log( `Invalidating distribution ${DistributionId}...` );
return cloudfront.createInvalidation( invalidationParams ).promise();
}
function putS3Object ( options ) {
return s3.putObject( options ).promise();
}
async function publishDir ( dir ) {
const filesOrFolders = await fs.readdir( dir );
for ( const fileOrFolder of filesOrFolders ) {
const fullPath = path.join( dir, fileOrFolder );
const stat = await fs.stat( fullPath );
if ( stat.isDirectory() ) {
await publishDir( fullPath );
}
if ( stat.isFile() ) {
const contents = await fs.readFile( fullPath );
const extension = fileOrFolder.split( '.' ).pop();
const Key = fullPath.replace( `${rootDir}/`, '' );
let ContentType = mime.lookup( extension );
if ( ! ContentType ) {
ContentType = 'text/html; charset=utf-8';
}
if ( 'feed' === fileOrFolder ) {
ContentType = 'application/atom+xml; charset=utf-8';
}
let CacheControl = null;
if ( longLivedAssetExtensions.includes( extension ) ) {
CacheControl = 'max-age=31556952';
}
console.log( `Uploading ${Key} (${ContentType})...` );
await putS3Object( {
ACL: 'public-read',
Body: Buffer.from( contents ),
Bucket,
CacheControl,
ContentType,
Key,
} );
}
}
}
publishDir( rootDir )
.then( () => invalidate() )
.then( () => console.log( 'Published!' ) );