1 import * as program from 'commander'
2 import { PluginType } from '../../shared/models/plugins/plugin.type'
3 import { getAccessToken } from '../../shared/extra-utils/users/login'
4 import { getMyUserInformation } from '../../shared/extra-utils/users/users'
5 import { installPlugin, listPlugins, uninstallPlugin } from '../../shared/extra-utils/server/plugins'
6 import { getServerCredentials } from './cli'
7 import { User, UserRole } from '../../shared/models/users'
8 import { PeerTubePlugin } from '../../shared/models/plugins/peertube-plugin.model'
9 import { isAbsolute } from 'path'
11 const Table = require('cli-table')
15 .usage('[command] [options]')
19 .description('List installed plugins')
20 .option('-u, --url <url>', 'Server url')
21 .option('-U, --username <username>', 'Username')
22 .option('-p, --password <token>', 'Password')
23 .option('-t, --only-themes', 'List themes only')
24 .option('-P, --only-plugins', 'List plugins only')
25 .action(() => pluginsListCLI())
29 .description('Install a plugin or a theme')
30 .option('-u, --url <url>', 'Server url')
31 .option('-U, --username <username>', 'Username')
32 .option('-p, --password <token>', 'Password')
33 .option('-P --path <path>', 'Install from a path')
34 .option('-n, --npm-name <npmName>', 'Install from npm')
35 .action((options) => installPluginCLI(options))
39 .description('Uninstall 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('-n, --npm-name <npmName>', 'NPM plugin/theme name')
44 .action(options => uninstallPluginCLI(options))
46 if (!process.argv.slice(2).length) {
50 program.parse(process.argv)
52 // ----------------------------------------------------------------------------
54 async function pluginsListCLI () {
55 const { url, username, password } = await getServerCredentials(program)
56 const accessToken = await getAdminTokenOrDie(url, username, password)
59 if (program['onlyThemes']) type = PluginType.THEME
60 if (program['onlyPlugins']) type = PluginType.PLUGIN
62 const res = await listPlugins({
70 const plugins: PeerTubePlugin[] = res.body.data
72 const table = new Table({
73 head: ['name', 'version', 'homepage'],
74 colWidths: [ 50, 10, 50 ]
77 for (const plugin of plugins) {
78 const npmName = plugin.type === PluginType.PLUGIN
79 ? 'peertube-plugin-' + plugin.name
80 : 'peertube-theme-' + plugin.name
89 console.log(table.toString())
93 async function installPluginCLI (options: any) {
94 if (!options['path'] && !options['npmName']) {
95 console.error('You need to specify the npm name or the path of the plugin you want to install.\n')
100 if (options['path'] && !isAbsolute(options['path'])) {
101 console.error('Path should be absolute.')
105 const { url, username, password } = await getServerCredentials(options)
106 const accessToken = await getAdminTokenOrDie(url, username, password)
109 await installPlugin({
112 npmName: options['npmName'],
113 path: options['path']
116 console.error('Cannot install plugin.', err)
121 console.log('Plugin installed.')
125 async function uninstallPluginCLI (options: any) {
126 if (!options['npmName']) {
127 console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n')
132 const { url, username, password } = await getServerCredentials(options)
133 const accessToken = await getAdminTokenOrDie(url, username, password)
136 await uninstallPlugin({
139 npmName: options[ 'npmName' ]
142 console.error('Cannot uninstall plugin.', err)
147 console.log('Plugin uninstalled.')
151 async function getAdminTokenOrDie (url: string, username: string, password: string) {
152 const accessToken = await getAccessToken(url, username, password)
153 const resMe = await getMyUserInformation(url, accessToken)
154 const me: User = resMe.body
156 if (me.role !== UserRole.ADMINISTRATOR) {
157 console.error('Cannot list plugins if you are not administrator.')