|
@@ -1,7 +1,7 @@
|
|
|
import { PluginModel } from '../../models/server/plugin'
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
import { RegisterHookOptions } from '../../../shared/models/plugins/register.model'
|
|
|
-import { join } from 'path'
|
|
|
+import { basename, join } from 'path'
|
|
|
import { CONFIG } from '../../initializers/config'
|
|
|
import { isLibraryCodeValid, isPackageJSONValid } from '../../helpers/custom-validators/plugins'
|
|
|
import { PluginPackageJson } from '../../../shared/models/plugins/plugin-package-json.model'
|
|
@@ -9,6 +9,7 @@ import { PluginLibrary } from '../../../shared/models/plugins/plugin-library.mod
|
|
|
import { createReadStream, createWriteStream } from 'fs'
|
|
|
import { PLUGIN_GLOBAL_CSS_PATH } from '../../initializers/constants'
|
|
|
import { PluginType } from '../../../shared/models/plugins/plugin.type'
|
|
|
+import { installNpmPlugin, installNpmPluginFromDisk, removeNpmPlugin } from './yarn'
|
|
|
|
|
|
export interface RegisteredPlugin {
|
|
|
name: string
|
|
@@ -84,11 +85,63 @@ export class PluginManager {
|
|
|
await plugin.unregister()
|
|
|
}
|
|
|
|
|
|
+ async install (toInstall: string, version: string, fromDisk = false) {
|
|
|
+ let plugin: PluginModel
|
|
|
+ let name: string
|
|
|
+
|
|
|
+ logger.info('Installing plugin %s.', toInstall)
|
|
|
+
|
|
|
+ try {
|
|
|
+ fromDisk
|
|
|
+ ? await installNpmPluginFromDisk(toInstall)
|
|
|
+ : await installNpmPlugin(toInstall, version)
|
|
|
+
|
|
|
+ name = fromDisk ? basename(toInstall) : toInstall
|
|
|
+ const pluginType = name.startsWith('peertube-theme-') ? PluginType.THEME : PluginType.PLUGIN
|
|
|
+ const pluginName = this.normalizePluginName(name)
|
|
|
+
|
|
|
+ const packageJSON = this.getPackageJSON(pluginName, pluginType)
|
|
|
+ if (!isPackageJSONValid(packageJSON, pluginType)) {
|
|
|
+ throw new Error('PackageJSON is invalid.')
|
|
|
+ }
|
|
|
+
|
|
|
+ [ plugin ] = await PluginModel.upsert({
|
|
|
+ name: pluginName,
|
|
|
+ description: packageJSON.description,
|
|
|
+ type: pluginType,
|
|
|
+ version: packageJSON.version,
|
|
|
+ enabled: true,
|
|
|
+ uninstalled: false,
|
|
|
+ peertubeEngine: packageJSON.engine.peertube
|
|
|
+ }, { returning: true })
|
|
|
+ } catch (err) {
|
|
|
+ logger.error('Cannot install plugin %s, removing it...', toInstall, { err })
|
|
|
+
|
|
|
+ try {
|
|
|
+ await removeNpmPlugin(name)
|
|
|
+ } catch (err) {
|
|
|
+ logger.error('Cannot remove plugin %s after failed installation.', toInstall, { err })
|
|
|
+ }
|
|
|
+
|
|
|
+ throw err
|
|
|
+ }
|
|
|
+
|
|
|
+ logger.info('Successful installation of plugin %s.', toInstall)
|
|
|
+
|
|
|
+ await this.registerPluginOrTheme(plugin)
|
|
|
+ }
|
|
|
+
|
|
|
+ async uninstall (packageName: string) {
|
|
|
+ await PluginModel.uninstall(this.normalizePluginName(packageName))
|
|
|
+
|
|
|
+ await removeNpmPlugin(packageName)
|
|
|
+ }
|
|
|
+
|
|
|
private async registerPluginOrTheme (plugin: PluginModel) {
|
|
|
logger.info('Registering plugin or theme %s.', plugin.name)
|
|
|
|
|
|
- const pluginPath = join(CONFIG.STORAGE.PLUGINS_DIR, plugin.name, plugin.version)
|
|
|
- const packageJSON: PluginPackageJson = require(join(pluginPath, 'package.json'))
|
|
|
+ const packageJSON = this.getPackageJSON(plugin.name, plugin.type)
|
|
|
+ const pluginPath = this.getPluginPath(plugin.name, plugin.type)
|
|
|
|
|
|
if (!isPackageJSONValid(packageJSON, plugin.type)) {
|
|
|
throw new Error('Package.JSON is invalid.')
|
|
@@ -124,6 +177,7 @@ export class PluginManager {
|
|
|
}
|
|
|
|
|
|
const library: PluginLibrary = require(join(pluginPath, packageJSON.library))
|
|
|
+
|
|
|
if (!isLibraryCodeValid(library)) {
|
|
|
throw new Error('Library code is not valid (miss register or unregister function)')
|
|
|
}
|
|
@@ -163,6 +217,22 @@ export class PluginManager {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
+ private getPackageJSON (pluginName: string, pluginType: PluginType) {
|
|
|
+ const pluginPath = join(this.getPluginPath(pluginName, pluginType), 'package.json')
|
|
|
+
|
|
|
+ return require(pluginPath) as PluginPackageJson
|
|
|
+ }
|
|
|
+
|
|
|
+ private getPluginPath (pluginName: string, pluginType: PluginType) {
|
|
|
+ const prefix = pluginType === PluginType.PLUGIN ? 'peertube-plugin-' : 'peertube-theme-'
|
|
|
+
|
|
|
+ return join(CONFIG.STORAGE.PLUGINS_DIR, 'node_modules', prefix + pluginName)
|
|
|
+ }
|
|
|
+
|
|
|
+ private normalizePluginName (name: string) {
|
|
|
+ return name.replace(/^peertube-((theme)|(plugin))-/, '')
|
|
|
+ }
|
|
|
+
|
|
|
static get Instance () {
|
|
|
return this.instance || (this.instance = new this())
|
|
|
}
|