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>>({
49 defaultExpectedStatus: HttpStatusCode.OK_200
53 listAvailable (options: OverrideCommandOptions & {
57 pluginType?: PluginType
58 currentPeerTubeEngine?: string
60 expectedStatus?: HttpStatusCode
62 const { start, count, sort, pluginType, search, currentPeerTubeEngine } = options
63 const path = '/api/v1/plugins/available'
65 const query: PeertubePluginIndexList = {
70 currentPeerTubeEngine,
74 return this.getRequestBody<ResultList<PeerTubePluginIndex>>({
80 defaultExpectedStatus: HttpStatusCode.OK_200
84 get (options: OverrideCommandOptions & {
87 const path = '/api/v1/plugins/' + options.npmName
89 return this.getRequestBody<PeerTubePlugin>({
94 defaultExpectedStatus: HttpStatusCode.OK_200
98 updateSettings (options: OverrideCommandOptions & {
102 const { npmName, settings } = options
103 const path = '/api/v1/plugins/' + npmName + '/settings'
105 return this.putBodyRequest({
109 fields: { settings },
111 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
115 getRegisteredSettings (options: OverrideCommandOptions & {
118 const path = '/api/v1/plugins/' + options.npmName + '/registered-settings'
120 return this.getRequestBody<RegisteredServerSettings>({
125 defaultExpectedStatus: HttpStatusCode.OK_200
129 getPublicSettings (options: OverrideCommandOptions & {
132 const { npmName } = options
133 const path = '/api/v1/plugins/' + npmName + '/public-settings'
135 return this.getRequestBody<PublicServerSetting>({
139 implicitToken: false,
140 defaultExpectedStatus: HttpStatusCode.OK_200
144 getTranslations (options: OverrideCommandOptions & {
147 const { locale } = options
148 const path = '/plugins/translations/' + locale + '.json'
150 return this.getRequestBody<PluginTranslation>({
154 implicitToken: false,
155 defaultExpectedStatus: HttpStatusCode.OK_200
159 install (options: OverrideCommandOptions & {
163 const { npmName, path } = options
164 const apiPath = '/api/v1/plugins/install'
166 return this.postBodyRequest({
170 fields: { npmName, path },
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 buildServerDirectory(this.server, join('plugins', 'node_modules', npmName, 'package.json'))