mirror of
https://github.com/vuejs/vue-cli.git
synced 2026-05-12 14:58:26 -05:00
refactor: extract rc file loading logic
This commit is contained in:
@@ -2,8 +2,7 @@ const Lowdb = require('lowdb')
|
||||
const FileSync = require('lowdb/adapters/FileSync')
|
||||
const fs = require('fs-extra')
|
||||
const path = require('path')
|
||||
const os = require('os')
|
||||
const { xdgConfigPath } = require('@vue/cli/lib/util/xdgConfig')
|
||||
const { getRcPath } = require('@vue/cli/lib/util/rcPath')
|
||||
|
||||
let folder
|
||||
|
||||
@@ -14,9 +13,10 @@ if (process.env.VUE_CLI_UI_TEST) {
|
||||
} else if (process.env.VUE_APP_CLI_UI_DEV) {
|
||||
folder = '../../live'
|
||||
} else {
|
||||
folder = process.env.VUE_CLI_UI_DB_PATH ||
|
||||
xdgConfigPath('.vue-cli-ui') ||
|
||||
path.join(os.homedir(), '.vue-cli-ui')
|
||||
folder = (
|
||||
process.env.VUE_CLI_UI_DB_PATH ||
|
||||
getRcPath('.vue-cli-ui')
|
||||
)
|
||||
}
|
||||
|
||||
fs.ensureDirSync(path.resolve(__dirname, folder))
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
const fs = require('fs')
|
||||
const os = require('os')
|
||||
const path = require('path')
|
||||
const cloneDeep = require('lodash.clonedeep')
|
||||
const { getRcPath } = require('./util/rcPath')
|
||||
const { exit } = require('@vue/cli-shared-utils/lib/exit')
|
||||
const { error } = require('@vue/cli-shared-utils/lib/logger')
|
||||
const { createSchema, validate } = require('@vue/cli-shared-utils/lib/validate')
|
||||
const { exit } = require('@vue/cli-shared-utils/lib/exit')
|
||||
const { xdgConfigPath, windowsConfigPath } = require('./util/rcPath')
|
||||
|
||||
const rcPath = exports.rcPath = (
|
||||
process.env.VUE_CLI_CONFIG_PATH ||
|
||||
xdgConfigPath() ||
|
||||
windowsConfigPath() ||
|
||||
path.join(os.homedir(), '.vuerc')
|
||||
getRcPath('.vuerc')
|
||||
)
|
||||
|
||||
const presetSchema = createSchema(joi => joi.object().keys({
|
||||
|
||||
@@ -2,18 +2,18 @@ const fs = require('fs')
|
||||
const os = require('os')
|
||||
const path = require('path')
|
||||
|
||||
exports.xdgConfigPath = () => {
|
||||
const xdgConfigPath = file => {
|
||||
const xdgConfigHome = process.env.XDG_CONFIG_HOME
|
||||
if (xdgConfigHome) {
|
||||
const rcDir = path.join(xdgConfigHome, 'vue')
|
||||
if (!fs.existsSync(rcDir)) {
|
||||
fs.mkdirSync(rcDir, 0o700)
|
||||
}
|
||||
return path.join(rcDir, '.vuerc')
|
||||
return path.join(rcDir, file)
|
||||
}
|
||||
}
|
||||
|
||||
exports.windowsConfigPath = file => {
|
||||
const windowsConfigPath = file => {
|
||||
if (process.platform !== 'win32') {
|
||||
return
|
||||
}
|
||||
@@ -23,15 +23,21 @@ exports.windowsConfigPath = file => {
|
||||
if (!fs.existsSync(rcDir)) {
|
||||
fs.mkdirSync(rcDir)
|
||||
}
|
||||
const rcPath = path.join(rcDir, '.vuerc')
|
||||
const rcPath = path.join(rcDir, file)
|
||||
// migration for < 3.0.0-rc.7
|
||||
const oldRcFile = path.join(os.homedir(), '.vuerc')
|
||||
const oldRcFile = path.join(os.homedir(), file)
|
||||
if (fs.existsSync(oldRcFile)) {
|
||||
fs.writeFileSync(rcPath, fs.readFileSync(oldRcFile))
|
||||
const chalk = require('chalk')
|
||||
console.log(`Detected ${chalk.cyan(`.vuerc`)} in ${chalk.cyan(path.dirname(oldRcFile))}...`)
|
||||
console.log(`Detected ${chalk.cyan(file)} in ${chalk.cyan(path.dirname(oldRcFile))}...`)
|
||||
console.log(`Migrated to ${chalk.cyan(rcPath)}.`)
|
||||
}
|
||||
return rcPath
|
||||
}
|
||||
}
|
||||
|
||||
exports.getRcPath = file => (
|
||||
xdgConfigPath(file) ||
|
||||
windowsConfigPath(file) ||
|
||||
path.join(os.homedir(), file)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user