aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/server/plugins-command.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-07 10:33:49 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:17 +0200
commitae2abfd3aed3e75d39a316b49b914d187faa7475 (patch)
tree4d5060dc310f3d8cdcd5829779522f49b3d10e71 /shared/extra-utils/server/plugins-command.ts
parent9c6327f803aaf4200672f1fc40b2f43786daca47 (diff)
downloadPeerTube-ae2abfd3aed3e75d39a316b49b914d187faa7475.tar.gz
PeerTube-ae2abfd3aed3e75d39a316b49b914d187faa7475.tar.zst
PeerTube-ae2abfd3aed3e75d39a316b49b914d187faa7475.zip
Introduce plugins command
Diffstat (limited to 'shared/extra-utils/server/plugins-command.ts')
-rw-r--r--shared/extra-utils/server/plugins-command.ts245
1 files changed, 245 insertions, 0 deletions
diff --git a/shared/extra-utils/server/plugins-command.ts b/shared/extra-utils/server/plugins-command.ts
new file mode 100644
index 000000000..f06e58a22
--- /dev/null
+++ b/shared/extra-utils/server/plugins-command.ts
@@ -0,0 +1,245 @@
1/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
2
3import { readJSON, writeJSON } from 'fs-extra'
4import { join } from 'path'
5import { root } from '@server/helpers/core-utils'
6import { HttpStatusCode } from '@shared/core-utils'
7import {
8 PeerTubePlugin,
9 PeerTubePluginIndex,
10 PeertubePluginIndexList,
11 PluginPackageJson,
12 PluginTranslation,
13 PluginType,
14 PublicServerSetting,
15 RegisteredServerSettings,
16 ResultList
17} from '@shared/models'
18import { buildServerDirectory } from '../miscs'
19import { AbstractCommand, OverrideCommandOptions } from '../shared'
20
21export 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 },
48 defaultExpectedStatus: HttpStatusCode.OK_200
49 })
50 }
51
52 listAvailable (options: OverrideCommandOptions & {
53 start?: number
54 count?: number
55 sort?: string
56 pluginType?: PluginType
57 currentPeerTubeEngine?: string
58 search?: string
59 expectedStatus?: HttpStatusCode
60 }) {
61 const { start, count, sort, pluginType, search, currentPeerTubeEngine } = options
62 const path = '/api/v1/plugins/available'
63
64 const query: PeertubePluginIndexList = {
65 start,
66 count,
67 sort,
68 pluginType,
69 currentPeerTubeEngine,
70 search
71 }
72
73 return this.getRequestBody<ResultList<PeerTubePluginIndex>>({
74 ...options,
75
76 path,
77 query,
78 defaultExpectedStatus: HttpStatusCode.OK_200
79 })
80 }
81
82 get (options: OverrideCommandOptions & {
83 npmName: string
84 }) {
85 const path = '/api/v1/plugins/' + options.npmName
86
87 return this.getRequestBody<PeerTubePlugin>({
88 ...options,
89
90 path,
91 defaultExpectedStatus: HttpStatusCode.OK_200
92 })
93 }
94
95 updateSettings (options: OverrideCommandOptions & {
96 npmName: string
97 settings: any
98 }) {
99 const { npmName, settings } = options
100 const path = '/api/v1/plugins/' + npmName + '/settings'
101
102 return this.putBodyRequest({
103 ...options,
104
105 path,
106 fields: { settings },
107 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
108 })
109 }
110
111 getRegisteredSettings (options: OverrideCommandOptions & {
112 npmName: string
113 }) {
114 const path = '/api/v1/plugins/' + options.npmName + '/registered-settings'
115
116 return this.getRequestBody<RegisteredServerSettings>({
117 ...options,
118
119 path,
120 defaultExpectedStatus: HttpStatusCode.OK_200
121 })
122 }
123
124 getPublicSettings (options: OverrideCommandOptions & {
125 npmName: string
126 }) {
127 const { npmName } = options
128 const path = '/api/v1/plugins/' + npmName + '/public-settings'
129
130 return this.getRequestBody<PublicServerSetting>({
131 ...options,
132
133 path,
134 defaultExpectedStatus: HttpStatusCode.OK_200
135 })
136 }
137
138 getTranslations (options: OverrideCommandOptions & {
139 locale: string
140 }) {
141 const { locale } = options
142 const path = '/plugins/translations/' + locale + '.json'
143
144 return this.getRequestBody<PluginTranslation>({
145 ...options,
146
147 path,
148 defaultExpectedStatus: HttpStatusCode.OK_200
149 })
150 }
151
152 install (options: OverrideCommandOptions & {
153 path?: string
154 npmName?: string
155 }) {
156 const { npmName, path } = options
157 const apiPath = '/api/v1/plugins/install'
158
159 return this.postBodyRequest({
160 ...options,
161
162 path: apiPath,
163 fields: { npmName, path },
164 defaultExpectedStatus: HttpStatusCode.OK_200
165 })
166 }
167
168 update (options: OverrideCommandOptions & {
169 path?: string
170 npmName?: string
171 }) {
172 const { npmName, path } = options
173 const apiPath = '/api/v1/plugins/update'
174
175 return this.postBodyRequest({
176 ...options,
177
178 path: apiPath,
179 fields: { npmName, path },
180 defaultExpectedStatus: HttpStatusCode.OK_200
181 })
182 }
183
184 uninstall (options: OverrideCommandOptions & {
185 npmName: string
186 }) {
187 const { npmName } = options
188 const apiPath = '/api/v1/plugins/uninstall'
189
190 return this.postBodyRequest({
191 ...options,
192
193 path: apiPath,
194 fields: { npmName },
195 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
196 })
197 }
198
199 getCSS (options: OverrideCommandOptions = {}) {
200 const path = '/plugins/global.css'
201
202 return this.getRequestText({
203 ...options,
204
205 path,
206 defaultExpectedStatus: HttpStatusCode.OK_200
207 })
208 }
209
210 getExternalAuth (options: OverrideCommandOptions & {
211 npmName: string
212 npmVersion: string
213 authName: string
214 query?: any
215 }) {
216 const { npmName, npmVersion, authName, query } = options
217
218 const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName
219
220 return this.getRequest({
221 ...options,
222
223 path,
224 query,
225 defaultExpectedStatus: HttpStatusCode.OK_200,
226 redirects: 0
227 })
228 }
229
230 updatePackageJSON (npmName: string, json: any) {
231 const path = this.getPackageJSONPath(npmName)
232
233 return writeJSON(path, json)
234 }
235
236 getPackageJSON (npmName: string): Promise<PluginPackageJson> {
237 const path = this.getPackageJSONPath(npmName)
238
239 return readJSON(path)
240 }
241
242 private getPackageJSONPath (npmName: string) {
243 return buildServerDirectory(this.server, join('plugins', 'node_modules', npmName, 'package.json'))
244 }
245}