UNPKG

1.14 kBJavaScriptView Raw
1const crypto = require('crypto');
2const AWS = require('aws-sdk');
3const { Readable } = require('stream');
4const schema = require('@financial-times/tc-schema-sdk');
5
6const s3Client = new AWS.S3({ region: 'eu-west-1' });
7
8const getVersion = schemaObject => {
9 const asString = JSON.stringify(schemaObject, null, 2);
10 return crypto.createHash('md5').update(asString).digest('hex');
11};
12
13const 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 // eslint-disable-next-line no-console
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
41module.exports = { sendSchemaToS3 };