-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupload-s3.mjs
170 lines (160 loc) · 5.05 KB
/
upload-s3.mjs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import {
CloudFrontClient,
CreateInvalidationCommand
} from '@aws-sdk/client-cloudfront';
import {PutObjectCommand, S3Client} from '@aws-sdk/client-s3';
import {readFile} from 'fs/promises';
import path from 'path';
import * as url from 'url';
import pkg from './dist/shopgun-sdk/package.json' with {type: 'json'};
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
const s3Client = new S3Client({region: 'eu-west-1'});
const cfClient = new CloudFrontClient({region: 'eu-west-1'});
const bucket = 'sgn-js-sdk';
const distribution = 'EKE7310HEBVSP';
const putObject = async ({key, body, bodyPath, contentType}) => {
try {
const result = await s3Client.send(
new PutObjectCommand({
Bucket: bucket,
Key: key,
Body: body || (await readFile(bodyPath, 'utf-8')),
ACL: 'public-read',
ContentType: contentType
})
);
console.log('S3: Uploaded https://js-sdk.tjek.com/' + key);
return result;
} catch (uploadError) {
console.error(uploadError);
throw new Error('S3: Could not upload ' + key);
}
};
const putVersion = async (version) => {
const publicationViewerHtml = (
await readFile(
path.join(__dirname, 'embeds', 'publication-viewer.html'),
'utf-8'
)
).replace('x.x.x', version);
await Promise.all([
version === 'x.x.x' &&
putObject({
key: 'embeds/publication-viewer.html',
body: publicationViewerHtml,
contentType: 'text/html; charset=utf-8'
}),
putObject({
key: 'embeds/publication-viewer-' + version + '.html',
body: publicationViewerHtml,
contentType: 'text/html; charset=utf-8'
}),
putObject({
key: 'sgn-sdk-' + version + '.js',
bodyPath: path.join(__dirname, 'dist', 'shopgun-sdk', 'sgn-sdk.js'),
contentType: 'application/javascript'
}),
putObject({
key: 'sgn-sdk-' + version + '.js.map',
bodyPath: path.join(
__dirname,
'dist',
'shopgun-sdk',
'sgn-sdk.min.js.map'
),
contentType: 'application/json'
}),
putObject({
key: 'sgn-sdk-' + version + '.min.js',
bodyPath: path.join(
__dirname,
'dist',
'shopgun-sdk',
'sgn-sdk.min.js'
),
contentType: 'application/javascript'
}),
putObject({
key: 'sgn-sdk-' + version + '.min.js.map',
bodyPath: path.join(
__dirname,
'dist',
'shopgun-sdk',
'sgn-sdk.min.js.map'
),
contentType: 'application/json'
}),
putObject({
key: 'sgn-sdk-' + version + '.css',
bodyPath: path.join(
__dirname,
'dist',
'shopgun-sdk',
'sgn-sdk.css'
),
contentType: 'text/css'
}),
putObject({
key: 'sgn-sdk-' + version + '.min.css',
bodyPath: path.join(
__dirname,
'dist',
'shopgun-sdk',
'sgn-sdk.min.css'
),
contentType: 'text/css'
})
]);
return [
'/embeds/publication-viewer.html',
'/embeds/publication-viewer-' + version + '.html',
'/sgn-sdk-' + version + '.js',
'/sgn-sdk-' + version + '.js.map',
'/sgn-sdk-' + version + '.min.js',
'/sgn-sdk-' + version + '.min.js.map',
'/sgn-sdk-' + version + '.css',
'/sgn-sdk-' + version + '.min.css'
];
};
const versionRE = /(\d+)\.(\d+)\.(\d+)/;
const Items = (
await Promise.all([
// Exact version
putVersion(pkg.version),
// Patch mask
putVersion(
pkg.version.replace(versionRE, (_, maj, min) => `${maj}.${min}.x`)
),
// Minor mask
putVersion(pkg.version.replace(versionRE, (_, maj) => `${maj}.x.x`)),
// Major mask
putVersion(pkg.version.replace(versionRE, 'x.x.x'))
])
)
.flat()
.filter((value, index, array) => array.indexOf(value) === index);
try {
console.log(
'CloudFront: Submitting for invalidation: ' +
new Intl.ListFormat().format(Items)
);
await cfClient.send(
new CreateInvalidationCommand({
DistributionId: distribution,
InvalidationBatch: {
CallerReference: String(Date.now()),
Paths: {Quantity: Items.length, Items}
}
})
);
console.log(
'CloudFront: Invalidation In Progress: ' +
new Intl.ListFormat().format(Items)
);
} catch (invalidationError) {
console.error(invalidationError);
console.log(
'CloudFront: Could not invalidate ' +
new Intl.ListFormat().format(Items)
);
}