]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/server/plugins-command.ts
CLI: plugins install command accept a --plugin-version parameter. (#4599)
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / server / plugins-command.ts
CommitLineData
ae2abfd3
C
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'
ae2abfd3 6import {
4c7e60bc 7 HttpStatusCode,
ae2abfd3
C
8 PeerTubePlugin,
9 PeerTubePluginIndex,
10 PeertubePluginIndexList,
11 PluginPackageJson,
12 PluginTranslation,
13 PluginType,
14 PublicServerSetting,
15 RegisteredServerSettings,
16 ResultList
17} from '@shared/models'
ae2abfd3
C
18import { AbstractCommand, OverrideCommandOptions } from '../shared'
19
20export class PluginsCommand extends AbstractCommand {
21
22 static getPluginTestPath (suffix = '') {
23 return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix)
24 }
25
26 list (options: OverrideCommandOptions & {
27 start?: number
28 count?: number
29 sort?: string
30 pluginType?: PluginType
31 uninstalled?: boolean
32 }) {
33 const { start, count, sort, pluginType, uninstalled } = options
34 const path = '/api/v1/plugins'
35
36 return this.getRequestBody<ResultList<PeerTubePlugin>>({
37 ...options,
38
39 path,
40 query: {
41 start,
42 count,
43 sort,
44 pluginType,
45 uninstalled
46 },
a1637fa1 47 implicitToken: true,
ae2abfd3
C
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,
a1637fa1 78 implicitToken: true,
ae2abfd3
C
79 defaultExpectedStatus: HttpStatusCode.OK_200
80 })
81 }
82
83 get (options: OverrideCommandOptions & {
84 npmName: string
85 }) {
86 const path = '/api/v1/plugins/' + options.npmName
87
88 return this.getRequestBody<PeerTubePlugin>({
89 ...options,
90
91 path,
a1637fa1 92 implicitToken: true,
ae2abfd3
C
93 defaultExpectedStatus: HttpStatusCode.OK_200
94 })
95 }
96
97 updateSettings (options: OverrideCommandOptions & {
98 npmName: string
99 settings: any
100 }) {
101 const { npmName, settings } = options
102 const path = '/api/v1/plugins/' + npmName + '/settings'
103
104 return this.putBodyRequest({
105 ...options,
106
107 path,
108 fields: { settings },
a1637fa1 109 implicitToken: true,
ae2abfd3
C
110 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
111 })
112 }
113
114 getRegisteredSettings (options: OverrideCommandOptions & {
115 npmName: string
116 }) {
117 const path = '/api/v1/plugins/' + options.npmName + '/registered-settings'
118
119 return this.getRequestBody<RegisteredServerSettings>({
120 ...options,
121
122 path,
a1637fa1 123 implicitToken: true,
ae2abfd3
C
124 defaultExpectedStatus: HttpStatusCode.OK_200
125 })
126 }
127
128 getPublicSettings (options: OverrideCommandOptions & {
129 npmName: string
130 }) {
131 const { npmName } = options
132 const path = '/api/v1/plugins/' + npmName + '/public-settings'
133
134 return this.getRequestBody<PublicServerSetting>({
135 ...options,
136
137 path,
a1637fa1 138 implicitToken: false,
ae2abfd3
C
139 defaultExpectedStatus: HttpStatusCode.OK_200
140 })
141 }
142
143 getTranslations (options: OverrideCommandOptions & {
144 locale: string
145 }) {
146 const { locale } = options
147 const path = '/plugins/translations/' + locale + '.json'
148
149 return this.getRequestBody<PluginTranslation>({
150 ...options,
151
152 path,
a1637fa1 153 implicitToken: false,
ae2abfd3
C
154 defaultExpectedStatus: HttpStatusCode.OK_200
155 })
156 }
157
158 install (options: OverrideCommandOptions & {
159 path?: string
160 npmName?: string
3a1157a6 161 pluginVersion?: string
ae2abfd3 162 }) {
3a1157a6 163 const { npmName, path, pluginVersion } = options
ae2abfd3
C
164 const apiPath = '/api/v1/plugins/install'
165
166 return this.postBodyRequest({
167 ...options,
168
169 path: apiPath,
3a1157a6 170 fields: { npmName, path, pluginVersion },
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) {
89d241a7 255 return this.server.servers.buildDirectory(join('plugins', 'node_modules', npmName, 'package.json'))
ae2abfd3
C
256 }
257}