forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetopenrpcspecs.js
53 lines (45 loc) · 1.73 KB
/
getopenrpcspecs.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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
const axios = require('axios');
const fs = require('fs');
const path = require('path');
// Create directory
const topdir = path.join(__dirname, "../open-spec");
if (!fs.existsSync(topdir)){
fs.mkdirSync(topdir);
}
const downloadFile = (branch) => {
const branchdir = path.join(topdir,branch);
if (!fs.existsSync(branchdir)){
fs.mkdirSync(branchdir);
}
axios({
method: "get",
url: `https://raw.githubusercontent.com/MystenLabs/sui/${branch}/crates/sui-open-rpc/spec/openrpc.json`,
responseType: "blob"
}).then((res) => {
if (fs.existsSync(path.join(__dirname, `../open-spec/${branch}/openrpc_backup.json`))){
fs.unlink(path.join(__dirname, `../open-spec/${branch}/openrpc_backup.json`), (err) => {
if (err) {
return console.log(err);
}
console.log(`Deleted ${branch} backup spec.`)
} )
} else {
console.log(`Backup file for ${branch} does not exist.`)
}
if (fs.existsSync(path.join(__dirname, `../open-spec/${branch}/openrpc.json`))){
fs.renameSync(path.join(__dirname, `../open-spec/${branch}/openrpc.json`), path.join(__dirname, `../open-spec/${branch}/openrpc_backup.json`));
}
fs.writeFileSync(path.join(__dirname, `../open-spec/${branch}/openrpc.json`), res.data, 'utf8');
}).catch(err => {
console.log(`Error downloading ${branch} openrpc spec.`);
console.error(err);
})
}
// Download Mainnet OpenRPC spec
downloadFile("mainnet");
// Download Testnet OpenRPC spec
downloadFile("testnet");
// Download Devnet OpenRPC spec
downloadFile("devnet");