peertube-plugins.ts 5.3 KB

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