]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/plugins/yarn.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / yarn.ts
CommitLineData
8280d0c2
C
1import { outputJSON, pathExists } from 'fs-extra'
2import { join } from 'path'
f023a19c 3import { execShell } from '../../helpers/core-utils'
ff91b644 4import { isNpmPluginNameValid, isPluginStableOrUnstableVersionValid } from '../../helpers/custom-validators/plugins'
8280d0c2 5import { logger } from '../../helpers/logger'
f023a19c 6import { CONFIG } from '../../initializers/config'
8280d0c2 7import { getLatestPluginVersion } from './plugin-index'
f023a19c 8
8280d0c2 9async function installNpmPlugin (npmName: string, versionArg?: string) {
f023a19c 10 // Security check
9b474844 11 checkNpmPluginNameOrThrow(npmName)
8280d0c2
C
12 if (versionArg) checkPluginVersionOrThrow(versionArg)
13
14 const version = versionArg || await getLatestPluginVersion(npmName)
ad91e700 15
9b474844 16 let toInstall = npmName
ad91e700 17 if (version) toInstall += `@${version}`
f023a19c 18
09071200
C
19 const { stdout } = await execYarn('add ' + toInstall)
20
21 logger.debug('Added a yarn package.', { yarnStdout: stdout })
f023a19c
C
22}
23
24async function installNpmPluginFromDisk (path: string) {
25 await execYarn('add file:' + path)
26}
27
28async function removeNpmPlugin (name: string) {
29 checkNpmPluginNameOrThrow(name)
30
31 await execYarn('remove ' + name)
32}
33
c795e196
C
34async function rebuildNativePlugins () {
35 await execYarn('install --pure-lockfile')
36}
37
f023a19c
C
38// ############################################################################
39
40export {
41 installNpmPlugin,
42 installNpmPluginFromDisk,
c795e196 43 rebuildNativePlugins,
f023a19c
C
44 removeNpmPlugin
45}
46
47// ############################################################################
48
49async function execYarn (command: string) {
50 try {
51 const pluginDirectory = CONFIG.STORAGE.PLUGINS_DIR
52 const pluginPackageJSON = join(pluginDirectory, 'package.json')
53
54 // Create empty package.json file if needed
55 if (!await pathExists(pluginPackageJSON)) {
56 await outputJSON(pluginPackageJSON, {})
57 }
58
09071200 59 return execShell(`yarn ${command}`, { cwd: pluginDirectory })
f023a19c
C
60 } catch (result) {
61 logger.error('Cannot exec yarn.', { command, err: result.err, stderr: result.stderr })
62
63 throw result.err
64 }
65}
66
67function checkNpmPluginNameOrThrow (name: string) {
68 if (!isNpmPluginNameValid(name)) throw new Error('Invalid NPM plugin name to install')
69}
70
71function checkPluginVersionOrThrow (name: string) {
ff91b644 72 if (!isPluginStableOrUnstableVersionValid(name)) throw new Error('Invalid NPM plugin version to install')
f023a19c 73}