This repository was archived by the owner on May 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathupdate-plugins.ts
115 lines (101 loc) · 3.13 KB
/
update-plugins.ts
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import axios, { AxiosError } from "axios";
import dotenv from "dotenv";
import * as fs from "fs/promises";
dotenv.config();
const individual = process.env["PLUGINS_UPDATE_INDIVIDUAL"] !== undefined;
if (individual)
console.log("Updating plugins individually. This will take a while.");
const plugins = process.argv
.slice(2)
.filter((file) => file.match(/^plugins\/[A-Za-z0-9_\-\.]*\/index\.ts$/))
.map((file) => file.replace(/\.ts$/, ".js"))
.map(async (file) => {
let plugin = await import(`../dist/cjs/${file}`);
return plugin.default;
});
const readmes = process.argv
.slice(2)
.filter((file) =>
file.match(/^plugins\/[A-Za-z0-9_\-\.]*\/(README|readme)\.md$/)
)
.map(async (file) => ({
name: file.replace(/^plugins\/(.*)\/(README|readme)\.md$/, "$1"),
content: await fs.readFile(file, { encoding: "utf8" }),
}));
const updateRemote = async ({
plugins,
readmes,
}: {
plugins?: any[];
readmes?: any[];
}) => {
return await axios.post(
"https://api.fig.io/plugins/update",
{ plugins: plugins ?? [], readmes: readmes ?? [] },
{
headers: {
Authorization: `Bearer ${process.env.PLUGINS_UPDATE_TOKEN}`,
"Content-Type": "application/json",
},
}
);
};
Promise.all([Promise.all(plugins), Promise.all(readmes)]).then(
async ([plugins, readmes]) => {
console.log("Updating plugins:");
for (const plugin of plugins) {
console.log(`- ${plugin.name}`);
}
console.log("Updating readmes:");
for (const readme of readmes) {
console.log(`- ${readme.name}`);
}
if (individual) {
let error = false;
for (const plugin of plugins) {
updateRemote({ plugins: [plugin] })
.then((res) => {
console.log(`${res.status} ${res.statusText}`);
console.log(`Updated plugin: ${plugin.name}`);
})
.catch((err: AxiosError) => {
console.log(
`Failed to update plugin (${plugin.name}):`,
err.message
);
error = true;
});
// Sleep for a bit to avoid hitting the API too quickly
await new Promise((resolve) => setTimeout(resolve, 100));
}
for (const readme of readmes) {
updateRemote({ readmes: [readme] })
.then((res) => {
console.log(`${res.status} ${res.statusText}`);
console.log(`Updated readme: ${readme.name}`);
})
.catch((err: AxiosError) => {
console.log(
`Failed to update readme (${readme.name}):`,
err.message
);
error = true;
});
// Sleep for a bit to avoid hitting the API too quickly
await new Promise((resolve) => setTimeout(resolve, 100));
}
if (error) process.exit(1);
} else {
updateRemote({ plugins, readmes })
.then((res) => {
console.log(`${res.status} ${res.statusText}`);
console.log("Updated plugins");
process.exit(0);
})
.catch((err: AxiosError) => {
console.log("Failed to update:", err.message);
process.exit(1);
});
}
}
);