Skip to content

Commit 153c418

Browse files
Payton BurdetteAkryum
authored andcommitted
feat: vue config command (vuejs#1554)
* fixed broken plugin dev link on contributing guide * feat(cli-service): vue config command added * feat(cli-service): vue config command added * feat(cli-service): added config commands get and delete * feat(cli-service): added vue edit command and opn dependcy * feat(cli-service): added vue config set command and vue config check * feat(cli-service): nested path support and command/logs adjustment * feat(cli-service): command option descriptions updated with preset * refactor: object get/set/unset * feat: json option + fs/JSON fixes
1 parent 33b852f commit 153c418

File tree

10 files changed

+108
-15
lines changed

10 files changed

+108
-15
lines changed

packages/@vue/cli-service/lib/commands/inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = (api, options) => {
1111
'--verbose': 'show full function definitions in output'
1212
}
1313
}, args => {
14-
const get = require('get-value')
14+
const { get } = require('@vue/cli-shared-utils')
1515
const { toString } = require('webpack-chain')
1616
const config = api.resolveWebpackConfig()
1717
const { _: paths, verbose } = args

packages/@vue/cli-service/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
"file-loader": "^1.1.11",
4242
"friendly-errors-webpack-plugin": "^1.7.0",
4343
"fs-extra": "^6.0.1",
44-
"get-value": "^3.0.1",
4544
"globby": "^8.0.1",
4645
"hash-sum": "^1.0.2",
4746
"html-webpack-plugin": "^3.2.0",

packages/@vue/cli-shared-utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
'exit',
44
'ipc',
55
'logger',
6+
'object',
67
'openBrowser',
78
'pluginResolution',
89
'request',

packages/@vue/cli-ui/src/util/object.js renamed to packages/@vue/cli-shared-utils/lib/object.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ exports.get = function (target, path) {
2626
return obj[fields[l - 1]]
2727
}
2828

29-
exports.remove = function (target, path) {
29+
exports.unset = function (target, path) {
3030
const fields = path.split('.')
3131
let obj = target
3232
const l = fields.length

packages/@vue/cli-ui/apollo-server/connectors/configurations.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const plugins = require('./plugins')
99
const folders = require('./folders')
1010
const prompts = require('./prompts')
1111
// Utils
12-
const { get, set, remove } = require('../../src/util/object')
12+
const { get, set, unset } = require('@vue/cli-shared-utils')
1313
const { log } = require('../util/logger')
1414
const { loadModule } = require('@vue/cli/lib/util/module')
1515
const extendJSConfig = require('@vue/cli/lib/util/extendJSConfig')
@@ -232,7 +232,7 @@ async function save (id, context) {
232232

233233
const value = newData[key]
234234
if (typeof value === 'undefined') {
235-
remove(data[fileId], key)
235+
unset(data[fileId], key)
236236
} else {
237237
set(data[fileId], key, value)
238238
}

packages/@vue/cli-ui/apollo-server/connectors/prompts.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Utils
2-
const ObjectUtil = require('../../src/util/object')
2+
const { get, set, unset } = require('@vue/cli-shared-utils')
33
const { log } = require('../util/logger')
44

55
let answers = {}
@@ -85,11 +85,11 @@ async function getChoices (prompt) {
8585
}
8686

8787
function setAnswer (id, value) {
88-
ObjectUtil.set(answers, id, value)
88+
set(answers, id, value)
8989
}
9090

9191
function removeAnswer (id) {
92-
ObjectUtil.remove(answers, id)
92+
unset(answers, id)
9393
}
9494

9595
function generatePrompt (data) {
@@ -159,7 +159,7 @@ function getAnswers () {
159159
}
160160

161161
function getAnswer (id) {
162-
return ObjectUtil.get(answers, id)
162+
return get(answers, id)
163163
}
164164

165165
async function reset () {

packages/@vue/cli/bin/vue.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,18 @@ program
126126
loadCommand('init', '@vue/cli-init')
127127
})
128128

129+
program
130+
.command('config [value]')
131+
.description('inspect and modify the config')
132+
.option('-g, --get <path>', 'get value from option')
133+
.option('-s, --set <path> <value>', 'set option value')
134+
.option('-d, --delete <path>', 'delete option from config')
135+
.option('-e, --edit', 'open config with default editor')
136+
.option('--json', 'outputs JSON result only')
137+
.action((value, cmd) => {
138+
require('../lib/config')(value, cleanArgs(cmd))
139+
})
140+
129141
// output help information on unknown commands
130142
program
131143
.arguments('<command>')

packages/@vue/cli/lib/config.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

packages/@vue/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"javascript-stringify": "^1.6.0",
4747
"js-yaml": "^3.12.0",
4848
"klaw-sync": "^4.0.0",
49+
"launch-editor": "^2.2.1",
4950
"lodash.clonedeep": "^4.5.0",
5051
"minimist": "^1.2.0",
5152
"recast": "^0.15.2",

yarn.lock

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5476,12 +5476,6 @@ get-value@^2.0.3, get-value@^2.0.6:
54765476
version "2.0.6"
54775477
resolved "https://registry.yarnpkg.com/get-value/-/get-value-2.0.6.tgz#dc15ca1c672387ca76bd37ac0a395ba2042a2c28"
54785478

5479-
get-value@^3.0.1:
5480-
version "3.0.1"
5481-
resolved "https://registry.yarnpkg.com/get-value/-/get-value-3.0.1.tgz#5efd2a157f1d6a516d7524e124ac52d0a39ef5a8"
5482-
dependencies:
5483-
isobject "^3.0.1"
5484-
54855479
getos@2.8.4:
54865480
version "2.8.4"
54875481
resolved "https://registry.yarnpkg.com/getos/-/getos-2.8.4.tgz#7b8603d3619c28e38cb0fe7a4f63c3acb80d5163"

0 commit comments

Comments
 (0)