]>
Commit | Line | Data |
---|---|---|
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 { installPlugin, listPlugins, uninstallPlugin, updatePlugin } from '../../shared/extra-utils/server/plugins' | |
8 | import { getAdminTokenOrDie, getServerCredentials } from './cli' | |
9 | import { PeerTubePlugin, PluginType } from '../../shared/models' | |
10 | import { isAbsolute } from 'path' | |
11 | import * as CliTable3 from 'cli-table3' | |
12 | ||
13 | program | |
14 | .name('plugins') | |
15 | .usage('[command] [options]') | |
16 | ||
17 | program | |
18 | .command('list') | |
19 | .description('List installed plugins') | |
20 | .option('-u, --url <url>', 'Server url') | |
21 | .option('-U, --username <username>', 'Username') | |
22 | .option('-p, --password <token>', 'Password') | |
23 | .option('-t, --only-themes', 'List themes only') | |
24 | .option('-P, --only-plugins', 'List plugins only') | |
25 | .action((options, command) => pluginsListCLI(command, options)) | |
26 | ||
27 | program | |
28 | .command('install') | |
29 | .description('Install a plugin or a theme') | |
30 | .option('-u, --url <url>', 'Server url') | |
31 | .option('-U, --username <username>', 'Username') | |
32 | .option('-p, --password <token>', 'Password') | |
33 | .option('-P --path <path>', 'Install from a path') | |
34 | .option('-n, --npm-name <npmName>', 'Install 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 accessToken = await getAdminTokenOrDie(url, username, password) | |
67 | ||
68 | let pluginType: PluginType | |
69 | if (options.onlyThemes) pluginType = PluginType.THEME | |
70 | if (options.onlyPlugins) pluginType = PluginType.PLUGIN | |
71 | ||
72 | const res = await listPlugins({ | |
73 | url, | |
74 | accessToken, | |
75 | start: 0, | |
76 | count: 100, | |
77 | sort: 'name', | |
78 | pluginType | |
79 | }) | |
80 | const plugins: PeerTubePlugin[] = res.body.data | |
81 | ||
82 | const table = new CliTable3({ | |
83 | head: [ 'name', 'version', 'homepage' ], | |
84 | colWidths: [ 50, 10, 50 ] | |
85 | }) as any | |
86 | ||
87 | for (const plugin of plugins) { | |
88 | const npmName = plugin.type === PluginType.PLUGIN | |
89 | ? 'peertube-plugin-' + plugin.name | |
90 | : 'peertube-theme-' + plugin.name | |
91 | ||
92 | table.push([ | |
93 | npmName, | |
94 | plugin.version, | |
95 | plugin.homepage | |
96 | ]) | |
97 | } | |
98 | ||
99 | console.log(table.toString()) | |
100 | process.exit(0) | |
101 | } | |
102 | ||
103 | async function installPluginCLI (command: Command, options: OptionValues) { | |
104 | if (!options.path && !options.npmName) { | |
105 | console.error('You need to specify the npm name or the path of the plugin you want to install.\n') | |
106 | program.outputHelp() | |
107 | process.exit(-1) | |
108 | } | |
109 | ||
110 | if (options.path && !isAbsolute(options.path)) { | |
111 | console.error('Path should be absolute.') | |
112 | process.exit(-1) | |
113 | } | |
114 | ||
115 | const { url, username, password } = await getServerCredentials(command) | |
116 | const accessToken = await getAdminTokenOrDie(url, username, password) | |
117 | ||
118 | try { | |
119 | await installPlugin({ | |
120 | url, | |
121 | accessToken, | |
122 | npmName: options.npmName, | |
123 | path: options.path | |
124 | }) | |
125 | } catch (err) { | |
126 | console.error('Cannot install plugin.', err) | |
127 | process.exit(-1) | |
128 | } | |
129 | ||
130 | console.log('Plugin installed.') | |
131 | process.exit(0) | |
132 | } | |
133 | ||
134 | async function updatePluginCLI (command: Command, options: OptionValues) { | |
135 | if (!options.path && !options.npmName) { | |
136 | console.error('You need to specify the npm name or the path of the plugin you want to update.\n') | |
137 | program.outputHelp() | |
138 | process.exit(-1) | |
139 | } | |
140 | ||
141 | if (options.path && !isAbsolute(options.path)) { | |
142 | console.error('Path should be absolute.') | |
143 | process.exit(-1) | |
144 | } | |
145 | ||
146 | const { url, username, password } = await getServerCredentials(command) | |
147 | const accessToken = await getAdminTokenOrDie(url, username, password) | |
148 | ||
149 | try { | |
150 | await updatePlugin({ | |
151 | url, | |
152 | accessToken, | |
153 | npmName: options.npmName, | |
154 | path: options.path | |
155 | }) | |
156 | } catch (err) { | |
157 | console.error('Cannot update plugin.', err) | |
158 | process.exit(-1) | |
159 | } | |
160 | ||
161 | console.log('Plugin updated.') | |
162 | process.exit(0) | |
163 | } | |
164 | ||
165 | async function uninstallPluginCLI (command: Command, options: OptionValues) { | |
166 | if (!options.npmName) { | |
167 | console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n') | |
168 | program.outputHelp() | |
169 | process.exit(-1) | |
170 | } | |
171 | ||
172 | const { url, username, password } = await getServerCredentials(command) | |
173 | const accessToken = await getAdminTokenOrDie(url, username, password) | |
174 | ||
175 | try { | |
176 | await uninstallPlugin({ | |
177 | url, | |
178 | accessToken, | |
179 | npmName: options.npmName | |
180 | }) | |
181 | } catch (err) { | |
182 | console.error('Cannot uninstall plugin.', err) | |
183 | process.exit(-1) | |
184 | } | |
185 | ||
186 | console.log('Plugin uninstalled.') | |
187 | process.exit(0) | |
188 | } |