UNPKG

1.33 kBJavaScriptView Raw
1const fs = require('fs-extra')
2const path = require('path')
3const chalk = require('chalk')
4const __ = require('./translate')
5const defaults = require('../defaults/build-config')
6const getPathnameBuildConfigFile = require('./get-pathname-build-config-file')
7
8/**
9 * 读取打包配置文件的内容
10 * @param {String} [pathname] 打包配置文件路径
11 * @returns {Object} 配置对象
12 */
13module.exports = (
14 pathname = getPathnameBuildConfigFile()
15) => new Promise((resolve, reject) => {
16 const filename = `${path.basename(pathname)}.${path.extname(pathname)}`
17
18 // 读取构建配置
19 if (!fs.existsSync(pathname)) {
20 console.log(
21 chalk.redBright('× ')
22 + __('file_not_found', {
23 file: chalk.yellowBright(filename),
24 })
25 )
26 return reject(new Error('FILE NOT FOUND'))
27 }
28
29 const buildConfig = Object.assign({}, defaults, require(pathname))
30 if (typeof buildConfig !== 'object') {
31 console.log(
32 chalk.redBright('× ')
33 + __('build.config_type_error', {
34 file: chalk.yellowBright(filename),
35 type: chalk.green('Object')
36 })
37 )
38 return reject(new Error('TYPE NOT OBJECT'))
39 }
40
41 resolve(buildConfig)
42})