]>
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' |
8d2be0ed C |
13 | |
14 | program | |
15 | .name('plugins') | |
16 | .usage('[command] [options]') | |
17 | ||
18 | program | |
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') | |
26 | .action(() => pluginsListCLI()) | |
27 | ||
28 | program | |
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') | |
36 | .action((options) => installPluginCLI(options)) | |
37 | ||
b5f919ac C |
38 | program |
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') | |
46 | .action((options) => updatePluginCLI(options)) | |
47 | ||
8d2be0ed C |
48 | program |
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') | |
55 | .action(options => uninstallPluginCLI(options)) | |
56 | ||
57 | if (!process.argv.slice(2).length) { | |
58 | program.outputHelp() | |
59 | } | |
60 | ||
61 | program.parse(process.argv) | |
62 | ||
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | async function pluginsListCLI () { | |
66 | const { url, username, password } = await getServerCredentials(program) | |
67 | const accessToken = await getAdminTokenOrDie(url, username, password) | |
68 | ||
09071200 C |
69 | let pluginType: PluginType |
70 | if (program['onlyThemes']) pluginType = PluginType.THEME | |
71 | if (program['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 | ||
104 | async function installPluginCLI (options: any) { | |
105 | if (!options['path'] && !options['npmName']) { | |
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 | ||
111 | if (options['path'] && !isAbsolute(options['path'])) { | |
112 | console.error('Path should be absolute.') | |
113 | process.exit(-1) | |
114 | } | |
115 | ||
116 | const { url, username, password } = await getServerCredentials(options) | |
117 | const accessToken = await getAdminTokenOrDie(url, username, password) | |
118 | ||
119 | try { | |
120 | await installPlugin({ | |
121 | url, | |
122 | accessToken, | |
123 | npmName: options['npmName'], | |
124 | path: options['path'] | |
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 | ||
b5f919ac C |
135 | async function updatePluginCLI (options: any) { |
136 | if (!options['path'] && !options['npmName']) { | |
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 | ||
142 | if (options['path'] && !isAbsolute(options['path'])) { | |
143 | console.error('Path should be absolute.') | |
144 | process.exit(-1) | |
145 | } | |
146 | ||
147 | const { url, username, password } = await getServerCredentials(options) | |
148 | const accessToken = await getAdminTokenOrDie(url, username, password) | |
149 | ||
150 | try { | |
151 | await updatePlugin({ | |
152 | url, | |
153 | accessToken, | |
154 | npmName: options['npmName'], | |
155 | path: options['path'] | |
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 | ||
8d2be0ed C |
166 | async function uninstallPluginCLI (options: any) { |
167 | if (!options['npmName']) { | |
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 | ||
173 | const { url, username, password } = await getServerCredentials(options) | |
174 | const accessToken = await getAdminTokenOrDie(url, username, password) | |
175 | ||
176 | try { | |
177 | await uninstallPlugin({ | |
178 | url, | |
179 | accessToken, | |
a1587156 | 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 | } |