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 '@server/helpers/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 & {
162 const { npmName, path } = options
163 const apiPath = '/api/v1/plugins/install'
165 return this.postBodyRequest({
169 fields: { npmName, path },
171 defaultExpectedStatus: HttpStatusCode.OK_200
175 update (options: OverrideCommandOptions & {
179 const { npmName, path } = options
180 const apiPath = '/api/v1/plugins/update'
182 return this.postBodyRequest({
186 fields: { npmName, path },
188 defaultExpectedStatus: HttpStatusCode.OK_200
192 uninstall (options: OverrideCommandOptions & {
195 const { npmName } = options
196 const apiPath = '/api/v1/plugins/uninstall'
198 return this.postBodyRequest({
204 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
208 getCSS (options: OverrideCommandOptions = {}) {
209 const path = '/plugins/global.css'
211 return this.getRequestText({
215 implicitToken: false,
216 defaultExpectedStatus: HttpStatusCode.OK_200
220 getExternalAuth (options: OverrideCommandOptions & {
226 const { npmName, npmVersion, authName, query } = options
228 const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName
230 return this.getRequest({
235 implicitToken: false,
236 defaultExpectedStatus: HttpStatusCode.OK_200,
241 updatePackageJSON (npmName: string, json: any) {
242 const path = this.getPackageJSONPath(npmName)
244 return writeJSON(path, json)
247 getPackageJSON (npmName: string): Promise<PluginPackageJson> {
248 const path = this.getPackageJSONPath(npmName)
250 return readJSON(path)
253 private getPackageJSONPath (npmName: string) {
254 return this.server.servers.buildDirectory(join('plugins', 'node_modules', npmName, 'package.json'))