]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/tools/peertube-plugins.ts
Fix s3 mock cleanup
[github/Chocobozzz/PeerTube.git] / server / tools / peertube-plugins.ts
1 import CliTable3 from 'cli-table3'
2 import { Command, OptionValues, program } from 'commander'
3 import { isAbsolute } from 'path'
4 import { PluginType } from '../../shared/models'
5 import { assignToken, buildServer, getServerCredentials } from './shared'
6
7 program
8 .name('plugins')
9 .usage('[command] [options]')
10
11 program
12 .command('list')
13 .description('List installed plugins')
14 .option('-u, --url <url>', 'Server url')
15 .option('-U, --username <username>', 'Username')
16 .option('-p, --password <token>', 'Password')
17 .option('-t, --only-themes', 'List themes only')
18 .option('-P, --only-plugins', 'List plugins only')
19 .action((options, command) => pluginsListCLI(command, options))
20
21 program
22 .command('install')
23 .description('Install a plugin or a theme')
24 .option('-u, --url <url>', 'Server url')
25 .option('-U, --username <username>', 'Username')
26 .option('-p, --password <token>', 'Password')
27 .option('-P --path <path>', 'Install from a path')
28 .option('-n, --npm-name <npmName>', 'Install from npm')
29 .option('--plugin-version <pluginVersion>', 'Specify the plugin version to install (only available when installing from npm)')
30 .action((options, command) => installPluginCLI(command, options))
31
32 program
33 .command('update')
34 .description('Update a plugin or a theme')
35 .option('-u, --url <url>', 'Server url')
36 .option('-U, --username <username>', 'Username')
37 .option('-p, --password <token>', 'Password')
38 .option('-P --path <path>', 'Update from a path')
39 .option('-n, --npm-name <npmName>', 'Update from npm')
40 .action((options, command) => updatePluginCLI(command, options))
41
42 program
43 .command('uninstall')
44 .description('Uninstall a plugin or a theme')
45 .option('-u, --url <url>', 'Server url')
46 .option('-U, --username <username>', 'Username')
47 .option('-p, --password <token>', 'Password')
48 .option('-n, --npm-name <npmName>', 'NPM plugin/theme name')
49 .action((options, command) => uninstallPluginCLI(command, options))
50
51 if (!process.argv.slice(2).length) {
52 program.outputHelp()
53 }
54
55 program.parse(process.argv)
56
57 // ----------------------------------------------------------------------------
58
59 async function pluginsListCLI (command: Command, options: OptionValues) {
60 const { url, username, password } = await getServerCredentials(command)
61 const server = buildServer(url)
62 await assignToken(server, username, password)
63
64 let pluginType: PluginType
65 if (options.onlyThemes) pluginType = PluginType.THEME
66 if (options.onlyPlugins) pluginType = PluginType.PLUGIN
67
68 const { data } = await server.plugins.list({ start: 0, count: 100, sort: 'name', pluginType })
69
70 const table = new CliTable3({
71 head: [ 'name', 'version', 'homepage' ],
72 colWidths: [ 50, 20, 50 ]
73 }) as any
74
75 for (const plugin of data) {
76 const npmName = plugin.type === PluginType.PLUGIN
77 ? 'peertube-plugin-' + plugin.name
78 : 'peertube-theme-' + plugin.name
79
80 table.push([
81 npmName,
82 plugin.version,
83 plugin.homepage
84 ])
85 }
86
87 console.log(table.toString())
88 process.exit(0)
89 }
90
91 async function installPluginCLI (command: Command, options: OptionValues) {
92 if (!options.path && !options.npmName) {
93 console.error('You need to specify the npm name or the path of the plugin you want to install.\n')
94 program.outputHelp()
95 process.exit(-1)
96 }
97
98 if (options.path && !isAbsolute(options.path)) {
99 console.error('Path should be absolute.')
100 process.exit(-1)
101 }
102
103 const { url, username, password } = await getServerCredentials(command)
104 const server = buildServer(url)
105 await assignToken(server, username, password)
106
107 try {
108 await server.plugins.install({ npmName: options.npmName, path: options.path, pluginVersion: options.pluginVersion })
109 } catch (err) {
110 console.error('Cannot install plugin.', err)
111 process.exit(-1)
112 }
113
114 console.log('Plugin installed.')
115 process.exit(0)
116 }
117
118 async function updatePluginCLI (command: Command, options: OptionValues) {
119 if (!options.path && !options.npmName) {
120 console.error('You need to specify the npm name or the path of the plugin you want to update.\n')
121 program.outputHelp()
122 process.exit(-1)
123 }
124
125 if (options.path && !isAbsolute(options.path)) {
126 console.error('Path should be absolute.')
127 process.exit(-1)
128 }
129
130 const { url, username, password } = await getServerCredentials(command)
131 const server = buildServer(url)
132 await assignToken(server, username, password)
133
134 try {
135 await server.plugins.update({ npmName: options.npmName, path: options.path })
136 } catch (err) {
137 console.error('Cannot update plugin.', err)
138 process.exit(-1)
139 }
140
141 console.log('Plugin updated.')
142 process.exit(0)
143 }
144
145 async function uninstallPluginCLI (command: Command, options: OptionValues) {
146 if (!options.npmName) {
147 console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n')
148 program.outputHelp()
149 process.exit(-1)
150 }
151
152 const { url, username, password } = await getServerCredentials(command)
153 const server = buildServer(url)
154 await assignToken(server, username, password)
155
156 try {
157 await server.plugins.uninstall({ npmName: options.npmName })
158 } catch (err) {
159 console.error('Cannot uninstall plugin.', err)
160 process.exit(-1)
161 }
162
163 console.log('Plugin uninstalled.')
164 process.exit(0)
165 }