-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpublish.js
69 lines (57 loc) · 2.21 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
// A script to publish JSON schemas to https://schemas.ably.com
//
// Each schema in the src directory is expected to have an equivalent version
// in versions.json, and gets published as:
//
// https://schemas.ably.com/json/${name}-${version}.json
//
// For example:
//
// https://schemas.ably.com/json/app-stats-0.0.1.json
//
// This URL is also set as the $id field in the published schema.
//
// It's expected that the surrounding environment is configured with AWS
// credentials which permit uploads to the schemas.ably.com S3 bucket in
// the Ably SDK AWS account.
const { readdir } = require('fs/promises');
const path = require('path');
const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
const srcDir = path.resolve(__dirname, 'src');
const versions = require('./versions.json');
const baseUrl = 'https://schemas.ably.com/json';
const s3Bucket = 'schemas.ably.com';
const s3 = new S3Client({
region: 'eu-west-2',
});
(async () => {
const files = await readdir(srcDir);
await Promise.all(files.map((file) => {
console.log(`Checking JSON schema ${file}`);
// check the schema has a version in versions.json
const name = path.basename(file, '.json');
if (!Object.prototype.hasOwnProperty.call(versions, name)) {
throw new Error(`Missing version for JSON schema ${name}, please add ${name} to versions.json`);
}
const version = versions[name];
// read the schema, and check it doesn't have an $id field
// eslint-disable-next-line import/no-dynamic-require, global-require
let schema = require(path.resolve(srcDir, file));
if (Object.prototype.hasOwnProperty.call(schema, '$id')) {
throw new Error(`Unexpected $id property in JSON schema ${name}, please remove`);
}
// generate the $id field and prepend it to the schema
const id = `${baseUrl}/${name}-${version}.json`;
schema = { $id: id, ...schema };
// upload to the S3 bucket
console.log(`Uploading JSON schema: ${id}...`);
return s3.send(new PutObjectCommand({
Bucket: s3Bucket,
Key: `json/${path.basename(id)}`,
Body: JSON.stringify(schema, null, 2),
ContentType: 'application/json',
ACL: 'public-read',
}));
}));
console.log('Done!');
})();