1 // eslint-disable @typescript-eslint/no-unnecessary-type-assertion
3 import { registerTSPaths } from '../helpers/register-ts-paths'
6 import * as program 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'
12 import commander = require('commander')
16 .usage('[command] [options]')
20 .description('List installed plugins')
21 .option('-u, --url <url>', 'Server url')
22 .option('-U, --username <username>', 'Username')
23 .option('-p, --password <token>', 'Password')
24 .option('-t, --only-themes', 'List themes only')
25 .option('-P, --only-plugins', 'List plugins only')
26 .action((options, command) => pluginsListCLI(command, options))
30 .description('Install a plugin or a theme')
31 .option('-u, --url <url>', 'Server url')
32 .option('-U, --username <username>', 'Username')
33 .option('-p, --password <token>', 'Password')
34 .option('-P --path <path>', 'Install from a path')
35 .option('-n, --npm-name <npmName>', 'Install from npm')
36 .action((options, command) => installPluginCLI(command, options))
40 .description('Update a plugin or a theme')
41 .option('-u, --url <url>', 'Server url')
42 .option('-U, --username <username>', 'Username')
43 .option('-p, --password <token>', 'Password')
44 .option('-P --path <path>', 'Update from a path')
45 .option('-n, --npm-name <npmName>', 'Update from npm')
46 .action((options, command) => updatePluginCLI(command, options))
50 .description('Uninstall a plugin or a theme')
51 .option('-u, --url <url>', 'Server url')
52 .option('-U, --username <username>', 'Username')
53 .option('-p, --password <token>', 'Password')
54 .option('-n, --npm-name <npmName>', 'NPM plugin/theme name')
55 .action((options, command) => uninstallPluginCLI(command, options))
57 if (!process.argv.slice(2).length) {
61 program.parse(process.argv)
63 // ----------------------------------------------------------------------------
65 async function pluginsListCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
66 const { url, username, password } = await getServerCredentials(command)
67 const accessToken = await getAdminTokenOrDie(url, username, password)
69 let pluginType: PluginType
70 if (options.onlyThemes) pluginType = PluginType.THEME
71 if (options.onlyPlugins) pluginType = PluginType.PLUGIN
73 const res = await listPlugins({
81 const plugins: PeerTubePlugin[] = res.body.data
83 const table = new CliTable3({
84 head: [ 'name', 'version', 'homepage' ],
85 colWidths: [ 50, 10, 50 ]
88 for (const plugin of plugins) {
89 const npmName = plugin.type === PluginType.PLUGIN
90 ? 'peertube-plugin-' + plugin.name
91 : 'peertube-theme-' + plugin.name
100 console.log(table.toString())
104 async function installPluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
105 if (!options.path && !options.npmName) {
106 console.error('You need to specify the npm name or the path of the plugin you want to install.\n')
111 if (options.path && !isAbsolute(options.path)) {
112 console.error('Path should be absolute.')
116 const { url, username, password } = await getServerCredentials(command)
117 const accessToken = await getAdminTokenOrDie(url, username, password)
120 await installPlugin({
123 npmName: options.npmName,
127 console.error('Cannot install plugin.', err)
131 console.log('Plugin installed.')
135 async function updatePluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
136 if (!options.path && !options.npmName) {
137 console.error('You need to specify the npm name or the path of the plugin you want to update.\n')
142 if (options.path && !isAbsolute(options.path)) {
143 console.error('Path should be absolute.')
147 const { url, username, password } = await getServerCredentials(command)
148 const accessToken = await getAdminTokenOrDie(url, username, password)
154 npmName: options.npmName,
158 console.error('Cannot update plugin.', err)
162 console.log('Plugin updated.')
166 async function uninstallPluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
167 if (!options.npmName) {
168 console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n')
173 const { url, username, password } = await getServerCredentials(command)
174 const accessToken = await getAdminTokenOrDie(url, username, password)
177 await uninstallPlugin({
180 npmName: options.npmName
183 console.error('Cannot uninstall plugin.', err)
187 console.log('Plugin uninstalled.')