cwd.js 662 B

12345678910111213141516171819202122232425262728
  1. const channels = require('../channels')
  2. const fs = require('fs')
  3. const path = require('path')
  4. let cwd = process.cwd()
  5. function normalize (value) {
  6. if (value.length === 1) return value
  7. const lastChar = value.charAt(value.length - 1)
  8. if (lastChar === path.sep) {
  9. value = value.substr(0, value.length - 1)
  10. }
  11. return value
  12. }
  13. module.exports = {
  14. get: () => cwd,
  15. set: (value, context) => {
  16. value = normalize(value)
  17. if (!fs.existsSync(value)) return
  18. cwd = value
  19. process.env.VUE_CLI_CONTEXT = value
  20. context.pubsub.publish(channels.CWD_CHANGED, { cwdChanged: value })
  21. try {
  22. process.chdir(value)
  23. } catch (err) {}
  24. }
  25. }