config.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const fs = require('fs-extra')
  2. const path = require('path')
  3. const homedir = require('os').homedir()
  4. const { get, set, unset, error, launch } = require('@vue/cli-shared-utils')
  5. async function configure (value, options) {
  6. const file = path.resolve(homedir, '.vuerc')
  7. const config = await fs.readJson(file)
  8. if (!options.delete && !options.get && !options.edit && !options.set) {
  9. if (options.json) {
  10. console.log(JSON.stringify({
  11. resolvedPath: file,
  12. content: config
  13. }))
  14. } else {
  15. console.log('Resolved path: ' + file + '\n', JSON.stringify(config, null, 2))
  16. }
  17. }
  18. if (options.get) {
  19. // eslint-disable-next-line no-shadow
  20. const value = get(config, options.get)
  21. if (options.json) {
  22. console.log(JSON.stringify({
  23. value
  24. }))
  25. } else {
  26. console.log(value)
  27. }
  28. }
  29. if (options.delete) {
  30. unset(config, options.delete)
  31. await fs.writeFile(file, JSON.stringify(config, null, 2), 'utf-8')
  32. if (options.json) {
  33. console.log(JSON.stringify({
  34. deleted: options.delete
  35. }))
  36. } else {
  37. console.log(`You have removed the option: ${options.delete}`)
  38. }
  39. }
  40. if (options.edit) {
  41. launch(file)
  42. }
  43. if (options.set && !value) {
  44. throw new Error(`Make sure you define a value for the option ${options.set}`)
  45. }
  46. if (options.set && value) {
  47. set(config, options.set, value)
  48. if (value.match('[0-9]')) {
  49. set(config, options.set, parseInt(value))
  50. }
  51. if (value === 'true') {
  52. set(config, options.set, true)
  53. }
  54. if (value === 'false') {
  55. set(config, options.set, false)
  56. }
  57. await fs.writeFile(file, JSON.stringify(config, null, 2), 'utf-8')
  58. if (options.json) {
  59. console.log(JSON.stringify({
  60. updated: options.set
  61. }))
  62. } else {
  63. console.log(`You have updated the option: ${options.set} to ${value}`)
  64. }
  65. }
  66. }
  67. module.exports = (...args) => {
  68. return configure(...args).catch(err => {
  69. error(err)
  70. if (!process.env.VUE_CLI_TEST) {
  71. process.exit(1)
  72. }
  73. })
  74. }