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