]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import CliTable3 from 'cli-table3' |
f8360396 C |
2 | import { Command, OptionValues, program } from 'commander' |
3 | import { isAbsolute } from 'path' | |
4 | import { PluginType } from '../../shared/models' | |
5 | import { assignToken, buildServer, getServerCredentials } from './cli' | |
8d2be0ed C |
6 | |
7 | program | |
8 | .name('plugins') | |
9 | .usage('[command] [options]') | |
10 | ||
11 | program | |
12 | .command('list') | |
13 | .description('List installed plugins') | |
14 | .option('-u, --url <url>', 'Server url') | |
15 | .option('-U, --username <username>', 'Username') | |
16 | .option('-p, --password <token>', 'Password') | |
17 | .option('-t, --only-themes', 'List themes only') | |
18 | .option('-P, --only-plugins', 'List plugins only') | |
1e0741d1 | 19 | .action((options, command) => pluginsListCLI(command, options)) |
8d2be0ed C |
20 | |
21 | program | |
22 | .command('install') | |
23 | .description('Install a plugin or a theme') | |
24 | .option('-u, --url <url>', 'Server url') | |
25 | .option('-U, --username <username>', 'Username') | |
26 | .option('-p, --password <token>', 'Password') | |
27 | .option('-P --path <path>', 'Install from a path') | |
28 | .option('-n, --npm-name <npmName>', 'Install from npm') | |
3a1157a6 | 29 | .option('--plugin-version <pluginVersion>', 'Specify the plugin version to install (only available when installing from npm)') |
ba5a8d89 | 30 | .action((options, command) => installPluginCLI(command, options)) |
8d2be0ed | 31 | |
b5f919ac C |
32 | program |
33 | .command('update') | |
34 | .description('Update a plugin or a theme') | |
35 | .option('-u, --url <url>', 'Server url') | |
36 | .option('-U, --username <username>', 'Username') | |
37 | .option('-p, --password <token>', 'Password') | |
38 | .option('-P --path <path>', 'Update from a path') | |
39 | .option('-n, --npm-name <npmName>', 'Update from npm') | |
ba5a8d89 | 40 | .action((options, command) => updatePluginCLI(command, options)) |
b5f919ac | 41 | |
8d2be0ed C |
42 | program |
43 | .command('uninstall') | |
44 | .description('Uninstall a plugin or a theme') | |
45 | .option('-u, --url <url>', 'Server url') | |
46 | .option('-U, --username <username>', 'Username') | |
47 | .option('-p, --password <token>', 'Password') | |
48 | .option('-n, --npm-name <npmName>', 'NPM plugin/theme name') | |
ba5a8d89 | 49 | .action((options, command) => uninstallPluginCLI(command, options)) |
8d2be0ed C |
50 | |
51 | if (!process.argv.slice(2).length) { | |
52 | program.outputHelp() | |
53 | } | |
54 | ||
55 | program.parse(process.argv) | |
56 | ||
57 | // ---------------------------------------------------------------------------- | |
58 | ||
12152aa0 | 59 | async function pluginsListCLI (command: Command, options: OptionValues) { |
1e0741d1 | 60 | const { url, username, password } = await getServerCredentials(command) |
d0a0fa42 C |
61 | const server = buildServer(url) |
62 | await assignToken(server, username, password) | |
8d2be0ed | 63 | |
09071200 | 64 | let pluginType: PluginType |
ba5a8d89 C |
65 | if (options.onlyThemes) pluginType = PluginType.THEME |
66 | if (options.onlyPlugins) pluginType = PluginType.PLUGIN | |
8d2be0ed | 67 | |
89d241a7 | 68 | const { data } = await server.plugins.list({ start: 0, count: 100, sort: 'name', pluginType }) |
8d2be0ed | 69 | |
26fcf2ef | 70 | const table = new CliTable3({ |
a1587156 | 71 | head: [ 'name', 'version', 'homepage' ], |
8d2be0ed | 72 | colWidths: [ 50, 10, 50 ] |
a1587156 | 73 | }) as any |
8d2be0ed | 74 | |
078f17e6 | 75 | for (const plugin of data) { |
8d2be0ed C |
76 | const npmName = plugin.type === PluginType.PLUGIN |
77 | ? 'peertube-plugin-' + plugin.name | |
78 | : 'peertube-theme-' + plugin.name | |
79 | ||
80 | table.push([ | |
81 | npmName, | |
82 | plugin.version, | |
83 | plugin.homepage | |
84 | ]) | |
85 | } | |
86 | ||
87 | console.log(table.toString()) | |
88 | process.exit(0) | |
89 | } | |
90 | ||
12152aa0 | 91 | async function installPluginCLI (command: Command, options: OptionValues) { |
ba5a8d89 | 92 | if (!options.path && !options.npmName) { |
8d2be0ed C |
93 | console.error('You need to specify the npm name or the path of the plugin you want to install.\n') |
94 | program.outputHelp() | |
95 | process.exit(-1) | |
96 | } | |
97 | ||
ba5a8d89 | 98 | if (options.path && !isAbsolute(options.path)) { |
8d2be0ed C |
99 | console.error('Path should be absolute.') |
100 | process.exit(-1) | |
101 | } | |
102 | ||
ba5a8d89 | 103 | const { url, username, password } = await getServerCredentials(command) |
d0a0fa42 C |
104 | const server = buildServer(url) |
105 | await assignToken(server, username, password) | |
8d2be0ed C |
106 | |
107 | try { | |
3a1157a6 | 108 | await server.plugins.install({ npmName: options.npmName, path: options.path, pluginVersion: options.pluginVersion }) |
8d2be0ed C |
109 | } catch (err) { |
110 | console.error('Cannot install plugin.', err) | |
111 | process.exit(-1) | |
8d2be0ed C |
112 | } |
113 | ||
114 | console.log('Plugin installed.') | |
115 | process.exit(0) | |
116 | } | |
117 | ||
12152aa0 | 118 | async function updatePluginCLI (command: Command, options: OptionValues) { |
ba5a8d89 | 119 | if (!options.path && !options.npmName) { |
b5f919ac C |
120 | console.error('You need to specify the npm name or the path of the plugin you want to update.\n') |
121 | program.outputHelp() | |
122 | process.exit(-1) | |
123 | } | |
124 | ||
ba5a8d89 | 125 | if (options.path && !isAbsolute(options.path)) { |
b5f919ac C |
126 | console.error('Path should be absolute.') |
127 | process.exit(-1) | |
128 | } | |
129 | ||
ba5a8d89 | 130 | const { url, username, password } = await getServerCredentials(command) |
d0a0fa42 C |
131 | const server = buildServer(url) |
132 | await assignToken(server, username, password) | |
b5f919ac C |
133 | |
134 | try { | |
89d241a7 | 135 | await server.plugins.update({ npmName: options.npmName, path: options.path }) |
b5f919ac C |
136 | } catch (err) { |
137 | console.error('Cannot update plugin.', err) | |
138 | process.exit(-1) | |
b5f919ac C |
139 | } |
140 | ||
141 | console.log('Plugin updated.') | |
142 | process.exit(0) | |
143 | } | |
144 | ||
12152aa0 | 145 | async function uninstallPluginCLI (command: Command, options: OptionValues) { |
ba5a8d89 | 146 | if (!options.npmName) { |
8d2be0ed C |
147 | console.error('You need to specify the npm name of the plugin/theme you want to uninstall.\n') |
148 | program.outputHelp() | |
149 | process.exit(-1) | |
150 | } | |
151 | ||
ba5a8d89 | 152 | const { url, username, password } = await getServerCredentials(command) |
d0a0fa42 C |
153 | const server = buildServer(url) |
154 | await assignToken(server, username, password) | |
8d2be0ed C |
155 | |
156 | try { | |
89d241a7 | 157 | await server.plugins.uninstall({ npmName: options.npmName }) |
8d2be0ed C |
158 | } catch (err) { |
159 | console.error('Cannot uninstall plugin.', err) | |
160 | process.exit(-1) | |
8d2be0ed C |
161 | } |
162 | ||
163 | console.log('Plugin uninstalled.') | |
164 | process.exit(0) | |
165 | } |