aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/plugins/yarn.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-07-05 15:28:49 +0200
committerChocobozzz <chocobozzz@cpy.re>2019-07-24 10:58:16 +0200
commitf023a19c3eeeea2b014b47fae522a62eab320048 (patch)
tree988ff97432663db928f1e3e3f498da856e739de1 /server/lib/plugins/yarn.ts
parent345da516fae80f24c90c2196e96393b489af2243 (diff)
downloadPeerTube-f023a19c3eeeea2b014b47fae522a62eab320048.tar.gz
PeerTube-f023a19c3eeeea2b014b47fae522a62eab320048.tar.zst
PeerTube-f023a19c3eeeea2b014b47fae522a62eab320048.zip
WIP plugins: install/uninstall
Diffstat (limited to 'server/lib/plugins/yarn.ts')
-rw-r--r--server/lib/plugins/yarn.ts61
1 files changed, 61 insertions, 0 deletions
diff --git a/server/lib/plugins/yarn.ts b/server/lib/plugins/yarn.ts
new file mode 100644
index 000000000..35fe1625f
--- /dev/null
+++ b/server/lib/plugins/yarn.ts
@@ -0,0 +1,61 @@
1import { execShell } from '../../helpers/core-utils'
2import { logger } from '../../helpers/logger'
3import { isNpmPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
4import { CONFIG } from '../../initializers/config'
5import { outputJSON, pathExists } from 'fs-extra'
6import { join } from 'path'
7
8async function installNpmPlugin (name: string, version: string) {
9 // Security check
10 checkNpmPluginNameOrThrow(name)
11 checkPluginVersionOrThrow(version)
12
13 const toInstall = `${name}@${version}`
14 await execYarn('add ' + toInstall)
15}
16
17async function installNpmPluginFromDisk (path: string) {
18 await execYarn('add file:' + path)
19}
20
21async function removeNpmPlugin (name: string) {
22 checkNpmPluginNameOrThrow(name)
23
24 await execYarn('remove ' + name)
25}
26
27// ############################################################################
28
29export {
30 installNpmPlugin,
31 installNpmPluginFromDisk,
32 removeNpmPlugin
33}
34
35// ############################################################################
36
37async function execYarn (command: string) {
38 try {
39 const pluginDirectory = CONFIG.STORAGE.PLUGINS_DIR
40 const pluginPackageJSON = join(pluginDirectory, 'package.json')
41
42 // Create empty package.json file if needed
43 if (!await pathExists(pluginPackageJSON)) {
44 await outputJSON(pluginPackageJSON, {})
45 }
46
47 await execShell(`yarn ${command}`, { cwd: pluginDirectory })
48 } catch (result) {
49 logger.error('Cannot exec yarn.', { command, err: result.err, stderr: result.stderr })
50
51 throw result.err
52 }
53}
54
55function checkNpmPluginNameOrThrow (name: string) {
56 if (!isNpmPluginNameValid(name)) throw new Error('Invalid NPM plugin name to install')
57}
58
59function checkPluginVersionOrThrow (name: string) {
60 if (!isPluginVersionValid(name)) throw new Error('Invalid NPM plugin version to install')
61}