UNPKG

2.93 kBJavaScriptView Raw
1const fs = require('fs')
2const path = require('path')
3const readPkg = require('read-pkg')
4const chalk = require('chalk')
5const { warn, error } = require('@vue/cli-shared-utils')
6// const { defaults, validate } = require('./options')
7
8module.exports = async function loadUserOptions (context) {
9 // controlla.config.js
10 let fileConfig, pkgConfig, resolved, resolvedFrom
11 const configPath = (
12 process.env.CONTROLLA_CLI_SERVICE_CONFIG_PATH ||
13 path.resolve(context, 'controlla.config.js')
14 )
15 if (await fs.existsSync(configPath)) {
16 try {
17 fileConfig = require(configPath)
18
19 if (typeof fileConfig === 'function') {
20 fileConfig = fileConfig()
21 }
22
23 if (!fileConfig || typeof fileConfig !== 'object') {
24 error(
25 `Error loading ${chalk.bold('controlla.config.js')}: should export an object or a function that returns object.`
26 )
27 fileConfig = null
28 }
29 } catch (e) {
30 error(`Error loading ${chalk.bold('controlla.config.js')}:`)
31 throw e
32 }
33 }
34
35 // package.controlla
36 pkgConfig = await resolvePkg(context)
37 if (pkgConfig && typeof pkgConfig !== 'object') {
38 error(
39 `Error loading controlla config in ${chalk.bold(`package.json`)}: ` +
40 `the "controlla" field should be an object.`
41 )
42 pkgConfig = null
43 }
44
45 if (fileConfig) {
46 if (pkgConfig) {
47 warn(
48 `"controlla" field in package.json ignored ` +
49 `due to presence of ${chalk.bold('controlla.config.js')}.`
50 )
51 warn(
52 `You should migrate it into ${chalk.bold('controlla.config.js')} ` +
53 `and remove it from package.json.`
54 )
55 }
56 resolved = fileConfig
57 } else {
58 if (pkgConfig) {
59 resolved = pkgConfig
60 resolvedFrom = '"controlla" field in package.json'
61 } else {
62 resolvedFrom = 'controlla.config.js'
63 error(
64 `Error loading controlla config in ${chalk.bold(`package.json`)} ` +
65 `and error loading ${chalk.bold(resolvedFrom)}`
66 )
67 return null
68 }
69 }
70
71 if (typeof resolved.baseUrl !== 'undefined') {
72 if (typeof resolved.publicPath !== 'undefined') {
73 warn(
74 `You have set both "baseUrl" and "publicPath" in ${chalk.bold('controlla.config.js')}, ` +
75 `in this case, "baseUrl" will be ignored in favor of "publicPath".`
76 )
77 } else {
78 warn(
79 `"baseUrl" option in ${chalk.bold('controlla.config.js')} ` +
80 `is deprecated now, please use "publicPath" instead.`
81 )
82 resolved.publicPath = resolved.baseUrl
83 }
84 }
85
86 // // validate options
87 // validate(resolved, msg => {
88 // error(
89 // `Invalid options in ${chalk.bold(resolvedFrom)}: ${msg}`
90 // )
91 // })
92
93 return resolved
94}
95
96async function resolvePkg (context) {
97 if (await fs.existsSync(path.join(context, 'package.json'))) {
98 const pkg = readPkg.sync({ cwd: context })
99 return pkg.controlla
100 } else {
101 return {}
102 }
103}