ニジカ投稿局 https://tv.nizika.tv
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

74 lines
2.2 KiB

  1. import { outputJSON, pathExists } from 'fs-extra/esm'
  2. import { join } from 'path'
  3. import { execShell } from '../../helpers/core-utils.js'
  4. import { isNpmPluginNameValid, isPluginStableOrUnstableVersionValid } from '../../helpers/custom-validators/plugins.js'
  5. import { logger } from '../../helpers/logger.js'
  6. import { CONFIG } from '../../initializers/config.js'
  7. import { getLatestPluginVersion } from './plugin-index.js'
  8. async function installNpmPlugin (npmName: string, versionArg?: string) {
  9. // Security check
  10. checkNpmPluginNameOrThrow(npmName)
  11. if (versionArg) checkPluginVersionOrThrow(versionArg)
  12. const version = versionArg || await getLatestPluginVersion(npmName)
  13. let toInstall = npmName
  14. if (version) toInstall += `@${version}`
  15. const { stdout } = await execYarn('add ' + toInstall)
  16. logger.debug('Added a yarn package.', { yarnStdout: stdout })
  17. }
  18. async function installNpmPluginFromDisk (path: string) {
  19. await execYarn('add file:' + path)
  20. }
  21. async function removeNpmPlugin (name: string) {
  22. checkNpmPluginNameOrThrow(name)
  23. await execYarn('remove ' + name)
  24. }
  25. async function rebuildNativePlugins () {
  26. await execYarn('install --pure-lockfile')
  27. }
  28. // ############################################################################
  29. export {
  30. installNpmPlugin,
  31. installNpmPluginFromDisk,
  32. rebuildNativePlugins,
  33. removeNpmPlugin
  34. }
  35. // ############################################################################
  36. async function execYarn (command: string) {
  37. try {
  38. const pluginDirectory = CONFIG.STORAGE.PLUGINS_DIR
  39. const pluginPackageJSON = join(pluginDirectory, 'package.json')
  40. // Create empty package.json file if needed
  41. if (!await pathExists(pluginPackageJSON)) {
  42. await outputJSON(pluginPackageJSON, {})
  43. }
  44. return execShell(`yarn ${command}`, { cwd: pluginDirectory })
  45. } catch (result) {
  46. logger.error('Cannot exec yarn.', { command, err: result.err, stderr: result.stderr })
  47. throw result.err
  48. }
  49. }
  50. function checkNpmPluginNameOrThrow (name: string) {
  51. if (!isNpmPluginNameValid(name)) throw new Error('Invalid NPM plugin name to install')
  52. }
  53. function checkPluginVersionOrThrow (name: string) {
  54. if (!isPluginStableOrUnstableVersionValid(name)) throw new Error('Invalid NPM plugin version to install')
  55. }