Skip to content

Commit baa1e04

Browse files
committed
refactor: extract rc file loading logic
1 parent b9ecb90 commit baa1e04

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

packages/@vue/cli-ui/apollo-server/util/db.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ const Lowdb = require('lowdb')
22
const FileSync = require('lowdb/adapters/FileSync')
33
const fs = require('fs-extra')
44
const path = require('path')
5-
const os = require('os')
6-
const { xdgConfigPath } = require('@vue/cli/lib/util/xdgConfig')
5+
const { getRcPath } = require('@vue/cli/lib/util/rcPath')
76

87
let folder
98

@@ -14,9 +13,10 @@ if (process.env.VUE_CLI_UI_TEST) {
1413
} else if (process.env.VUE_APP_CLI_UI_DEV) {
1514
folder = '../../live'
1615
} else {
17-
folder = process.env.VUE_CLI_UI_DB_PATH ||
18-
xdgConfigPath('.vue-cli-ui') ||
19-
path.join(os.homedir(), '.vue-cli-ui')
16+
folder = (
17+
process.env.VUE_CLI_UI_DB_PATH ||
18+
getRcPath('.vue-cli-ui')
19+
)
2020
}
2121

2222
fs.ensureDirSync(path.resolve(__dirname, folder))

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
const fs = require('fs')
2-
const os = require('os')
3-
const path = require('path')
42
const cloneDeep = require('lodash.clonedeep')
3+
const { getRcPath } = require('./util/rcPath')
4+
const { exit } = require('@vue/cli-shared-utils/lib/exit')
55
const { error } = require('@vue/cli-shared-utils/lib/logger')
66
const { createSchema, validate } = require('@vue/cli-shared-utils/lib/validate')
7-
const { exit } = require('@vue/cli-shared-utils/lib/exit')
8-
const { xdgConfigPath, windowsConfigPath } = require('./util/rcPath')
97

108
const rcPath = exports.rcPath = (
119
process.env.VUE_CLI_CONFIG_PATH ||
12-
xdgConfigPath() ||
13-
windowsConfigPath() ||
14-
path.join(os.homedir(), '.vuerc')
10+
getRcPath('.vuerc')
1511
)
1612

1713
const presetSchema = createSchema(joi => joi.object().keys({

packages/@vue/cli/lib/util/rcPath.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ const fs = require('fs')
22
const os = require('os')
33
const path = require('path')
44

5-
exports.xdgConfigPath = () => {
5+
const xdgConfigPath = file => {
66
const xdgConfigHome = process.env.XDG_CONFIG_HOME
77
if (xdgConfigHome) {
88
const rcDir = path.join(xdgConfigHome, 'vue')
99
if (!fs.existsSync(rcDir)) {
1010
fs.mkdirSync(rcDir, 0o700)
1111
}
12-
return path.join(rcDir, '.vuerc')
12+
return path.join(rcDir, file)
1313
}
1414
}
1515

16-
exports.windowsConfigPath = file => {
16+
const windowsConfigPath = file => {
1717
if (process.platform !== 'win32') {
1818
return
1919
}
@@ -23,15 +23,21 @@ exports.windowsConfigPath = file => {
2323
if (!fs.existsSync(rcDir)) {
2424
fs.mkdirSync(rcDir)
2525
}
26-
const rcPath = path.join(rcDir, '.vuerc')
26+
const rcPath = path.join(rcDir, file)
2727
// migration for < 3.0.0-rc.7
28-
const oldRcFile = path.join(os.homedir(), '.vuerc')
28+
const oldRcFile = path.join(os.homedir(), file)
2929
if (fs.existsSync(oldRcFile)) {
3030
fs.writeFileSync(rcPath, fs.readFileSync(oldRcFile))
3131
const chalk = require('chalk')
32-
console.log(`Detected ${chalk.cyan(`.vuerc`)} in ${chalk.cyan(path.dirname(oldRcFile))}...`)
32+
console.log(`Detected ${chalk.cyan(file)} in ${chalk.cyan(path.dirname(oldRcFile))}...`)
3333
console.log(`Migrated to ${chalk.cyan(rcPath)}.`)
3434
}
3535
return rcPath
3636
}
3737
}
38+
39+
exports.getRcPath = file => (
40+
xdgConfigPath(file) ||
41+
windowsConfigPath(file) ||
42+
path.join(os.homedir(), file)
43+
)

0 commit comments

Comments
 (0)