suggestions.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const { semver, loadModule } = require('@vue/cli-shared-utils')
  2. const invoke = require('@vue/cli/lib/invoke')
  3. const add = require('@vue/cli/lib/add')
  4. const ROUTER = 'org.vue.vue-router-add'
  5. const VUEX = 'org.vue.vuex-add'
  6. const VUE_CONFIG_OPEN = 'org.vue.vue-config-open'
  7. module.exports = api => {
  8. api.onViewOpen(({ view }) => {
  9. if (view.id === 'vue-project-plugins') {
  10. if (!api.hasPlugin('router')) {
  11. api.addSuggestion({
  12. id: ROUTER,
  13. type: 'action',
  14. label: 'org.vue.cli-service.suggestions.vue-router-add.label',
  15. message: 'org.vue.cli-service.suggestions.vue-router-add.message',
  16. link: 'https://router.vuejs.org/',
  17. async handler () {
  18. await install(api, 'router')
  19. }
  20. })
  21. }
  22. if (!api.hasPlugin('vuex')) {
  23. api.addSuggestion({
  24. id: VUEX,
  25. type: 'action',
  26. label: 'org.vue.cli-service.suggestions.vuex-add.label',
  27. message: 'org.vue.cli-service.suggestions.vuex-add.message',
  28. link: 'https://vuex.vuejs.org/',
  29. async handler () {
  30. await install(api, 'vuex')
  31. }
  32. })
  33. }
  34. } else {
  35. [ROUTER, VUEX].forEach(id => api.removeSuggestion(id))
  36. }
  37. if (view.id !== 'vue-project-configurations') {
  38. api.removeSuggestion(VUE_CONFIG_OPEN)
  39. }
  40. })
  41. api.onConfigRead(({ config }) => {
  42. if (config.id === 'org.vue.vue-cli') {
  43. if (config.foundFiles.vue) {
  44. api.addSuggestion({
  45. id: VUE_CONFIG_OPEN,
  46. type: 'action',
  47. label: 'org.vue.vue-webpack.suggestions.vue-config-open',
  48. handler () {
  49. const file = config.foundFiles.vue.path
  50. console.log('open', file)
  51. const { launch } = require('@vue/cli-shared-utils')
  52. launch(file)
  53. return {
  54. keep: true
  55. }
  56. }
  57. })
  58. return
  59. }
  60. }
  61. api.removeSuggestion(VUE_CONFIG_OPEN)
  62. })
  63. }
  64. async function install (api, id) {
  65. api.setProgress({
  66. status: 'org.vue.cli-service.suggestions.progress',
  67. args: [id],
  68. progress: -1
  69. })
  70. const context = api.getCwd()
  71. let error
  72. try {
  73. const servicePkg = loadModule('@vue/cli-service/package.json', context)
  74. // @vue/cli-plugin-router is not compatible with @vue/cli-service v3,
  75. // so we have to check for the version and call the right generator
  76. if (semver.satisfies(servicePkg.version, '3.x')) {
  77. await invoke.runGenerator(context, {
  78. id: `core:${id}`,
  79. apply: loadModule(`@vue/cli-service/generator/${id}`, context)
  80. })
  81. } else {
  82. // FIXME: a temporary fix for adding router plugin
  83. // should implement a plugin prompt ui later
  84. await add(id, { $inlineOptions: '{}' }, context)
  85. }
  86. } catch (e) {
  87. error = e
  88. }
  89. api.removeProgress()
  90. if (error) throw error
  91. }