getPkg.js 787 B

1234567891011121314151617181920212223242526272829303132
  1. // Get the package.json containing all the `vue-cli-pluin-*` dependencies
  2. // See issue #1815
  3. const fs = require('fs')
  4. const path = require('path')
  5. function getPackageJson (projectPath) {
  6. const packagePath = path.join(projectPath, 'package.json')
  7. let packageJson
  8. try {
  9. packageJson = fs.readFileSync(packagePath, 'utf-8')
  10. } catch (err) {
  11. throw new Error(`${packagePath} not exist`)
  12. }
  13. try {
  14. packageJson = JSON.parse(packageJson)
  15. } catch (err) {
  16. throw new Error('The package.json is malformed')
  17. }
  18. return packageJson
  19. }
  20. module.exports = function getPkg (context) {
  21. const pkg = getPackageJson(context)
  22. if (pkg.vuePlugins && pkg.vuePlugins.resolveFrom) {
  23. return getPackageJson(path.resolve(context, pkg.vuePlugins.resolveFrom))
  24. }
  25. return pkg
  26. }