]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/plugins/yarn.ts
Add data directory for plugins and some helpers
[github/Chocobozzz/PeerTube.git] / server / lib / plugins / yarn.ts
index 35fe1625f4808001f628456759f05d6c8225d751..3f45681d3f90ca839d575616681a4680b164ca23 100644 (file)
@@ -1,17 +1,24 @@
+import { outputJSON, pathExists } from 'fs-extra'
+import { join } from 'path'
 import { execShell } from '../../helpers/core-utils'
-import { logger } from '../../helpers/logger'
 import { isNpmPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
+import { logger } from '../../helpers/logger'
 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)
-  checkPluginVersionOrThrow(version)
+  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)
 
-  const toInstall = `${name}@${version}`
-  await execYarn('add ' + toInstall)
+  logger.debug('Added a yarn package.', { yarnStdout: stdout })
 }
 
 async function installNpmPluginFromDisk (path: string) {
@@ -44,7 +51,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 })