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