]>
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 | ||
12152aa0 | 6 | import { program, Command, OptionValues } from 'commander' |
d0a0fa42 | 7 | import { assignToken, buildServer, getServerCredentials } from './cli' |
078f17e6 | 8 | import { PluginType } from '../../shared/models' |
8d2be0ed | 9 | import { isAbsolute } from 'path' |
41fb13c3 | 10 | import CliTable3 from 'cli-table3' |
8d2be0ed C |
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') | |
1e0741d1 | 24 | .action((options, command) => pluginsListCLI(command, options)) |
8d2be0ed C |
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') | |
ba5a8d89 | 34 | .action((options, command) => installPluginCLI(command, options)) |
8d2be0ed | 35 | |
b5f919ac C |
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') | |
ba5a8d89 | 44 | .action((options, command) => updatePluginCLI(command, options)) |
b5f919ac | 45 | |
8d2be0ed C |
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') | |
ba5a8d89 | 53 | .action((options, command) => uninstallPluginCLI(command, options)) |
8d2be0ed C |
54 | |
55 | if (!process.argv.slice(2).length) { | |
56 | program.outputHelp() | |
57 | } | |
58 | ||
59 | program.parse(process.argv) | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | ||
12152aa0 | 63 | async function pluginsListCLI (command: Command, options: OptionValues) { |
1e0741d1 | 64 | const { url, username, password } = await getServerCredentials(command) |
d0a0fa42 C |
65 | const server = buildServer(url) |
66 | await assignToken(server, username, password) | |
8d2be0ed | 67 | |
09071200 | 68 | let pluginType: PluginType |
ba5a8d89 C |
69 | if (options.onlyThemes) pluginType = PluginType.THEME |
70 | if (options.onlyPlugins) pluginType = PluginType.PLUGIN | |
8d2be0ed | 71 | |
89d241a7 | 72 | const { data } = await server.plugins.list({ start: 0, count: 100, sort: 'name', pluginType }) |
8d2be0ed | 73 | |
26fcf2ef | 74 | const table = new CliTable3({ |
a1587156 | 75 | head: [ 'name', 'version', 'homepage' ], |
8d2be0ed | 76 | colWidths: [ 50, 10, 50 ] |
a1587156 | 77 | }) as any |
8d2be0ed | 78 | |
078f17e6 | 79 | for (const plugin of data) { |
8d2be0ed C |
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 | ||
12152aa0 | 95 | async function installPluginCLI (command: Command, options: OptionValues) { |
ba5a8d89 | 96 | if (!options.path && !options.npmName) { |
8d2be0ed C |
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 | ||
ba5a8d89 | 102 | if (options.path && !isAbsolute(options.path)) { |
8d2be0ed C |
103 | console.error('Path should be absolute.') |
104 | process.exit(-1) | |
105 | } | |
106 | ||
ba5a8d89 | 107 | const { url, username, password } = await getServerCredentials(command) |
d0a0fa42 C |
108 | const server = buildServer(url) |
109 | await assignToken(server, username, password) | |
8d2be0ed C |
110 | |
111 | try { | |
89d241a7 | 112 | await server.plugins.install({ npmName: options.npmName, path: options.path }) |
8d2be0ed C |
113 | } catch (err) { |
114 | console.error('Cannot install plugin.', err) | |
115 | process.exit(-1) | |
8d2be0ed C |
116 | } |
117 | ||
118 | console.log('Plugin installed.') | |
119 | process.exit(0) | |
120 | } | |
121 | ||
12152aa0 | 122 | async function updatePluginCLI (command: Command, options: OptionValues) { |
ba5a8d89 | 123 | if (!options.path && !options.npmName) { |
b5f919ac C |
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 | ||
ba5a8d89 | 129 | if (options.path && !isAbsolute(options.path)) { |
b5f919ac C |
130 | console.error('Path should be absolute.') |
131 | process.exit(-1) | |
132 | } | |
133 | ||
ba5a8d89 | 134 | const { url, username, password } = await getServerCredentials(command) |
d0a0fa42 C |
135 | const server = buildServer(url) |
136 | await assignToken(server, username, password) | |
b5f919ac C |
137 | |
138 | try { | |
89d241a7 | 139 | await server.plugins.update({ npmName: options.npmName, path: options.path }) |
b5f919ac C |
140 | } catch (err) { |
141 | console.error('Cannot update plugin.', err) | |
142 | process.exit(-1) | |
b5f919ac C |
143 | } |
144 | ||
145 | console.log('Plugin updated.') | |
146 | process.exit(0) | |
147 | } | |
148 | ||
12152aa0 | 149 | async function uninstallPluginCLI (command: Command, options: OptionValues) { |
ba5a8d89 | 150 | if (!options.npmName) { |
8d2be0ed C |
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 | ||
ba5a8d89 | 156 | const { url, username, password } = await getServerCredentials(command) |
d0a0fa42 C |
157 | const server = buildServer(url) |
158 | await assignToken(server, username, password) | |
8d2be0ed C |
159 | |
160 | try { | |
89d241a7 | 161 | await server.plugins.uninstall({ npmName: options.npmName }) |
8d2be0ed C |
162 | } catch (err) { |
163 | console.error('Cannot uninstall plugin.', err) | |
164 | process.exit(-1) | |
8d2be0ed C |
165 | } |
166 | ||
167 | console.log('Plugin uninstalled.') | |
168 | process.exit(0) | |
169 | } |