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 { assignToken, buildServer, getServerCredentials } from './cli'
8 import { PluginType } from '../../shared/models'
9 import { isAbsolute } from 'path'
10 import CliTable3 from 'cli-table3'
14 .usage('[command] [options]')
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((options, command) => pluginsListCLI(command, options))
28 .description('Install a plugin or a theme')
29 .option('-u, --url <url>', 'Server url')
30 .option('-U, --username <username>', 'Username')
31 .option('-p, --password <token>', 'Password')
32 .option('-P --path <path>', 'Install from a path')
33 .option('-n, --npm-name <npmName>', 'Install from npm')
34 .action((options, command) => installPluginCLI(command, options))
38 .description('Update a plugin or a theme')
39 .option('-u, --url <url>', 'Server url')
40 .option('-U, --username <username>', 'Username')
41 .option('-p, --password <token>', 'Password')
42 .option('-P --path <path>', 'Update from a path')
43 .option('-n, --npm-name <npmName>', 'Update from npm')
44 .action((options, command) => updatePluginCLI(command, options))
48 .description('Uninstall a plugin or a theme')
49 .option('-u, --url <url>', 'Server url')
50 .option('-U, --username <username>', 'Username')
51 .option('-p, --password <token>', 'Password')
52 .option('-n, --npm-name <npmName>', 'NPM plugin/theme name')
53 .action((options, command) => uninstallPluginCLI(command, options))
55 if (!process.argv.slice(2).length) {
59 program.parse(process.argv)
61 // ----------------------------------------------------------------------------
63 async function pluginsListCLI (command: Command, options: OptionValues) {
64 const { url, username, password } = await getServerCredentials(command)
65 const server = buildServer(url)
66 await assignToken(server, username, password)
68 let pluginType: PluginType
69 if (options.onlyThemes) pluginType = PluginType.THEME
70 if (options.onlyPlugins) pluginType = PluginType.PLUGIN
72 const { data } = await server.plugins.list({ start: 0, count: 100, sort: 'name', pluginType })
74 const table = new CliTable3({
75 head: [ 'name', 'version', 'homepage' ],
76 colWidths: [ 50, 10, 50 ]
79 for (const plugin of data) {
80 const npmName = plugin.type === PluginType.PLUGIN
81 ? 'peertube-plugin-' + plugin.name
82 : 'peertube-theme-' + plugin.name
91 console.log(table.toString())
95 async function installPluginCLI (command: Command, options: OptionValues) {
96 if (!options.path && !options.npmName) {
97 console.error('You need to specify the npm name or the path of the plugin you want to install.\n')
102 if (options.path && !isAbsolute(options.path)) {
103 console.error('Path should be absolute.')
107 const { url, username, password } = await getServerCredentials(command)
108 const server = buildServer(url)
109 await assignToken(server, username, password)
112 await server.plugins.install({ npmName: options.npmName, path: options.path })
114 console.error('Cannot install plugin.', err)
118 console.log('Plugin installed.')
122 async function updatePluginCLI (command: Command, options: OptionValues) {
123 if (!options.path && !options.npmName) {
124 console.error('You need to specify the npm name or the path of the plugin you want to update.\n')
129 if (options.path && !isAbsolute(options.path)) {
130 console.error('Path should be absolute.')
134 const { url, username, password } = await getServerCredentials(command)
135 const server = buildServer(url)
136 await assignToken(server, username, password)
139 await server.plugins.update({ npmName: options.npmName, path: options.path })
141 console.error('Cannot update plugin.', err)
145 console.log('Plugin updated.')
149 async function uninstallPluginCLI (command: Command, options: OptionValues) {
150 if (!options.npmName) {
151 console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n')
156 const { url, username, password } = await getServerCredentials(command)
157 const server = buildServer(url)
158 await assignToken(server, username, password)
161 await server.plugins.uninstall({ npmName: options.npmName })
163 console.error('Cannot uninstall plugin.', err)
167 console.log('Plugin uninstalled.')