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 { PluginType } from '../../shared/models/plugins/plugin.type'
8 import { installPlugin, listPlugins, uninstallPlugin, updatePlugin } from '../../shared/extra-utils/server/plugins'
9 import { getAdminTokenOrDie, getServerCredentials } from './cli'
10 import { PeerTubePlugin } from '../../shared/models/plugins/peertube-plugin.model'
11 import { isAbsolute } from 'path'
12 import * as CliTable3 from 'cli-table3'
13 import commander = require('commander')
17 .usage('[command] [options]')
21 .description('List installed plugins')
22 .option('-u, --url <url>', 'Server url')
23 .option('-U, --username <username>', 'Username')
24 .option('-p, --password <token>', 'Password')
25 .option('-t, --only-themes', 'List themes only')
26 .option('-P, --only-plugins', 'List plugins only')
27 .action((options, command) => pluginsListCLI(command, options))
31 .description('Install a plugin or a theme')
32 .option('-u, --url <url>', 'Server url')
33 .option('-U, --username <username>', 'Username')
34 .option('-p, --password <token>', 'Password')
35 .option('-P --path <path>', 'Install from a path')
36 .option('-n, --npm-name <npmName>', 'Install from npm')
37 .action((options, command) => installPluginCLI(command, options))
41 .description('Update a plugin or a theme')
42 .option('-u, --url <url>', 'Server url')
43 .option('-U, --username <username>', 'Username')
44 .option('-p, --password <token>', 'Password')
45 .option('-P --path <path>', 'Update from a path')
46 .option('-n, --npm-name <npmName>', 'Update from npm')
47 .action((options, command) => updatePluginCLI(command, options))
51 .description('Uninstall a plugin or a theme')
52 .option('-u, --url <url>', 'Server url')
53 .option('-U, --username <username>', 'Username')
54 .option('-p, --password <token>', 'Password')
55 .option('-n, --npm-name <npmName>', 'NPM plugin/theme name')
56 .action((options, command) => uninstallPluginCLI(command, options))
58 if (!process.argv.slice(2).length) {
62 program.parse(process.argv)
64 // ----------------------------------------------------------------------------
66 async function pluginsListCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
67 const { url, username, password } = await getServerCredentials(command)
68 const accessToken = await getAdminTokenOrDie(url, username, password)
70 let pluginType: PluginType
71 if (options.onlyThemes) pluginType = PluginType.THEME
72 if (options.onlyPlugins) pluginType = PluginType.PLUGIN
74 const res = await listPlugins({
82 const plugins: PeerTubePlugin[] = res.body.data
84 const table = new CliTable3({
85 head: [ 'name', 'version', 'homepage' ],
86 colWidths: [ 50, 10, 50 ]
89 for (const plugin of plugins) {
90 const npmName = plugin.type === PluginType.PLUGIN
91 ? 'peertube-plugin-' + plugin.name
92 : 'peertube-theme-' + plugin.name
101 console.log(table.toString())
105 async function installPluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
106 if (!options.path && !options.npmName) {
107 console.error('You need to specify the npm name or the path of the plugin you want to install.\n')
112 if (options.path && !isAbsolute(options.path)) {
113 console.error('Path should be absolute.')
117 const { url, username, password } = await getServerCredentials(command)
118 const accessToken = await getAdminTokenOrDie(url, username, password)
121 await installPlugin({
124 npmName: options.npmName,
128 console.error('Cannot install plugin.', err)
132 console.log('Plugin installed.')
136 async function updatePluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
137 if (!options.path && !options.npmName) {
138 console.error('You need to specify the npm name or the path of the plugin you want to update.\n')
143 if (options.path && !isAbsolute(options.path)) {
144 console.error('Path should be absolute.')
148 const { url, username, password } = await getServerCredentials(command)
149 const accessToken = await getAdminTokenOrDie(url, username, password)
155 npmName: options.npmName,
159 console.error('Cannot update plugin.', err)
163 console.log('Plugin updated.')
167 async function uninstallPluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
168 if (!options.npmName) {
169 console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n')
174 const { url, username, password } = await getServerCredentials(command)
175 const accessToken = await getAdminTokenOrDie(url, username, password)
178 await uninstallPlugin({
181 npmName: options.npmName
184 console.error('Cannot uninstall plugin.', err)
188 console.log('Plugin uninstalled.')