plugins-command.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { readJSON, writeJSON } from 'fs-extra'
  3. import { join } from 'path'
  4. import { root } from '@shared/core-utils'
  5. import {
  6. HttpStatusCode,
  7. PeerTubePlugin,
  8. PeerTubePluginIndex,
  9. PeertubePluginIndexList,
  10. PluginPackageJSON,
  11. PluginTranslation,
  12. PluginType,
  13. PublicServerSetting,
  14. RegisteredServerSettings,
  15. ResultList
  16. } from '@shared/models'
  17. import { AbstractCommand, OverrideCommandOptions } from '../shared'
  18. export class PluginsCommand extends AbstractCommand {
  19. static getPluginTestPath (suffix = '') {
  20. return join(root(), 'server', 'tests', 'fixtures', 'peertube-plugin-test' + suffix)
  21. }
  22. list (options: OverrideCommandOptions & {
  23. start?: number
  24. count?: number
  25. sort?: string
  26. pluginType?: PluginType
  27. uninstalled?: boolean
  28. }) {
  29. const { start, count, sort, pluginType, uninstalled } = options
  30. const path = '/api/v1/plugins'
  31. return this.getRequestBody<ResultList<PeerTubePlugin>>({
  32. ...options,
  33. path,
  34. query: {
  35. start,
  36. count,
  37. sort,
  38. pluginType,
  39. uninstalled
  40. },
  41. implicitToken: true,
  42. defaultExpectedStatus: HttpStatusCode.OK_200
  43. })
  44. }
  45. listAvailable (options: OverrideCommandOptions & {
  46. start?: number
  47. count?: number
  48. sort?: string
  49. pluginType?: PluginType
  50. currentPeerTubeEngine?: string
  51. search?: string
  52. expectedStatus?: HttpStatusCode
  53. }) {
  54. const { start, count, sort, pluginType, search, currentPeerTubeEngine } = options
  55. const path = '/api/v1/plugins/available'
  56. const query: PeertubePluginIndexList = {
  57. start,
  58. count,
  59. sort,
  60. pluginType,
  61. currentPeerTubeEngine,
  62. search
  63. }
  64. return this.getRequestBody<ResultList<PeerTubePluginIndex>>({
  65. ...options,
  66. path,
  67. query,
  68. implicitToken: true,
  69. defaultExpectedStatus: HttpStatusCode.OK_200
  70. })
  71. }
  72. get (options: OverrideCommandOptions & {
  73. npmName: string
  74. }) {
  75. const path = '/api/v1/plugins/' + options.npmName
  76. return this.getRequestBody<PeerTubePlugin>({
  77. ...options,
  78. path,
  79. implicitToken: true,
  80. defaultExpectedStatus: HttpStatusCode.OK_200
  81. })
  82. }
  83. updateSettings (options: OverrideCommandOptions & {
  84. npmName: string
  85. settings: any
  86. }) {
  87. const { npmName, settings } = options
  88. const path = '/api/v1/plugins/' + npmName + '/settings'
  89. return this.putBodyRequest({
  90. ...options,
  91. path,
  92. fields: { settings },
  93. implicitToken: true,
  94. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  95. })
  96. }
  97. getRegisteredSettings (options: OverrideCommandOptions & {
  98. npmName: string
  99. }) {
  100. const path = '/api/v1/plugins/' + options.npmName + '/registered-settings'
  101. return this.getRequestBody<RegisteredServerSettings>({
  102. ...options,
  103. path,
  104. implicitToken: true,
  105. defaultExpectedStatus: HttpStatusCode.OK_200
  106. })
  107. }
  108. getPublicSettings (options: OverrideCommandOptions & {
  109. npmName: string
  110. }) {
  111. const { npmName } = options
  112. const path = '/api/v1/plugins/' + npmName + '/public-settings'
  113. return this.getRequestBody<PublicServerSetting>({
  114. ...options,
  115. path,
  116. implicitToken: false,
  117. defaultExpectedStatus: HttpStatusCode.OK_200
  118. })
  119. }
  120. getTranslations (options: OverrideCommandOptions & {
  121. locale: string
  122. }) {
  123. const { locale } = options
  124. const path = '/plugins/translations/' + locale + '.json'
  125. return this.getRequestBody<PluginTranslation>({
  126. ...options,
  127. path,
  128. implicitToken: false,
  129. defaultExpectedStatus: HttpStatusCode.OK_200
  130. })
  131. }
  132. install (options: OverrideCommandOptions & {
  133. path?: string
  134. npmName?: string
  135. pluginVersion?: string
  136. }) {
  137. const { npmName, path, pluginVersion } = options
  138. const apiPath = '/api/v1/plugins/install'
  139. return this.postBodyRequest({
  140. ...options,
  141. path: apiPath,
  142. fields: { npmName, path, pluginVersion },
  143. implicitToken: true,
  144. defaultExpectedStatus: HttpStatusCode.OK_200
  145. })
  146. }
  147. update (options: OverrideCommandOptions & {
  148. path?: string
  149. npmName?: string
  150. }) {
  151. const { npmName, path } = options
  152. const apiPath = '/api/v1/plugins/update'
  153. return this.postBodyRequest({
  154. ...options,
  155. path: apiPath,
  156. fields: { npmName, path },
  157. implicitToken: true,
  158. defaultExpectedStatus: HttpStatusCode.OK_200
  159. })
  160. }
  161. uninstall (options: OverrideCommandOptions & {
  162. npmName: string
  163. }) {
  164. const { npmName } = options
  165. const apiPath = '/api/v1/plugins/uninstall'
  166. return this.postBodyRequest({
  167. ...options,
  168. path: apiPath,
  169. fields: { npmName },
  170. implicitToken: true,
  171. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  172. })
  173. }
  174. getCSS (options: OverrideCommandOptions = {}) {
  175. const path = '/plugins/global.css'
  176. return this.getRequestText({
  177. ...options,
  178. path,
  179. implicitToken: false,
  180. defaultExpectedStatus: HttpStatusCode.OK_200
  181. })
  182. }
  183. getExternalAuth (options: OverrideCommandOptions & {
  184. npmName: string
  185. npmVersion: string
  186. authName: string
  187. query?: any
  188. }) {
  189. const { npmName, npmVersion, authName, query } = options
  190. const path = '/plugins/' + npmName + '/' + npmVersion + '/auth/' + authName
  191. return this.getRequest({
  192. ...options,
  193. path,
  194. query,
  195. implicitToken: false,
  196. defaultExpectedStatus: HttpStatusCode.OK_200,
  197. redirects: 0
  198. })
  199. }
  200. updatePackageJSON (npmName: string, json: any) {
  201. const path = this.getPackageJSONPath(npmName)
  202. return writeJSON(path, json)
  203. }
  204. getPackageJSON (npmName: string): Promise<PluginPackageJSON> {
  205. const path = this.getPackageJSONPath(npmName)
  206. return readJSON(path)
  207. }
  208. private getPackageJSONPath (npmName: string) {
  209. return this.server.servers.buildDirectory(join('plugins', 'node_modules', npmName, 'package.json'))
  210. }
  211. }