]>
Commit | Line | Data |
---|---|---|
ae2abfd3 C |
1 | /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ |
2 | ||
3 | import { readJSON, writeJSON } from 'fs-extra' | |
4 | import { join } from 'path' | |
5 | import { root } from '@server/helpers/core-utils' | |
6 | import { HttpStatusCode } from '@shared/core-utils' | |
7 | import { | |
8 | PeerTubePlugin, | |
9 | PeerTubePluginIndex, | |
10 | PeertubePluginIndexList, | |
11 | PluginPackageJson, | |
12 | PluginTranslation, | |
13 | PluginType, | |
14 | PublicServerSetting, | |
15 | RegisteredServerSettings, | |
16 | ResultList | |
17 | } from '@shared/models' | |
18 | import { buildServerDirectory } from '../miscs' | |
19 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | |
20 | ||
21 | export class PluginsCommand extends AbstractCommand { | |
22 | ||
23 | static getPluginTestPath (suffix = '') { | |
24 | return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix) | |
25 | } | |
26 | ||
27 | list (options: OverrideCommandOptions & { | |
28 | start?: number | |
29 | count?: number | |
30 | sort?: string | |
31 | pluginType?: PluginType | |
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 | }, | |
a1637fa1 | 48 | implicitToken: true, |
ae2abfd3 C |
49 | defaultExpectedStatus: HttpStatusCode.OK_200 |
50 | }) | |
51 | } | |
52 | ||
53 | listAvailable (options: OverrideCommandOptions & { | |
54 | start?: number | |
55 | count?: number | |
56 | sort?: string | |
57 | pluginType?: PluginType | |
58 | currentPeerTubeEngine?: string | |
59 | search?: string | |
60 | expectedStatus?: HttpStatusCode | |
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, | |
a1637fa1 | 79 | implicitToken: true, |
ae2abfd3 C |
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, | |
a1637fa1 | 93 | implicitToken: true, |
ae2abfd3 C |
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 }, | |
a1637fa1 | 110 | implicitToken: true, |
ae2abfd3 C |
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, | |
a1637fa1 | 124 | implicitToken: true, |
ae2abfd3 C |
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, | |
a1637fa1 | 139 | implicitToken: false, |
ae2abfd3 C |
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, | |
a1637fa1 | 154 | implicitToken: false, |
ae2abfd3 C |
155 | defaultExpectedStatus: HttpStatusCode.OK_200 |
156 | }) | |
157 | } | |
158 | ||
159 | install (options: OverrideCommandOptions & { | |
160 | path?: string | |
161 | npmName?: string | |
162 | }) { | |
163 | const { npmName, path } = options | |
164 | const apiPath = '/api/v1/plugins/install' | |
165 | ||
166 | return this.postBodyRequest({ | |
167 | ...options, | |
168 | ||
169 | path: apiPath, | |
170 | fields: { npmName, path }, | |
a1637fa1 | 171 | implicitToken: true, |
ae2abfd3 C |
172 | defaultExpectedStatus: HttpStatusCode.OK_200 |
173 | }) | |
174 | } | |
175 | ||
176 | update (options: OverrideCommandOptions & { | |
177 | path?: string | |
178 | npmName?: string | |
179 | }) { | |
180 | const { npmName, path } = options | |
181 | const apiPath = '/api/v1/plugins/update' | |
182 | ||
183 | return this.postBodyRequest({ | |
184 | ...options, | |
185 | ||
186 | path: apiPath, | |
187 | fields: { npmName, path }, | |
a1637fa1 | 188 | implicitToken: true, |
ae2abfd3 C |
189 | defaultExpectedStatus: HttpStatusCode.OK_200 |
190 | }) | |
191 | } | |
192 | ||
193 | uninstall (options: OverrideCommandOptions & { | |
194 | npmName: string | |
195 | }) { | |
196 | const { npmName } = options | |
197 | const apiPath = '/api/v1/plugins/uninstall' | |
198 | ||
199 | return this.postBodyRequest({ | |
200 | ...options, | |
201 | ||
202 | path: apiPath, | |
203 | fields: { npmName }, | |
a1637fa1 | 204 | implicitToken: true, |
ae2abfd3 C |
205 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 |
206 | }) | |
207 | } | |
208 | ||
209 | getCSS (options: OverrideCommandOptions = {}) { | |
210 | const path = '/plugins/global.css' | |
211 | ||
212 | return this.getRequestText({ | |
213 | ...options, | |
214 | ||
215 | path, | |
a1637fa1 | 216 | implicitToken: false, |
ae2abfd3 C |
217 | defaultExpectedStatus: HttpStatusCode.OK_200 |
218 | }) | |
219 | } | |
220 | ||
221 | getExternalAuth (options: OverrideCommandOptions & { | |
222 | npmName: string | |
223 | npmVersion: string | |
224 | authName: string | |
225 | query?: any | |
226 | }) { | |
227 | const { npmName, npmVersion, authName, query } = options | |
228 | ||
229 | const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName | |
230 | ||
231 | return this.getRequest({ | |
232 | ...options, | |
233 | ||
234 | path, | |
235 | query, | |
a1637fa1 | 236 | implicitToken: false, |
ae2abfd3 C |
237 | defaultExpectedStatus: HttpStatusCode.OK_200, |
238 | redirects: 0 | |
239 | }) | |
240 | } | |
241 | ||
242 | updatePackageJSON (npmName: string, json: any) { | |
243 | const path = this.getPackageJSONPath(npmName) | |
244 | ||
245 | return writeJSON(path, json) | |
246 | } | |
247 | ||
248 | getPackageJSON (npmName: string): Promise<PluginPackageJson> { | |
249 | const path = this.getPackageJSONPath(npmName) | |
250 | ||
251 | return readJSON(path) | |
252 | } | |
253 | ||
254 | private getPackageJSONPath (npmName: string) { | |
255 | return buildServerDirectory(this.server, join('plugins', 'node_modules', npmName, 'package.json')) | |
256 | } | |
257 | } |