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