1 // eslint-disable @typescript-eslint/no-unnecessary-type-assertion
3 import { registerTSPaths } from '../helpers/register-ts-paths'
6 import { program, Command, OptionValues } from 'commander'
7 import { installPlugin, listPlugins, uninstallPlugin, updatePlugin } from '../../shared/extra-utils/server/plugins'
8 import { getAdminTokenOrDie, getServerCredentials } from './cli'
9 import { PeerTubePlugin, PluginType } from '../../shared/models'
10 import { isAbsolute } from 'path'
11 import * as CliTable3 from 'cli-table3'
15 .usage('[command] [options]')
19 .description('List installed plugins')
20 .option('-u, --url <url>', 'Server url')
21 .option('-U, --username <username>', 'Username')
22 .option('-p, --password <token>', 'Password')
23 .option('-t, --only-themes', 'List themes only')
24 .option('-P, --only-plugins', 'List plugins only')
25 .action((options, command) => pluginsListCLI(command, options))
29 .description('Install a plugin or a theme')
30 .option('-u, --url <url>', 'Server url')
31 .option('-U, --username <username>', 'Username')
32 .option('-p, --password <token>', 'Password')
33 .option('-P --path <path>', 'Install from a path')
34 .option('-n, --npm-name <npmName>', 'Install from npm')
35 .action((options, command) => installPluginCLI(command, options))
39 .description('Update a plugin or a theme')
40 .option('-u, --url <url>', 'Server url')
41 .option('-U, --username <username>', 'Username')
42 .option('-p, --password <token>', 'Password')
43 .option('-P --path <path>', 'Update from a path')
44 .option('-n, --npm-name <npmName>', 'Update from npm')
45 .action((options, command) => updatePluginCLI(command, options))
49 .description('Uninstall a plugin or a theme')
50 .option('-u, --url <url>', 'Server url')
51 .option('-U, --username <username>', 'Username')
52 .option('-p, --password <token>', 'Password')
53 .option('-n, --npm-name <npmName>', 'NPM plugin/theme name')
54 .action((options, command) => uninstallPluginCLI(command, options))
56 if (!process.argv.slice(2).length) {
60 program.parse(process.argv)
62 // ----------------------------------------------------------------------------
64 async function pluginsListCLI (command: Command, options: OptionValues) {
65 const { url, username, password } = await getServerCredentials(command)
66 const accessToken = await getAdminTokenOrDie(url, username, password)
68 let pluginType: PluginType
69 if (options.onlyThemes) pluginType = PluginType.THEME
70 if (options.onlyPlugins) pluginType = PluginType.PLUGIN
72 const res = await listPlugins({
80 const plugins: PeerTubePlugin[] = res.body.data
82 const table = new CliTable3({
83 head: [ 'name', 'version', 'homepage' ],
84 colWidths: [ 50, 10, 50 ]
87 for (const plugin of plugins) {
88 const npmName = plugin.type === PluginType.PLUGIN
89 ? 'peertube-plugin-' + plugin.name
90 : 'peertube-theme-' + plugin.name
99 console.log(table.toString())
103 async function installPluginCLI (command: Command, options: OptionValues) {
104 if (!options.path && !options.npmName) {
105 console.error('You need to specify the npm name or the path of the plugin you want to install.\n')
110 if (options.path && !isAbsolute(options.path)) {
111 console.error('Path should be absolute.')
115 const { url, username, password } = await getServerCredentials(command)
116 const accessToken = await getAdminTokenOrDie(url, username, password)
119 await installPlugin({
122 npmName: options.npmName,
126 console.error('Cannot install plugin.', err)
130 console.log('Plugin installed.')
134 async function updatePluginCLI (command: Command, options: OptionValues) {
135 if (!options.path && !options.npmName) {
136 console.error('You need to specify the npm name or the path of the plugin you want to update.\n')
141 if (options.path && !isAbsolute(options.path)) {
142 console.error('Path should be absolute.')
146 const { url, username, password } = await getServerCredentials(command)
147 const accessToken = await getAdminTokenOrDie(url, username, password)
153 npmName: options.npmName,
157 console.error('Cannot update plugin.', err)
161 console.log('Plugin updated.')
165 async function uninstallPluginCLI (command: Command, options: OptionValues) {
166 if (!options.npmName) {
167 console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n')
172 const { url, username, password } = await getServerCredentials(command)
173 const accessToken = await getAdminTokenOrDie(url, username, password)
176 await uninstallPlugin({
179 npmName: options.npmName
182 console.error('Cannot uninstall plugin.', err)
186 console.log('Plugin uninstalled.')