]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-plugins.ts
Use a class for youtube-dl
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-plugins.ts
CommitLineData
a1587156
C
1// eslint-disable @typescript-eslint/no-unnecessary-type-assertion
2
2aaa1a3f
C
3import { registerTSPaths } from '../helpers/register-ts-paths'
4registerTSPaths()
5
8d2be0ed
C
6import * as program from 'commander'
7import { PluginType } from '../../shared/models/plugins/plugin.type'
b5f919ac 8import { installPlugin, listPlugins, uninstallPlugin, updatePlugin } from '../../shared/extra-utils/server/plugins'
26fcf2ef 9import { getAdminTokenOrDie, getServerCredentials } from './cli'
8d2be0ed
C
10import { PeerTubePlugin } from '../../shared/models/plugins/peertube-plugin.model'
11import { isAbsolute } from 'path'
26fcf2ef 12import * as CliTable3 from 'cli-table3'
ba5a8d89 13import commander = require('commander')
8d2be0ed
C
14
15program
16 .name('plugins')
17 .usage('[command] [options]')
18
19program
20 .command('list')
21 .description('List installed plugins')
22 .option('-u, --url <url>', 'Server url')
23 .option('-U, --username <username>', 'Username')
24 .option('-p, --password <token>', 'Password')
25 .option('-t, --only-themes', 'List themes only')
26 .option('-P, --only-plugins', 'List plugins only')
1e0741d1 27 .action((options, command) => pluginsListCLI(command, options))
8d2be0ed
C
28
29program
30 .command('install')
31 .description('Install a plugin or a theme')
32 .option('-u, --url <url>', 'Server url')
33 .option('-U, --username <username>', 'Username')
34 .option('-p, --password <token>', 'Password')
35 .option('-P --path <path>', 'Install from a path')
36 .option('-n, --npm-name <npmName>', 'Install from npm')
ba5a8d89 37 .action((options, command) => installPluginCLI(command, options))
8d2be0ed 38
b5f919ac
C
39program
40 .command('update')
41 .description('Update a plugin or a theme')
42 .option('-u, --url <url>', 'Server url')
43 .option('-U, --username <username>', 'Username')
44 .option('-p, --password <token>', 'Password')
45 .option('-P --path <path>', 'Update from a path')
46 .option('-n, --npm-name <npmName>', 'Update from npm')
ba5a8d89 47 .action((options, command) => updatePluginCLI(command, options))
b5f919ac 48
8d2be0ed
C
49program
50 .command('uninstall')
51 .description('Uninstall a plugin or a theme')
52 .option('-u, --url <url>', 'Server url')
53 .option('-U, --username <username>', 'Username')
54 .option('-p, --password <token>', 'Password')
55 .option('-n, --npm-name <npmName>', 'NPM plugin/theme name')
ba5a8d89 56 .action((options, command) => uninstallPluginCLI(command, options))
8d2be0ed
C
57
58if (!process.argv.slice(2).length) {
59 program.outputHelp()
60}
61
62program.parse(process.argv)
63
64// ----------------------------------------------------------------------------
65
1e0741d1
JL
66async function pluginsListCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
67 const { url, username, password } = await getServerCredentials(command)
8d2be0ed
C
68 const accessToken = await getAdminTokenOrDie(url, username, password)
69
09071200 70 let pluginType: PluginType
ba5a8d89
C
71 if (options.onlyThemes) pluginType = PluginType.THEME
72 if (options.onlyPlugins) pluginType = PluginType.PLUGIN
8d2be0ed
C
73
74 const res = await listPlugins({
75 url,
76 accessToken,
77 start: 0,
78 count: 100,
79 sort: 'name',
09071200 80 pluginType
8d2be0ed
C
81 })
82 const plugins: PeerTubePlugin[] = res.body.data
83
26fcf2ef 84 const table = new CliTable3({
a1587156 85 head: [ 'name', 'version', 'homepage' ],
8d2be0ed 86 colWidths: [ 50, 10, 50 ]
a1587156 87 }) as any
8d2be0ed
C
88
89 for (const plugin of plugins) {
90 const npmName = plugin.type === PluginType.PLUGIN
91 ? 'peertube-plugin-' + plugin.name
92 : 'peertube-theme-' + plugin.name
93
94 table.push([
95 npmName,
96 plugin.version,
97 plugin.homepage
98 ])
99 }
100
101 console.log(table.toString())
102 process.exit(0)
103}
104
ba5a8d89
C
105async function installPluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
106 if (!options.path && !options.npmName) {
8d2be0ed
C
107 console.error('You need to specify the npm name or the path of the plugin you want to install.\n')
108 program.outputHelp()
109 process.exit(-1)
110 }
111
ba5a8d89 112 if (options.path && !isAbsolute(options.path)) {
8d2be0ed
C
113 console.error('Path should be absolute.')
114 process.exit(-1)
115 }
116
ba5a8d89 117 const { url, username, password } = await getServerCredentials(command)
8d2be0ed
C
118 const accessToken = await getAdminTokenOrDie(url, username, password)
119
120 try {
121 await installPlugin({
122 url,
123 accessToken,
ba5a8d89
C
124 npmName: options.npmName,
125 path: options.path
8d2be0ed
C
126 })
127 } catch (err) {
128 console.error('Cannot install plugin.', err)
129 process.exit(-1)
8d2be0ed
C
130 }
131
132 console.log('Plugin installed.')
133 process.exit(0)
134}
135
ba5a8d89
C
136async function updatePluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
137 if (!options.path && !options.npmName) {
b5f919ac
C
138 console.error('You need to specify the npm name or the path of the plugin you want to update.\n')
139 program.outputHelp()
140 process.exit(-1)
141 }
142
ba5a8d89 143 if (options.path && !isAbsolute(options.path)) {
b5f919ac
C
144 console.error('Path should be absolute.')
145 process.exit(-1)
146 }
147
ba5a8d89 148 const { url, username, password } = await getServerCredentials(command)
b5f919ac
C
149 const accessToken = await getAdminTokenOrDie(url, username, password)
150
151 try {
152 await updatePlugin({
153 url,
154 accessToken,
ba5a8d89
C
155 npmName: options.npmName,
156 path: options.path
b5f919ac
C
157 })
158 } catch (err) {
159 console.error('Cannot update plugin.', err)
160 process.exit(-1)
b5f919ac
C
161 }
162
163 console.log('Plugin updated.')
164 process.exit(0)
165}
166
ba5a8d89
C
167async function uninstallPluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
168 if (!options.npmName) {
8d2be0ed
C
169 console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n')
170 program.outputHelp()
171 process.exit(-1)
172 }
173
ba5a8d89 174 const { url, username, password } = await getServerCredentials(command)
8d2be0ed
C
175 const accessToken = await getAdminTokenOrDie(url, username, password)
176
177 try {
178 await uninstallPlugin({
179 url,
180 accessToken,
ba5a8d89 181 npmName: options.npmName
8d2be0ed
C
182 })
183 } catch (err) {
184 console.error('Cannot uninstall plugin.', err)
185 process.exit(-1)
8d2be0ed
C
186 }
187
188 console.log('Plugin uninstalled.')
189 process.exit(0)
190}