1 /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
3 import { readJSON, writeJSON } from 'fs-extra'
4 import { join } from 'path'
5 import { root } from '@shared/core-utils'
10 PeertubePluginIndexList,
15 RegisteredServerSettings,
17 } from '@shared/models'
18 import { AbstractCommand, OverrideCommandOptions } from '../shared'
20 export class PluginsCommand extends AbstractCommand {
22 static getPluginTestPath (suffix = '') {
23 return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix)
26 list (options: OverrideCommandOptions & {
30 pluginType?: PluginType
33 const { start, count, sort, pluginType, uninstalled } = options
34 const path = '/api/v1/plugins'
36 return this.getRequestBody<ResultList<PeerTubePlugin>>({
48 defaultExpectedStatus: HttpStatusCode.OK_200
52 listAvailable (options: OverrideCommandOptions & {
56 pluginType?: PluginType
57 currentPeerTubeEngine?: string
59 expectedStatus?: HttpStatusCode
61 const { start, count, sort, pluginType, search, currentPeerTubeEngine } = options
62 const path = '/api/v1/plugins/available'
64 const query: PeertubePluginIndexList = {
69 currentPeerTubeEngine,
73 return this.getRequestBody<ResultList<PeerTubePluginIndex>>({
79 defaultExpectedStatus: HttpStatusCode.OK_200
83 get (options: OverrideCommandOptions & {
86 const path = '/api/v1/plugins/' + options.npmName
88 return this.getRequestBody<PeerTubePlugin>({
93 defaultExpectedStatus: HttpStatusCode.OK_200
97 updateSettings (options: OverrideCommandOptions & {
101 const { npmName, settings } = options
102 const path = '/api/v1/plugins/' + npmName + '/settings'
104 return this.putBodyRequest({
108 fields: { settings },
110 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
114 getRegisteredSettings (options: OverrideCommandOptions & {
117 const path = '/api/v1/plugins/' + options.npmName + '/registered-settings'
119 return this.getRequestBody<RegisteredServerSettings>({
124 defaultExpectedStatus: HttpStatusCode.OK_200
128 getPublicSettings (options: OverrideCommandOptions & {
131 const { npmName } = options
132 const path = '/api/v1/plugins/' + npmName + '/public-settings'
134 return this.getRequestBody<PublicServerSetting>({
138 implicitToken: false,
139 defaultExpectedStatus: HttpStatusCode.OK_200
143 getTranslations (options: OverrideCommandOptions & {
146 const { locale } = options
147 const path = '/plugins/translations/' + locale + '.json'
149 return this.getRequestBody<PluginTranslation>({
153 implicitToken: false,
154 defaultExpectedStatus: HttpStatusCode.OK_200
158 install (options: OverrideCommandOptions & {
161 pluginVersion?: string
163 const { npmName, path, pluginVersion } = options
164 const apiPath = '/api/v1/plugins/install'
166 return this.postBodyRequest({
170 fields: { npmName, path, pluginVersion },
172 defaultExpectedStatus: HttpStatusCode.OK_200
176 update (options: OverrideCommandOptions & {
180 const { npmName, path } = options
181 const apiPath = '/api/v1/plugins/update'
183 return this.postBodyRequest({
187 fields: { npmName, path },
189 defaultExpectedStatus: HttpStatusCode.OK_200
193 uninstall (options: OverrideCommandOptions & {
196 const { npmName } = options
197 const apiPath = '/api/v1/plugins/uninstall'
199 return this.postBodyRequest({
205 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
209 getCSS (options: OverrideCommandOptions = {}) {
210 const path = '/plugins/global.css'
212 return this.getRequestText({
216 implicitToken: false,
217 defaultExpectedStatus: HttpStatusCode.OK_200
221 getExternalAuth (options: OverrideCommandOptions & {
227 const { npmName, npmVersion, authName, query } = options
229 const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName
231 return this.getRequest({
236 implicitToken: false,
237 defaultExpectedStatus: HttpStatusCode.OK_200,
242 updatePackageJSON (npmName: string, json: any) {
243 const path = this.getPackageJSONPath(npmName)
245 return writeJSON(path, json)
248 getPackageJSON (npmName: string): Promise<PluginPackageJSON> {
249 const path = this.getPackageJSONPath(npmName)
251 return readJSON(path)
254 private getPackageJSONPath (npmName: string) {
255 return this.server.servers.buildDirectory(join('plugins', 'node_modules', npmName, 'package.json'))