-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
89 lines (77 loc) · 3.27 KB
/
deploy.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const {AutotaskClient} = require('@openzeppelin/defender-autotask-client');
const fs = require('fs').promises;
const path = require('path');
const JSZip = require("jszip");
require('dotenv').config();
const client = new AutotaskClient({
apiKey: process.env.DEFENDER_API_KEY,
apiSecret: process.env.DEFENDER_API_SECRET
});
async function createFoldersAndConfigs(items) {
const baseDirectory = path.join(__dirname, 'tasks');
fs.mkdir(baseDirectory, {recursive: true})
.then(() => {
items.forEach(async item => {
const folderPath = path.join(baseDirectory, item.name);
// Ensure the folder exists, create if it doesn't
await fs.mkdir(folderPath, {recursive: true});
// Now that we know the folder exists, create/replace the config.json file
const configPath = path.join(folderPath, 'config.json');
await fs.writeFile(configPath, JSON.stringify(item, null, 2), 'utf8');
});
})
.catch(err => console.error('Error creating base directory:', err));
}
async function updateAndCreateTasks() {
const baseDirectory = path.join(__dirname, 'tasks');
try {
const directories = await fs.readdir(baseDirectory, {withFileTypes: true});
const folders = directories.filter(dirent => dirent.isDirectory()).map(dirent => dirent.name);
for (const folder of folders) {
console.log("Deploying: ", folder)
let configData;
let autotaskId;
const folderPath = path.join(baseDirectory, folder);
const configPath = path.join(folderPath, 'config.json');
const indexPath = path.join(folderPath, 'index.js');
let shouldCreate = true;
const configFile = await fs.readFile(configPath, 'utf8');
const indexFile = await fs.readFile(indexPath, 'utf8');
configData = JSON.parse(configFile);
// Check if autotaskId exists
if (configData.autotaskId) {
autotaskId = configData.autotaskId;
shouldCreate = false; // autotaskId exists, so we need to update instead of create
}
if (shouldCreate) {
const zip = new JSZip();
zip.file("index.js", indexFile);
const zippedContent = await zip.generateAsync({type: "nodebuffer"});
configData.encodedZippedCode = zippedContent.toString('base64');
try {
const createdTask = await client.create(configData);
console.log(createdTask)
} catch (e) {
console.log(e);
}
} else {
// update task
try {
await client.update(configData);
await client.updateCodeFromSources(autotaskId, {'index.js': indexFile});
} catch (e) {
console.log(e);
}
}
}
} catch
(err) {
console.error('Error updating tasks:', err);
}
}
(async () => {
await updateAndCreateTasks();
const items = await client.list();
console.log(await client.list());
await createFoldersAndConfigs(items.items);
})().catch(console.error);