peertube-plugins.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import { registerTSPaths } from '../helpers/register-ts-paths'
  2. registerTSPaths()
  3. import * as program from 'commander'
  4. import { PluginType } from '../../shared/models/plugins/plugin.type'
  5. import { getAccessToken } from '../../shared/extra-utils/users/login'
  6. import { getMyUserInformation } from '../../shared/extra-utils/users/users'
  7. import { installPlugin, listPlugins, uninstallPlugin, updatePlugin } from '../../shared/extra-utils/server/plugins'
  8. import { getServerCredentials } from './cli'
  9. import { User, UserRole } from '../../shared/models/users'
  10. import { PeerTubePlugin } from '../../shared/models/plugins/peertube-plugin.model'
  11. import { isAbsolute } from 'path'
  12. const Table = require('cli-table')
  13. program
  14. .name('plugins')
  15. .usage('[command] [options]')
  16. program
  17. .command('list')
  18. .description('List installed plugins')
  19. .option('-u, --url <url>', 'Server url')
  20. .option('-U, --username <username>', 'Username')
  21. .option('-p, --password <token>', 'Password')
  22. .option('-t, --only-themes', 'List themes only')
  23. .option('-P, --only-plugins', 'List plugins only')
  24. .action(() => pluginsListCLI())
  25. program
  26. .command('install')
  27. .description('Install a plugin or a theme')
  28. .option('-u, --url <url>', 'Server url')
  29. .option('-U, --username <username>', 'Username')
  30. .option('-p, --password <token>', 'Password')
  31. .option('-P --path <path>', 'Install from a path')
  32. .option('-n, --npm-name <npmName>', 'Install from npm')
  33. .action((options) => installPluginCLI(options))
  34. program
  35. .command('update')
  36. .description('Update a plugin or a theme')
  37. .option('-u, --url <url>', 'Server url')
  38. .option('-U, --username <username>', 'Username')
  39. .option('-p, --password <token>', 'Password')
  40. .option('-P --path <path>', 'Update from a path')
  41. .option('-n, --npm-name <npmName>', 'Update from npm')
  42. .action((options) => updatePluginCLI(options))
  43. program
  44. .command('uninstall')
  45. .description('Uninstall a plugin or a theme')
  46. .option('-u, --url <url>', 'Server url')
  47. .option('-U, --username <username>', 'Username')
  48. .option('-p, --password <token>', 'Password')
  49. .option('-n, --npm-name <npmName>', 'NPM plugin/theme name')
  50. .action(options => uninstallPluginCLI(options))
  51. if (!process.argv.slice(2).length) {
  52. program.outputHelp()
  53. }
  54. program.parse(process.argv)
  55. // ----------------------------------------------------------------------------
  56. async function pluginsListCLI () {
  57. const { url, username, password } = await getServerCredentials(program)
  58. const accessToken = await getAdminTokenOrDie(url, username, password)
  59. let pluginType: PluginType
  60. if (program['onlyThemes']) pluginType = PluginType.THEME
  61. if (program['onlyPlugins']) pluginType = PluginType.PLUGIN
  62. const res = await listPlugins({
  63. url,
  64. accessToken,
  65. start: 0,
  66. count: 100,
  67. sort: 'name',
  68. pluginType
  69. })
  70. const plugins: PeerTubePlugin[] = res.body.data
  71. const table = new Table({
  72. head: ['name', 'version', 'homepage'],
  73. colWidths: [ 50, 10, 50 ]
  74. })
  75. for (const plugin of plugins) {
  76. const npmName = plugin.type === PluginType.PLUGIN
  77. ? 'peertube-plugin-' + plugin.name
  78. : 'peertube-theme-' + plugin.name
  79. table.push([
  80. npmName,
  81. plugin.version,
  82. plugin.homepage
  83. ])
  84. }
  85. console.log(table.toString())
  86. process.exit(0)
  87. }
  88. async function installPluginCLI (options: any) {
  89. if (!options['path'] && !options['npmName']) {
  90. console.error('You need to specify the npm name or the path of the plugin you want to install.\n')
  91. program.outputHelp()
  92. process.exit(-1)
  93. }
  94. if (options['path'] && !isAbsolute(options['path'])) {
  95. console.error('Path should be absolute.')
  96. process.exit(-1)
  97. }
  98. const { url, username, password } = await getServerCredentials(options)
  99. const accessToken = await getAdminTokenOrDie(url, username, password)
  100. try {
  101. await installPlugin({
  102. url,
  103. accessToken,
  104. npmName: options['npmName'],
  105. path: options['path']
  106. })
  107. } catch (err) {
  108. console.error('Cannot install plugin.', err)
  109. process.exit(-1)
  110. return
  111. }
  112. console.log('Plugin installed.')
  113. process.exit(0)
  114. }
  115. async function updatePluginCLI (options: any) {
  116. if (!options['path'] && !options['npmName']) {
  117. console.error('You need to specify the npm name or the path of the plugin you want to update.\n')
  118. program.outputHelp()
  119. process.exit(-1)
  120. }
  121. if (options['path'] && !isAbsolute(options['path'])) {
  122. console.error('Path should be absolute.')
  123. process.exit(-1)
  124. }
  125. const { url, username, password } = await getServerCredentials(options)
  126. const accessToken = await getAdminTokenOrDie(url, username, password)
  127. try {
  128. await updatePlugin({
  129. url,
  130. accessToken,
  131. npmName: options['npmName'],
  132. path: options['path']
  133. })
  134. } catch (err) {
  135. console.error('Cannot update plugin.', err)
  136. process.exit(-1)
  137. return
  138. }
  139. console.log('Plugin updated.')
  140. process.exit(0)
  141. }
  142. async function uninstallPluginCLI (options: any) {
  143. if (!options['npmName']) {
  144. console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n')
  145. program.outputHelp()
  146. process.exit(-1)
  147. }
  148. const { url, username, password } = await getServerCredentials(options)
  149. const accessToken = await getAdminTokenOrDie(url, username, password)
  150. try {
  151. await uninstallPlugin({
  152. url,
  153. accessToken,
  154. npmName: options[ 'npmName' ]
  155. })
  156. } catch (err) {
  157. console.error('Cannot uninstall plugin.', err)
  158. process.exit(-1)
  159. return
  160. }
  161. console.log('Plugin uninstalled.')
  162. process.exit(0)
  163. }
  164. async function getAdminTokenOrDie (url: string, username: string, password: string) {
  165. const accessToken = await getAccessToken(url, username, password)
  166. const resMe = await getMyUserInformation(url, accessToken)
  167. const me: User = resMe.body
  168. if (me.role !== UserRole.ADMINISTRATOR) {
  169. console.error('Cannot list plugins if you are not administrator.')
  170. process.exit(-1)
  171. }
  172. return accessToken
  173. }