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, updatePlugin } 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('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) => updatePluginCLI(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 => uninstallPluginCLI(options))
56 if (!process.argv.slice(2).length) {
60 program.parse(process.argv)
62 // ----------------------------------------------------------------------------
64 async function pluginsListCLI () {
65 const { url, username, password } = await getServerCredentials(program)
66 const accessToken = await getAdminTokenOrDie(url, username, password)
68 let pluginType: PluginType
69 if (program['onlyThemes']) pluginType = PluginType.THEME
70 if (program['onlyPlugins']) pluginType = PluginType.PLUGIN
72 const res = await listPlugins({
80 const plugins: PeerTubePlugin[] = res.body.data
82 const table = new Table({
83 head: ['name', 'version', 'homepage'],
84 colWidths: [ 50, 10, 50 ]
87 for (const plugin of plugins) {
88 const npmName = plugin.type === PluginType.PLUGIN
89 ? 'peertube-plugin-' + plugin.name
90 : 'peertube-theme-' + plugin.name
99 console.log(table.toString())
103 async function installPluginCLI (options: any) {
104 if (!options['path'] && !options['npmName']) {
105 console.error('You need to specify the npm name or the path of the plugin you want to install.\n')
110 if (options['path'] && !isAbsolute(options['path'])) {
111 console.error('Path should be absolute.')
115 const { url, username, password } = await getServerCredentials(options)
116 const accessToken = await getAdminTokenOrDie(url, username, password)
119 await installPlugin({
122 npmName: options['npmName'],
123 path: options['path']
126 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)
163 console.log('Plugin updated.')
167 async function uninstallPluginCLI (options: any) {
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(options)
175 const accessToken = await getAdminTokenOrDie(url, username, password)
178 await uninstallPlugin({
181 npmName: options[ 'npmName' ]
184 console.error('Cannot uninstall plugin.', err)
189 console.log('Plugin uninstalled.')
193 async function getAdminTokenOrDie (url: string, username: string, password: string) {
194 const accessToken = await getAccessToken(url, username, password)
195 const resMe = await getMyUserInformation(url, accessToken)
196 const me: User = resMe.body
198 if (me.role !== UserRole.ADMINISTRATOR) {
199 console.error('Cannot list plugins if you are not administrator.')