2021-04-12 04:10:48 -04:00
|
|
|
import { outputJSON, pathExists } from 'fs-extra'
|
|
|
|
import { join } from 'path'
|
2019-07-05 09:28:49 -04:00
|
|
|
import { execShell } from '../../helpers/core-utils'
|
|
|
|
import { isNpmPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins'
|
2021-04-12 04:10:48 -04:00
|
|
|
import { logger } from '../../helpers/logger'
|
2019-07-05 09:28:49 -04:00
|
|
|
import { CONFIG } from '../../initializers/config'
|
2021-04-12 04:10:48 -04:00
|
|
|
import { getLatestPluginVersion } from './plugin-index'
|
2019-07-05 09:28:49 -04:00
|
|
|
|
2021-04-12 04:10:48 -04:00
|
|
|
async function installNpmPlugin (npmName: string, versionArg?: string) {
|
2019-07-05 09:28:49 -04:00
|
|
|
// Security check
|
2019-07-19 04:37:35 -04:00
|
|
|
checkNpmPluginNameOrThrow(npmName)
|
2021-04-12 04:10:48 -04:00
|
|
|
if (versionArg) checkPluginVersionOrThrow(versionArg)
|
|
|
|
|
|
|
|
const version = versionArg || await getLatestPluginVersion(npmName)
|
2019-07-10 10:59:53 -04:00
|
|
|
|
2019-07-19 04:37:35 -04:00
|
|
|
let toInstall = npmName
|
2019-07-10 10:59:53 -04:00
|
|
|
if (version) toInstall += `@${version}`
|
2019-07-05 09:28:49 -04:00
|
|
|
|
2019-07-19 08:36:04 -04:00
|
|
|
const { stdout } = await execYarn('add ' + toInstall)
|
|
|
|
|
|
|
|
logger.debug('Added a yarn package.', { yarnStdout: stdout })
|
2019-07-05 09:28:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function installNpmPluginFromDisk (path: string) {
|
|
|
|
await execYarn('add file:' + path)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function removeNpmPlugin (name: string) {
|
|
|
|
checkNpmPluginNameOrThrow(name)
|
|
|
|
|
|
|
|
await execYarn('remove ' + name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ############################################################################
|
|
|
|
|
|
|
|
export {
|
|
|
|
installNpmPlugin,
|
|
|
|
installNpmPluginFromDisk,
|
|
|
|
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, {})
|
|
|
|
}
|
|
|
|
|
2019-07-19 08:36:04 -04:00
|
|
|
return execShell(`yarn ${command}`, { cwd: pluginDirectory })
|
2019-07-05 09:28:49 -04:00
|
|
|
} 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 (!isPluginVersionValid(name)) throw new Error('Invalid NPM plugin version to install')
|
|
|
|
}
|