]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/tools/peertube-plugins.ts
Reorganize plugin models
[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 6import * as program from 'commander'
b5f919ac 7import { installPlugin, listPlugins, uninstallPlugin, updatePlugin } from '../../shared/extra-utils/server/plugins'
26fcf2ef 8import { getAdminTokenOrDie, getServerCredentials } from './cli'
428ccb8b 9import { PeerTubePlugin, PluginType } from '../../shared/models'
8d2be0ed 10import { isAbsolute } from 'path'
26fcf2ef 11import * as CliTable3 from 'cli-table3'
ba5a8d89 12import commander = require('commander')
8d2be0ed
C
13
14program
15 .name('plugins')
16 .usage('[command] [options]')
17
18program
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')
1e0741d1 26 .action((options, command) => pluginsListCLI(command, options))
8d2be0ed
C
27
28program
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')
ba5a8d89 36 .action((options, command) => installPluginCLI(command, options))
8d2be0ed 37
b5f919ac
C
38program
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')
ba5a8d89 46 .action((options, command) => updatePluginCLI(command, options))
b5f919ac 47
8d2be0ed
C
48program
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')
ba5a8d89 55 .action((options, command) => uninstallPluginCLI(command, options))
8d2be0ed
C
56
57if (!process.argv.slice(2).length) {
58 program.outputHelp()
59}
60
61program.parse(process.argv)
62
63// ----------------------------------------------------------------------------
64
1e0741d1
JL
65async function pluginsListCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
66 const { url, username, password } = await getServerCredentials(command)
8d2be0ed
C
67 const accessToken = await getAdminTokenOrDie(url, username, password)
68
09071200 69 let pluginType: PluginType
ba5a8d89
C
70 if (options.onlyThemes) pluginType = PluginType.THEME
71 if (options.onlyPlugins) pluginType = PluginType.PLUGIN
8d2be0ed
C
72
73 const res = await listPlugins({
74 url,
75 accessToken,
76 start: 0,
77 count: 100,
78 sort: 'name',
09071200 79 pluginType
8d2be0ed
C
80 })
81 const plugins: PeerTubePlugin[] = res.body.data
82
26fcf2ef 83 const table = new CliTable3({
a1587156 84 head: [ 'name', 'version', 'homepage' ],
8d2be0ed 85 colWidths: [ 50, 10, 50 ]
a1587156 86 }) as any
8d2be0ed
C
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
ba5a8d89
C
104async function installPluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
105 if (!options.path && !options.npmName) {
8d2be0ed
C
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
ba5a8d89 111 if (options.path && !isAbsolute(options.path)) {
8d2be0ed
C
112 console.error('Path should be absolute.')
113 process.exit(-1)
114 }
115
ba5a8d89 116 const { url, username, password } = await getServerCredentials(command)
8d2be0ed
C
117 const accessToken = await getAdminTokenOrDie(url, username, password)
118
119 try {
120 await installPlugin({
121 url,
122 accessToken,
ba5a8d89
C
123 npmName: options.npmName,
124 path: options.path
8d2be0ed
C
125 })
126 } catch (err) {
127 console.error('Cannot install plugin.', err)
128 process.exit(-1)
8d2be0ed
C
129 }
130
131 console.log('Plugin installed.')
132 process.exit(0)
133}
134
ba5a8d89
C
135async function updatePluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
136 if (!options.path && !options.npmName) {
b5f919ac
C
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
ba5a8d89 142 if (options.path && !isAbsolute(options.path)) {
b5f919ac
C
143 console.error('Path should be absolute.')
144 process.exit(-1)
145 }
146
ba5a8d89 147 const { url, username, password } = await getServerCredentials(command)
b5f919ac
C
148 const accessToken = await getAdminTokenOrDie(url, username, password)
149
150 try {
151 await updatePlugin({
152 url,
153 accessToken,
ba5a8d89
C
154 npmName: options.npmName,
155 path: options.path
b5f919ac
C
156 })
157 } catch (err) {
158 console.error('Cannot update plugin.', err)
159 process.exit(-1)
b5f919ac
C
160 }
161
162 console.log('Plugin updated.')
163 process.exit(0)
164}
165
ba5a8d89
C
166async function uninstallPluginCLI (command: commander.CommanderStatic, options: commander.OptionValues) {
167 if (!options.npmName) {
8d2be0ed
C
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
ba5a8d89 173 const { url, username, password } = await getServerCredentials(command)
8d2be0ed
C
174 const accessToken = await getAdminTokenOrDie(url, username, password)
175
176 try {
177 await uninstallPlugin({
178 url,
179 accessToken,
ba5a8d89 180 npmName: options.npmName
8d2be0ed
C
181 })
182 } catch (err) {
183 console.error('Cannot uninstall plugin.', err)
184 process.exit(-1)
8d2be0ed
C
185 }
186
187 console.log('Plugin uninstalled.')
188 process.exit(0)
189}