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