diff options
Diffstat (limited to 'apps/peertube-cli/src/peertube-plugins.ts')
-rw-r--r-- | apps/peertube-cli/src/peertube-plugins.ts | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/apps/peertube-cli/src/peertube-plugins.ts b/apps/peertube-cli/src/peertube-plugins.ts new file mode 100644 index 000000000..c9da56266 --- /dev/null +++ b/apps/peertube-cli/src/peertube-plugins.ts | |||
@@ -0,0 +1,167 @@ | |||
1 | import CliTable3 from 'cli-table3' | ||
2 | import { isAbsolute } from 'path' | ||
3 | import { Command } from '@commander-js/extra-typings' | ||
4 | import { PluginType, PluginType_Type } from '@peertube/peertube-models' | ||
5 | import { assignToken, buildServer, CommonProgramOptions, getServerCredentials } from './shared/index.js' | ||
6 | |||
7 | export function definePluginsProgram () { | ||
8 | const program = new Command() | ||
9 | |||
10 | program | ||
11 | .name('plugins') | ||
12 | .description('Manage instance plugins/themes') | ||
13 | .alias('p') | ||
14 | |||
15 | program | ||
16 | .command('list') | ||
17 | .description('List installed plugins') | ||
18 | .option('-u, --url <url>', 'Server url') | ||
19 | .option('-U, --username <username>', 'Username') | ||
20 | .option('-p, --password <token>', 'Password') | ||
21 | .option('-t, --only-themes', 'List themes only') | ||
22 | .option('-P, --only-plugins', 'List plugins only') | ||
23 | .action(async options => { | ||
24 | try { | ||
25 | await pluginsListCLI(options) | ||
26 | } catch (err) { | ||
27 | console.error('Cannot list plugins: ' + err.message) | ||
28 | process.exit(-1) | ||
29 | } | ||
30 | }) | ||
31 | |||
32 | program | ||
33 | .command('install') | ||
34 | .description('Install 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>', 'Install from a path') | ||
39 | .option('-n, --npm-name <npmName>', 'Install from npm') | ||
40 | .option('--plugin-version <pluginVersion>', 'Specify the plugin version to install (only available when installing from npm)') | ||
41 | .action(async options => { | ||
42 | try { | ||
43 | await installPluginCLI(options) | ||
44 | } catch (err) { | ||
45 | console.error('Cannot install plugin: ' + err.message) | ||
46 | process.exit(-1) | ||
47 | } | ||
48 | }) | ||
49 | |||
50 | program | ||
51 | .command('update') | ||
52 | .description('Update a plugin or a theme') | ||
53 | .option('-u, --url <url>', 'Server url') | ||
54 | .option('-U, --username <username>', 'Username') | ||
55 | .option('-p, --password <token>', 'Password') | ||
56 | .option('-P --path <path>', 'Update from a path') | ||
57 | .option('-n, --npm-name <npmName>', 'Update from npm') | ||
58 | .action(async options => { | ||
59 | try { | ||
60 | await updatePluginCLI(options) | ||
61 | } catch (err) { | ||
62 | console.error('Cannot update plugin: ' + err.message) | ||
63 | process.exit(-1) | ||
64 | } | ||
65 | }) | ||
66 | |||
67 | program | ||
68 | .command('uninstall') | ||
69 | .description('Uninstall a plugin or a theme') | ||
70 | .option('-u, --url <url>', 'Server url') | ||
71 | .option('-U, --username <username>', 'Username') | ||
72 | .option('-p, --password <token>', 'Password') | ||
73 | .option('-n, --npm-name <npmName>', 'NPM plugin/theme name') | ||
74 | .action(async options => { | ||
75 | try { | ||
76 | await uninstallPluginCLI(options) | ||
77 | } catch (err) { | ||
78 | console.error('Cannot uninstall plugin: ' + err.message) | ||
79 | process.exit(-1) | ||
80 | } | ||
81 | }) | ||
82 | |||
83 | return program | ||
84 | } | ||
85 | |||
86 | // ---------------------------------------------------------------------------- | ||
87 | |||
88 | async function pluginsListCLI (options: CommonProgramOptions & { onlyThemes?: true, onlyPlugins?: true }) { | ||
89 | const { url, username, password } = await getServerCredentials(options) | ||
90 | const server = buildServer(url) | ||
91 | await assignToken(server, username, password) | ||
92 | |||
93 | let pluginType: PluginType_Type | ||
94 | if (options.onlyThemes) pluginType = PluginType.THEME | ||
95 | if (options.onlyPlugins) pluginType = PluginType.PLUGIN | ||
96 | |||
97 | const { data } = await server.plugins.list({ start: 0, count: 100, sort: 'name', pluginType }) | ||
98 | |||
99 | const table = new CliTable3({ | ||
100 | head: [ 'name', 'version', 'homepage' ], | ||
101 | colWidths: [ 50, 20, 50 ] | ||
102 | }) as any | ||
103 | |||
104 | for (const plugin of data) { | ||
105 | const npmName = plugin.type === PluginType.PLUGIN | ||
106 | ? 'peertube-plugin-' + plugin.name | ||
107 | : 'peertube-theme-' + plugin.name | ||
108 | |||
109 | table.push([ | ||
110 | npmName, | ||
111 | plugin.version, | ||
112 | plugin.homepage | ||
113 | ]) | ||
114 | } | ||
115 | |||
116 | console.log(table.toString()) | ||
117 | } | ||
118 | |||
119 | async function installPluginCLI (options: CommonProgramOptions & { path?: string, npmName?: string, pluginVersion?: string }) { | ||
120 | if (!options.path && !options.npmName) { | ||
121 | throw new Error('You need to specify the npm name or the path of the plugin you want to install.') | ||
122 | } | ||
123 | |||
124 | if (options.path && !isAbsolute(options.path)) { | ||
125 | throw new Error('Path should be absolute.') | ||
126 | } | ||
127 | |||
128 | const { url, username, password } = await getServerCredentials(options) | ||
129 | const server = buildServer(url) | ||
130 | await assignToken(server, username, password) | ||
131 | |||
132 | await server.plugins.install({ npmName: options.npmName, path: options.path, pluginVersion: options.pluginVersion }) | ||
133 | |||
134 | console.log('Plugin installed.') | ||
135 | } | ||
136 | |||
137 | async function updatePluginCLI (options: CommonProgramOptions & { path?: string, npmName?: string }) { | ||
138 | if (!options.path && !options.npmName) { | ||
139 | throw new Error('You need to specify the npm name or the path of the plugin you want to update.') | ||
140 | } | ||
141 | |||
142 | if (options.path && !isAbsolute(options.path)) { | ||
143 | throw new Error('Path should be absolute.') | ||
144 | } | ||
145 | |||
146 | const { url, username, password } = await getServerCredentials(options) | ||
147 | const server = buildServer(url) | ||
148 | await assignToken(server, username, password) | ||
149 | |||
150 | await server.plugins.update({ npmName: options.npmName, path: options.path }) | ||
151 | |||
152 | console.log('Plugin updated.') | ||
153 | } | ||
154 | |||
155 | async function uninstallPluginCLI (options: CommonProgramOptions & { npmName?: string }) { | ||
156 | if (!options.npmName) { | ||
157 | throw new Error('You need to specify the npm name of the plugin/theme you want to uninstall.') | ||
158 | } | ||
159 | |||
160 | const { url, username, password } = await getServerCredentials(options) | ||
161 | const server = buildServer(url) | ||
162 | await assignToken(server, username, password) | ||
163 | |||
164 | await server.plugins.uninstall({ npmName: options.npmName }) | ||
165 | |||
166 | console.log('Plugin uninstalled.') | ||
167 | } | ||