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