mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-04 10:19:35 +02:00
WIP plugins: install/uninstall
This commit is contained in:
parent
345da516fa
commit
f023a19c3e
11 changed files with 216 additions and 9 deletions
61
server/lib/plugins/yarn.ts
Normal file
61
server/lib/plugins/yarn.ts
Normal file
|
@ -0,0 +1,61 @@
|
|||
import { execShell } from '../../helpers/core-utils'
|
||||
import { logger } from '../../helpers/logger'
|
||||
import { isNpmPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
|
||||
import { CONFIG } from '../../initializers/config'
|
||||
import { outputJSON, pathExists } from 'fs-extra'
|
||||
import { join } from 'path'
|
||||
|
||||
async function installNpmPlugin (name: string, version: string) {
|
||||
// Security check
|
||||
checkNpmPluginNameOrThrow(name)
|
||||
checkPluginVersionOrThrow(version)
|
||||
|
||||
const toInstall = `${name}@${version}`
|
||||
await execYarn('add ' + toInstall)
|
||||
}
|
||||
|
||||
async function installNpmPluginFromDisk (path: string) {
|
||||
await execYarn('add file:' + path)
|
||||
}
|
||||
|
||||
async function removeNpmPlugin (name: string) {
|
||||
checkNpmPluginNameOrThrow(name)
|
||||
|
||||
await execYarn('remove ' + name)
|
||||
}
|
||||
|
||||
// ############################################################################
|
||||
|
||||
export {
|
||||
installNpmPlugin,
|
||||
installNpmPluginFromDisk,
|
||||
removeNpmPlugin
|
||||
}
|
||||
|
||||
// ############################################################################
|
||||
|
||||
async function execYarn (command: string) {
|
||||
try {
|
||||
const pluginDirectory = CONFIG.STORAGE.PLUGINS_DIR
|
||||
const pluginPackageJSON = join(pluginDirectory, 'package.json')
|
||||
|
||||
// Create empty package.json file if needed
|
||||
if (!await pathExists(pluginPackageJSON)) {
|
||||
await outputJSON(pluginPackageJSON, {})
|
||||
}
|
||||
|
||||
await execShell(`yarn ${command}`, { cwd: pluginDirectory })
|
||||
} catch (result) {
|
||||
logger.error('Cannot exec yarn.', { command, err: result.err, stderr: result.stderr })
|
||||
|
||||
throw result.err
|
||||
}
|
||||
}
|
||||
|
||||
function checkNpmPluginNameOrThrow (name: string) {
|
||||
if (!isNpmPluginNameValid(name)) throw new Error('Invalid NPM plugin name to install')
|
||||
}
|
||||
|
||||
function checkPluginVersionOrThrow (name: string) {
|
||||
if (!isPluginVersionValid(name)) throw new Error('Invalid NPM plugin version to install')
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue