1 | const crypto = require('crypto');
|
2 | const AWS = require('aws-sdk');
|
3 | const { Readable } = require('stream');
|
4 | const schema = require('@financial-times/tc-schema-sdk');
|
5 |
|
6 | const s3Client = new AWS.S3({ region: 'eu-west-1' });
|
7 |
|
8 | const getVersion = schemaObject => {
|
9 | const asString = JSON.stringify(schemaObject, null, 2);
|
10 | return crypto.createHash('md5').update(asString).digest('hex');
|
11 | };
|
12 |
|
13 | const sendSchemaToS3 = async (
|
14 | environment,
|
15 | bucketName = process.env.TREECREEPER_SCHEMA_BUCKET,
|
16 | ) => {
|
17 | await schema.ready();
|
18 | const schemaObject = {
|
19 | ...schema.rawData.getAll(),
|
20 | };
|
21 | schemaObject.version = schemaObject.version || getVersion(schemaObject);
|
22 | const schemaAsString = JSON.stringify(schemaObject, null, 2);
|
23 |
|
24 | const uploadStream = new Readable();
|
25 | uploadStream.push(schemaAsString);
|
26 | uploadStream.push(null);
|
27 |
|
28 |
|
29 | console.log(`Deploying schema to ${bucketName}/${environment}/schema.json`);
|
30 |
|
31 | await s3Client
|
32 | .upload({
|
33 | Bucket: bucketName,
|
34 | Key: `${environment}/schema.json`,
|
35 | Body: uploadStream,
|
36 | ContentType: 'application/json',
|
37 | })
|
38 | .promise();
|
39 | };
|
40 |
|
41 | module.exports = { sendSchemaToS3 };
|