1 import { registerTSPaths } from '../helpers/register-ts-paths'
4 import * as program from 'commander'
5 import { PluginType } from '../../shared/models/plugins/plugin.type'
6 import { getAccessToken } from '../../shared/extra-utils/users/login'
7 import { getMyUserInformation } from '../../shared/extra-utils/users/users'
8 import { installPlugin, listPlugins, uninstallPlugin, updatePlugin } from '../../shared/extra-utils/server/plugins'
9 import { getServerCredentials } from './cli'
10 import { User, UserRole } from '../../shared/models/users'
11 import { PeerTubePlugin } from '../../shared/models/plugins/peertube-plugin.model'
12 import { isAbsolute } from 'path'
14 const Table = require('cli-table')
18 .usage('[command] [options]')
22 .description('List installed plugins')
23 .option('-u, --url <url>', 'Server url')
24 .option('-U, --username <username>', 'Username')
25 .option('-p, --password <token>', 'Password')
26 .option('-t, --only-themes', 'List themes only')
27 .option('-P, --only-plugins', 'List plugins only')
28 .action(() => pluginsListCLI())
32 .description('Install a plugin or a theme')
33 .option('-u, --url <url>', 'Server url')
34 .option('-U, --username <username>', 'Username')
35 .option('-p, --password <token>', 'Password')
36 .option('-P --path <path>', 'Install from a path')
37 .option('-n, --npm-name <npmName>', 'Install from npm')
38 .action((options) => installPluginCLI(options))
42 .description('Update a plugin or a theme')
43 .option('-u, --url <url>', 'Server url')
44 .option('-U, --username <username>', 'Username')
45 .option('-p, --password <token>', 'Password')
46 .option('-P --path <path>', 'Update from a path')
47 .option('-n, --npm-name <npmName>', 'Update from npm')
48 .action((options) => updatePluginCLI(options))
52 .description('Uninstall a plugin or a theme')
53 .option('-u, --url <url>', 'Server url')
54 .option('-U, --username <username>', 'Username')
55 .option('-p, --password <token>', 'Password')
56 .option('-n, --npm-name <npmName>', 'NPM plugin/theme name')
57 .action(options => uninstallPluginCLI(options))
59 if (!process.argv.slice(2).length) {
63 program.parse(process.argv)
65 // ----------------------------------------------------------------------------
67 async function pluginsListCLI () {
68 const { url, username, password } = await getServerCredentials(program)
69 const accessToken = await getAdminTokenOrDie(url, username, password)
71 let pluginType: PluginType
72 if (program['onlyThemes']) pluginType = PluginType.THEME
73 if (program['onlyPlugins']) pluginType = PluginType.PLUGIN
75 const res = await listPlugins({
83 const plugins: PeerTubePlugin[] = res.body.data
85 const table = new Table({
86 head: ['name', 'version', 'homepage'],
87 colWidths: [ 50, 10, 50 ]
90 for (const plugin of plugins) {
91 const npmName = plugin.type === PluginType.PLUGIN
92 ? 'peertube-plugin-' + plugin.name
93 : 'peertube-theme-' + plugin.name
102 console.log(table.toString())
106 async function installPluginCLI (options: any) {
107 if (!options['path'] && !options['npmName']) {
108 console.error('You need to specify the npm name or the path of the plugin you want to install.\n')
113 if (options['path'] && !isAbsolute(options['path'])) {
114 console.error('Path should be absolute.')
118 const { url, username, password } = await getServerCredentials(options)
119 const accessToken = await getAdminTokenOrDie(url, username, password)
122 await installPlugin({
125 npmName: options['npmName'],
126 path: options['path']
129 console.error('Cannot install plugin.', err)
134 console.log('Plugin installed.')
138 async function updatePluginCLI (options: any) {
139 if (!options['path'] && !options['npmName']) {
140 console.error('You need to specify the npm name or the path of the plugin you want to update.\n')
145 if (options['path'] && !isAbsolute(options['path'])) {
146 console.error('Path should be absolute.')
150 const { url, username, password } = await getServerCredentials(options)
151 const accessToken = await getAdminTokenOrDie(url, username, password)
157 npmName: options['npmName'],
158 path: options['path']
161 console.error('Cannot update plugin.', err)
166 console.log('Plugin updated.')
170 async function uninstallPluginCLI (options: any) {
171 if (!options['npmName']) {
172 console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n')
177 const { url, username, password } = await getServerCredentials(options)
178 const accessToken = await getAdminTokenOrDie(url, username, password)
181 await uninstallPlugin({
184 npmName: options[ 'npmName' ]
187 console.error('Cannot uninstall plugin.', err)
192 console.log('Plugin uninstalled.')
196 async function getAdminTokenOrDie (url: string, username: string, password: string) {
197 const accessToken = await getAccessToken(url, username, password)
198 const resMe = await getMyUserInformation(url, accessToken)
199 const me: User = resMe.body
201 if (me.role !== UserRole.ADMINISTRATOR) {
202 console.error('Cannot list plugins if you are not administrator.')