mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-04 02:09:37 +02:00
server/server -> server/core
This commit is contained in:
parent
114327d4ce
commit
5a3d0650c9
838 changed files with 111 additions and 111 deletions
73
server/core/lib/plugins/yarn.ts
Normal file
73
server/core/lib/plugins/yarn.ts
Normal file
|
@ -0,0 +1,73 @@
|
|||
import { outputJSON, pathExists } from 'fs-extra/esm'
|
||||
import { join } from 'path'
|
||||
import { execShell } from '../../helpers/core-utils.js'
|
||||
import { isNpmPluginNameValid, isPluginStableOrUnstableVersionValid } from '../../helpers/custom-validators/plugins.js'
|
||||
import { logger } from '../../helpers/logger.js'
|
||||
import { CONFIG } from '../../initializers/config.js'
|
||||
import { getLatestPluginVersion } from './plugin-index.js'
|
||||
|
||||
async function installNpmPlugin (npmName: string, versionArg?: string) {
|
||||
// Security check
|
||||
checkNpmPluginNameOrThrow(npmName)
|
||||
if (versionArg) checkPluginVersionOrThrow(versionArg)
|
||||
|
||||
const version = versionArg || await getLatestPluginVersion(npmName)
|
||||
|
||||
let toInstall = npmName
|
||||
if (version) toInstall += `@${version}`
|
||||
|
||||
const { stdout } = await execYarn('add ' + toInstall)
|
||||
|
||||
logger.debug('Added a yarn package.', { yarnStdout: stdout })
|
||||
}
|
||||
|
||||
async function installNpmPluginFromDisk (path: string) {
|
||||
await execYarn('add file:' + path)
|
||||
}
|
||||
|
||||
async function removeNpmPlugin (name: string) {
|
||||
checkNpmPluginNameOrThrow(name)
|
||||
|
||||
await execYarn('remove ' + name)
|
||||
}
|
||||
|
||||
async function rebuildNativePlugins () {
|
||||
await execYarn('install --pure-lockfile')
|
||||
}
|
||||
|
||||
// ############################################################################
|
||||
|
||||
export {
|
||||
installNpmPlugin,
|
||||
installNpmPluginFromDisk,
|
||||
rebuildNativePlugins,
|
||||
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, {})
|
||||
}
|
||||
|
||||
return 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 (!isPluginStableOrUnstableVersionValid(name)) throw new Error('Invalid NPM plugin version to install')
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue