diff options
Diffstat (limited to 'packages/server-commands/src/server/plugins-command.ts')
-rw-r--r-- | packages/server-commands/src/server/plugins-command.ts | 258 |
1 files changed, 258 insertions, 0 deletions
diff --git a/packages/server-commands/src/server/plugins-command.ts b/packages/server-commands/src/server/plugins-command.ts new file mode 100644 index 000000000..f85ef0330 --- /dev/null +++ b/packages/server-commands/src/server/plugins-command.ts | |||
@@ -0,0 +1,258 @@ | |||
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ | ||
2 | |||
3 | import { readJSON, writeJSON } from 'fs-extra/esm' | ||
4 | import { join } from 'path' | ||
5 | import { | ||
6 | HttpStatusCode, | ||
7 | HttpStatusCodeType, | ||
8 | PeerTubePlugin, | ||
9 | PeerTubePluginIndex, | ||
10 | PeertubePluginIndexList, | ||
11 | PluginPackageJSON, | ||
12 | PluginTranslation, | ||
13 | PluginType_Type, | ||
14 | PublicServerSetting, | ||
15 | RegisteredServerSettings, | ||
16 | ResultList | ||
17 | } from '@peertube/peertube-models' | ||
18 | import { buildAbsoluteFixturePath } from '@peertube/peertube-node-utils' | ||
19 | import { AbstractCommand, OverrideCommandOptions } from '../shared/index.js' | ||
20 | |||
21 | export class PluginsCommand extends AbstractCommand { | ||
22 | |||
23 | static getPluginTestPath (suffix = '') { | ||
24 | return buildAbsoluteFixturePath('peertube-plugin-test' + suffix) | ||
25 | } | ||
26 | |||
27 | list (options: OverrideCommandOptions & { | ||
28 | start?: number | ||
29 | count?: number | ||
30 | sort?: string | ||
31 | pluginType?: PluginType_Type | ||
32 | uninstalled?: boolean | ||
33 | }) { | ||
34 | const { start, count, sort, pluginType, uninstalled } = options | ||
35 | const path = '/api/v1/plugins' | ||
36 | |||
37 | return this.getRequestBody<ResultList<PeerTubePlugin>>({ | ||
38 | ...options, | ||
39 | |||
40 | path, | ||
41 | query: { | ||
42 | start, | ||
43 | count, | ||
44 | sort, | ||
45 | pluginType, | ||
46 | uninstalled | ||
47 | }, | ||
48 | implicitToken: true, | ||
49 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
50 | }) | ||
51 | } | ||
52 | |||
53 | listAvailable (options: OverrideCommandOptions & { | ||
54 | start?: number | ||
55 | count?: number | ||
56 | sort?: string | ||
57 | pluginType?: PluginType_Type | ||
58 | currentPeerTubeEngine?: string | ||
59 | search?: string | ||
60 | expectedStatus?: HttpStatusCodeType | ||
61 | }) { | ||
62 | const { start, count, sort, pluginType, search, currentPeerTubeEngine } = options | ||
63 | const path = '/api/v1/plugins/available' | ||
64 | |||
65 | const query: PeertubePluginIndexList = { | ||
66 | start, | ||
67 | count, | ||
68 | sort, | ||
69 | pluginType, | ||
70 | currentPeerTubeEngine, | ||
71 | search | ||
72 | } | ||
73 | |||
74 | return this.getRequestBody<ResultList<PeerTubePluginIndex>>({ | ||
75 | ...options, | ||
76 | |||
77 | path, | ||
78 | query, | ||
79 | implicitToken: true, | ||
80 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
81 | }) | ||
82 | } | ||
83 | |||
84 | get (options: OverrideCommandOptions & { | ||
85 | npmName: string | ||
86 | }) { | ||
87 | const path = '/api/v1/plugins/' + options.npmName | ||
88 | |||
89 | return this.getRequestBody<PeerTubePlugin>({ | ||
90 | ...options, | ||
91 | |||
92 | path, | ||
93 | implicitToken: true, | ||
94 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
95 | }) | ||
96 | } | ||
97 | |||
98 | updateSettings (options: OverrideCommandOptions & { | ||
99 | npmName: string | ||
100 | settings: any | ||
101 | }) { | ||
102 | const { npmName, settings } = options | ||
103 | const path = '/api/v1/plugins/' + npmName + '/settings' | ||
104 | |||
105 | return this.putBodyRequest({ | ||
106 | ...options, | ||
107 | |||
108 | path, | ||
109 | fields: { settings }, | ||
110 | implicitToken: true, | ||
111 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
112 | }) | ||
113 | } | ||
114 | |||
115 | getRegisteredSettings (options: OverrideCommandOptions & { | ||
116 | npmName: string | ||
117 | }) { | ||
118 | const path = '/api/v1/plugins/' + options.npmName + '/registered-settings' | ||
119 | |||
120 | return this.getRequestBody<RegisteredServerSettings>({ | ||
121 | ...options, | ||
122 | |||
123 | path, | ||
124 | implicitToken: true, | ||
125 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
126 | }) | ||
127 | } | ||
128 | |||
129 | getPublicSettings (options: OverrideCommandOptions & { | ||
130 | npmName: string | ||
131 | }) { | ||
132 | const { npmName } = options | ||
133 | const path = '/api/v1/plugins/' + npmName + '/public-settings' | ||
134 | |||
135 | return this.getRequestBody<PublicServerSetting>({ | ||
136 | ...options, | ||
137 | |||
138 | path, | ||
139 | implicitToken: false, | ||
140 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
141 | }) | ||
142 | } | ||
143 | |||
144 | getTranslations (options: OverrideCommandOptions & { | ||
145 | locale: string | ||
146 | }) { | ||
147 | const { locale } = options | ||
148 | const path = '/plugins/translations/' + locale + '.json' | ||
149 | |||
150 | return this.getRequestBody<PluginTranslation>({ | ||
151 | ...options, | ||
152 | |||
153 | path, | ||
154 | implicitToken: false, | ||
155 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
156 | }) | ||
157 | } | ||
158 | |||
159 | install (options: OverrideCommandOptions & { | ||
160 | path?: string | ||
161 | npmName?: string | ||
162 | pluginVersion?: string | ||
163 | }) { | ||
164 | const { npmName, path, pluginVersion } = options | ||
165 | const apiPath = '/api/v1/plugins/install' | ||
166 | |||
167 | return this.postBodyRequest({ | ||
168 | ...options, | ||
169 | |||
170 | path: apiPath, | ||
171 | fields: { npmName, path, pluginVersion }, | ||
172 | implicitToken: true, | ||
173 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
174 | }) | ||
175 | } | ||
176 | |||
177 | update (options: OverrideCommandOptions & { | ||
178 | path?: string | ||
179 | npmName?: string | ||
180 | }) { | ||
181 | const { npmName, path } = options | ||
182 | const apiPath = '/api/v1/plugins/update' | ||
183 | |||
184 | return this.postBodyRequest({ | ||
185 | ...options, | ||
186 | |||
187 | path: apiPath, | ||
188 | fields: { npmName, path }, | ||
189 | implicitToken: true, | ||
190 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
191 | }) | ||
192 | } | ||
193 | |||
194 | uninstall (options: OverrideCommandOptions & { | ||
195 | npmName: string | ||
196 | }) { | ||
197 | const { npmName } = options | ||
198 | const apiPath = '/api/v1/plugins/uninstall' | ||
199 | |||
200 | return this.postBodyRequest({ | ||
201 | ...options, | ||
202 | |||
203 | path: apiPath, | ||
204 | fields: { npmName }, | ||
205 | implicitToken: true, | ||
206 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
207 | }) | ||
208 | } | ||
209 | |||
210 | getCSS (options: OverrideCommandOptions = {}) { | ||
211 | const path = '/plugins/global.css' | ||
212 | |||
213 | return this.getRequestText({ | ||
214 | ...options, | ||
215 | |||
216 | path, | ||
217 | implicitToken: false, | ||
218 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
219 | }) | ||
220 | } | ||
221 | |||
222 | getExternalAuth (options: OverrideCommandOptions & { | ||
223 | npmName: string | ||
224 | npmVersion: string | ||
225 | authName: string | ||
226 | query?: any | ||
227 | }) { | ||
228 | const { npmName, npmVersion, authName, query } = options | ||
229 | |||
230 | const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName | ||
231 | |||
232 | return this.getRequest({ | ||
233 | ...options, | ||
234 | |||
235 | path, | ||
236 | query, | ||
237 | implicitToken: false, | ||
238 | defaultExpectedStatus: HttpStatusCode.OK_200, | ||
239 | redirects: 0 | ||
240 | }) | ||
241 | } | ||
242 | |||
243 | updatePackageJSON (npmName: string, json: any) { | ||
244 | const path = this.getPackageJSONPath(npmName) | ||
245 | |||
246 | return writeJSON(path, json) | ||
247 | } | ||
248 | |||
249 | getPackageJSON (npmName: string): Promise<PluginPackageJSON> { | ||
250 | const path = this.getPackageJSONPath(npmName) | ||
251 | |||
252 | return readJSON(path) | ||
253 | } | ||
254 | |||
255 | private getPackageJSONPath (npmName: string) { | ||
256 | return this.server.servers.buildDirectory(join('plugins', 'node_modules', npmName, 'package.json')) | ||
257 | } | ||
258 | } | ||