]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/plugins/yarn.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / yarn.ts
index 5fe1c50463d0f30aafd9d567f7a186a9896a6a1b..9cf6ec9e90e53be6f281c312130f1ab7654109bb 100644 (file)
@@ -1,19 +1,24 @@
+import { outputJSON, pathExists } from 'fs-extra'
+import { join } from 'path'
 import { execShell } from '../../helpers/core-utils'
+import { isNpmPluginNameValid, isPluginStableOrUnstableVersionValid } from '../../helpers/custom-validators/plugins'
 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'
+import { getLatestPluginVersion } from './plugin-index'
 
-async function installNpmPlugin (name: string, version?: string) {
+async function installNpmPlugin (npmName: string, versionArg?: string) {
   // Security check
-  checkNpmPluginNameOrThrow(name)
-  if (version) checkPluginVersionOrThrow(version)
+  checkNpmPluginNameOrThrow(npmName)
+  if (versionArg) checkPluginVersionOrThrow(versionArg)
 
-  let toInstall = name
+  const version = versionArg || await getLatestPluginVersion(npmName)
+
+  let toInstall = npmName
   if (version) toInstall += `@${version}`
 
-  await execYarn('add ' + toInstall)
+  const { stdout } = await execYarn('add ' + toInstall)
+
+  logger.debug('Added a yarn package.', { yarnStdout: stdout })
 }
 
 async function installNpmPluginFromDisk (path: string) {
@@ -26,11 +31,16 @@ async function removeNpmPlugin (name: string) {
   await execYarn('remove ' + name)
 }
 
+async function rebuildNativePlugins () {
+  await execYarn('install --pure-lockfile')
+}
+
 // ############################################################################
 
 export {
   installNpmPlugin,
   installNpmPluginFromDisk,
+  rebuildNativePlugins,
   removeNpmPlugin
 }
 
@@ -46,7 +56,7 @@ async function execYarn (command: string) {
       await outputJSON(pluginPackageJSON, {})
     }
 
-    await execShell(`yarn ${command}`, { cwd: pluginDirectory })
+    return execShell(`yarn ${command}`, { cwd: pluginDirectory })
   } catch (result) {
     logger.error('Cannot exec yarn.', { command, err: result.err, stderr: result.stderr })
 
@@ -59,5 +69,5 @@ function checkNpmPluginNameOrThrow (name: string) {
 }
 
 function checkPluginVersionOrThrow (name: string) {
-  if (!isPluginVersionValid(name)) throw new Error('Invalid NPM plugin version to install')
+  if (!isPluginStableOrUnstableVersionValid(name)) throw new Error('Invalid NPM plugin version to install')
 }