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'
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(() => pluginsListCLI())
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) => installPluginCLI(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) => updatePluginCLI(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 => uninstallPluginCLI(options))
57 if (!process.argv.slice(2).length) {
61 program.parse(process.argv)
63 // ----------------------------------------------------------------------------
65 async function pluginsListCLI () {
66 const { url, username, password } = await getServerCredentials(program)
67 const accessToken = await getAdminTokenOrDie(url, username, password)
69 let pluginType: PluginType
70 if (program['onlyThemes']) pluginType = PluginType.THEME
71 if (program['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 (options: any) {
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(options)
117 const accessToken = await getAdminTokenOrDie(url, username, password)
120 await installPlugin({
123 npmName: options['npmName'],
124 path: options['path']
127 console.error('Cannot install plugin.', err)
131 console.log('Plugin installed.')
135 async function updatePluginCLI (options: any) {
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(options)
148 const accessToken = await getAdminTokenOrDie(url, username, password)
154 npmName: options['npmName'],
155 path: options['path']
158 console.error('Cannot update plugin.', err)
162 console.log('Plugin updated.')
166 async function uninstallPluginCLI (options: any) {
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(options)
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.')