]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/peertube-plugins.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-plugins.ts
1 // eslint-disable @typescript-eslint/no-unnecessary-type-assertion
2
3 import { registerTSPaths } from '../helpers/register-ts-paths'
4 registerTSPaths()
5
6 import * as program from 'commander'
7 import { installPlugin, listPlugins, uninstallPlugin, updatePlugin } from '../../shared/extra-utils/server/plugins'
8 import { getAdminTokenOrDie, getServerCredentials } from './cli'
9 import { PeerTubePlugin, PluginType } from '../../shared/models'
10 import { isAbsolute } from 'path'
11 import * as CliTable3 from 'cli-table3'
12 import commander = require('commander')
13
14 program
15 .name('plugins')
16 .usage('[command] [options]')
17
18 program
19 .command('list')
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((options, command) => pluginsListCLI(command, options))
27
28 program
29 .command('install')
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, command) => installPluginCLI(command, options))
37
38 program
39 .command('update')
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, command) => updatePluginCLI(command, options))
47
48 program
49 .command('uninstall')
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, command) => uninstallPluginCLI(command, options))
56
57 if (!process.argv.slice(2).length) {
58 program.outputHelp()
59 }
60
61 program.parse(process.argv)
62
63 // ----------------------------------------------------------------------------
64
65 async function pluginsListCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
66 const { url, username, password } = await getServerCredentials(command)
67 const accessToken = await getAdminTokenOrDie(url, username, password)
68
69 let pluginType: PluginType
70 if (options.onlyThemes) pluginType = PluginType.THEME
71 if (options.onlyPlugins) pluginType = PluginType.PLUGIN
72
73 const res = await listPlugins({
74 url,
75 accessToken,
76 start: 0,
77 count: 100,
78 sort: 'name',
79 pluginType
80 })
81 const plugins: PeerTubePlugin[] = res.body.data
82
83 const table = new CliTable3({
84 head: [ 'name', 'version', 'homepage' ],
85 colWidths: [ 50, 10, 50 ]
86 }) as any
87
88 for (const plugin of plugins) {
89 const npmName = plugin.type === PluginType.PLUGIN
90 ? 'peertube-plugin-' + plugin.name
91 : 'peertube-theme-' + plugin.name
92
93 table.push([
94 npmName,
95 plugin.version,
96 plugin.homepage
97 ])
98 }
99
100 console.log(table.toString())
101 process.exit(0)
102 }
103
104 async function installPluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
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')
107 program.outputHelp()
108 process.exit(-1)
109 }
110
111 if (options.path && !isAbsolute(options.path)) {
112 console.error('Path should be absolute.')
113 process.exit(-1)
114 }
115
116 const { url, username, password } = await getServerCredentials(command)
117 const accessToken = await getAdminTokenOrDie(url, username, password)
118
119 try {
120 await installPlugin({
121 url,
122 accessToken,
123 npmName: options.npmName,
124 path: options.path
125 })
126 } catch (err) {
127 console.error('Cannot install plugin.', err)
128 process.exit(-1)
129 }
130
131 console.log('Plugin installed.')
132 process.exit(0)
133 }
134
135 async function updatePluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
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')
138 program.outputHelp()
139 process.exit(-1)
140 }
141
142 if (options.path && !isAbsolute(options.path)) {
143 console.error('Path should be absolute.')
144 process.exit(-1)
145 }
146
147 const { url, username, password } = await getServerCredentials(command)
148 const accessToken = await getAdminTokenOrDie(url, username, password)
149
150 try {
151 await updatePlugin({
152 url,
153 accessToken,
154 npmName: options.npmName,
155 path: options.path
156 })
157 } catch (err) {
158 console.error('Cannot update plugin.', err)
159 process.exit(-1)
160 }
161
162 console.log('Plugin updated.')
163 process.exit(0)
164 }
165
166 async function uninstallPluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
167 if (!options.npmName) {
168 console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n')
169 program.outputHelp()
170 process.exit(-1)
171 }
172
173 const { url, username, password } = await getServerCredentials(command)
174 const accessToken = await getAdminTokenOrDie(url, username, password)
175
176 try {
177 await uninstallPlugin({
178 url,
179 accessToken,
180 npmName: options.npmName
181 })
182 } catch (err) {
183 console.error('Cannot uninstall plugin.', err)
184 process.exit(-1)
185 }
186
187 console.log('Plugin uninstalled.')
188 process.exit(0)
189 }