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 .option('--plugin-version <pluginVersion>', 'Specify the plugin version to install (only available when installing 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 server = buildServer(url)
67 await assignToken(server, username, password)
69 let pluginType: PluginType
70 if (options.onlyThemes) pluginType = PluginType.THEME
71 if (options.onlyPlugins) pluginType = PluginType.PLUGIN
73 const { data } = await server.plugins.list({ start: 0, count: 100, sort: 'name', pluginType })
75 const table = new CliTable3({
76 head: [ 'name', 'version', 'homepage' ],
77 colWidths: [ 50, 10, 50 ]
80 for (const plugin of data) {
81 const npmName = plugin.type === PluginType.PLUGIN
82 ? 'peertube-plugin-' + plugin.name
83 : 'peertube-theme-' + plugin.name
92 console.log(table.toString())
96 async function installPluginCLI (command: Command, options: OptionValues) {
97 if (!options.path && !options.npmName) {
98 console.error('You need to specify the npm name or the path of the plugin you want to install.\n')
103 if (options.path && !isAbsolute(options.path)) {
104 console.error('Path should be absolute.')
108 const { url, username, password } = await getServerCredentials(command)
109 const server = buildServer(url)
110 await assignToken(server, username, password)
113 await server.plugins.install({ npmName: options.npmName, path: options.path, pluginVersion: options.pluginVersion })
115 console.error('Cannot install plugin.', err)
119 console.log('Plugin installed.')
123 async function updatePluginCLI (command: Command, options: OptionValues) {
124 if (!options.path && !options.npmName) {
125 console.error('You need to specify the npm name or the path of the plugin you want to update.\n')
130 if (options.path && !isAbsolute(options.path)) {
131 console.error('Path should be absolute.')
135 const { url, username, password } = await getServerCredentials(command)
136 const server = buildServer(url)
137 await assignToken(server, username, password)
140 await server.plugins.update({ npmName: options.npmName, path: options.path })
142 console.error('Cannot update plugin.', err)
146 console.log('Plugin updated.')
150 async function uninstallPluginCLI (command: Command, options: OptionValues) {
151 if (!options.npmName) {
152 console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n')
157 const { url, username, password } = await getServerCredentials(command)
158 const server = buildServer(url)
159 await assignToken(server, username, password)
162 await server.plugins.uninstall({ npmName: options.npmName })
164 console.error('Cannot uninstall plugin.', err)
168 console.log('Plugin uninstalled.')