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'
6 import { HttpStatusCode } from '@shared/core-utils'
10 PeertubePluginIndexList,
15 RegisteredServerSettings,
17 } from '@shared/models'
18 import { buildServerDirectory } from '../miscs'
19 import { AbstractCommand, OverrideCommandOptions } from '../shared'
21 export class PluginsCommand extends AbstractCommand {
23 static getPluginTestPath (suffix = '') {
24 return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix)
27 list (options: OverrideCommandOptions & {
31 pluginType?: PluginType
34 const { start, count, sort, pluginType, uninstalled } = options
35 const path = '/api/v1/plugins'
37 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>>({
78 defaultExpectedStatus: HttpStatusCode.OK_200
82 get (options: OverrideCommandOptions & {
85 const path = '/api/v1/plugins/' + options.npmName
87 return this.getRequestBody<PeerTubePlugin>({
91 defaultExpectedStatus: HttpStatusCode.OK_200
95 updateSettings (options: OverrideCommandOptions & {
99 const { npmName, settings } = options
100 const path = '/api/v1/plugins/' + npmName + '/settings'
102 return this.putBodyRequest({
106 fields: { settings },
107 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
111 getRegisteredSettings (options: OverrideCommandOptions & {
114 const path = '/api/v1/plugins/' + options.npmName + '/registered-settings'
116 return this.getRequestBody<RegisteredServerSettings>({
120 defaultExpectedStatus: HttpStatusCode.OK_200
124 getPublicSettings (options: OverrideCommandOptions & {
127 const { npmName } = options
128 const path = '/api/v1/plugins/' + npmName + '/public-settings'
130 return this.getRequestBody<PublicServerSetting>({
134 defaultExpectedStatus: HttpStatusCode.OK_200
138 getTranslations (options: OverrideCommandOptions & {
141 const { locale } = options
142 const path = '/plugins/translations/' + locale + '.json'
144 return this.getRequestBody<PluginTranslation>({
148 defaultExpectedStatus: HttpStatusCode.OK_200
152 install (options: OverrideCommandOptions & {
156 const { npmName, path } = options
157 const apiPath = '/api/v1/plugins/install'
159 return this.postBodyRequest({
163 fields: { npmName, path },
164 defaultExpectedStatus: HttpStatusCode.OK_200
168 update (options: OverrideCommandOptions & {
172 const { npmName, path } = options
173 const apiPath = '/api/v1/plugins/update'
175 return this.postBodyRequest({
179 fields: { npmName, path },
180 defaultExpectedStatus: HttpStatusCode.OK_200
184 uninstall (options: OverrideCommandOptions & {
187 const { npmName } = options
188 const apiPath = '/api/v1/plugins/uninstall'
190 return this.postBodyRequest({
195 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
199 getCSS (options: OverrideCommandOptions = {}) {
200 const path = '/plugins/global.css'
202 return this.getRequestText({
206 defaultExpectedStatus: HttpStatusCode.OK_200
210 getExternalAuth (options: OverrideCommandOptions & {
216 const { npmName, npmVersion, authName, query } = options
218 const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName
220 return this.getRequest({
225 defaultExpectedStatus: HttpStatusCode.OK_200,
230 updatePackageJSON (npmName: string, json: any) {
231 const path = this.getPackageJSONPath(npmName)
233 return writeJSON(path, json)
236 getPackageJSON (npmName: string): Promise<PluginPackageJson> {
237 const path = this.getPackageJSONPath(npmName)
239 return readJSON(path)
242 private getPackageJSONPath (npmName: string) {
243 return buildServerDirectory(this.server, join('plugins', 'node_modules', npmName, 'package.json'))