UNPKG

1.74 kBJavaScriptView Raw
1const fs = require('fs')
2const getPathnameProjectConfigFile = require('./get-pathname-project-config-file')
3// const readBuildConfigFile = require('../utils/read-build-config-file')
4const getAppTypeString = require('./get-app-type-string')
5const envUpdateAppType = require('../libs/env/update-app-type')
6
7const extractType = () => {
8 const pathnameKootJS = getPathnameProjectConfigFile()
9
10 try {
11 const { type } = require(pathnameKootJS)
12 return type
13 } catch (e) { }
14
15 const content = fs.readFileSync(pathnameKootJS, 'utf-8')
16 const matches = /type[ ]*=[ ]*['"](.+?)['"]/gm.exec(content)
17 if (Array.isArray(matches) && matches.length > 1)
18 return matches[1]
19
20 return undefined
21}
22
23/**
24 * 根据 KOOT_PROJECT_TYPE 环境变量确定项目类型 (type),并修改/写入以下环境变量
25 * - WEBPACK_BUILD_TYPE: 'isomorphic' || 'spa' || etc...
26 * - KOOT_PROJECT_TYPE: 'ReactApp' || 'ReactSPA' || etc...
27 *
28 * 如果该环境变量未指定或为空值,则会尝试从项目配置中读取
29 *
30 * 项目配置:在 0.6 之前为 koot.js,0.6 之后为自动生成的临时配置文件
31 * - 使用临时配置文件是为了兼容 0.6 之前的行为
32 * - TODO: 在未来可能会抛弃独立配置文件行为,界时该方法会改写
33 *
34 * @async
35 * @param {String} [projectType] 指定项目类型,如果指定会强制采用该值
36 * @returns {String} process.env.KOOT_PROJECT_TYPE
37 */
38module.exports = async (projectType = process.env.KOOT_PROJECT_TYPE) => {
39 if (!projectType) {
40 projectType = extractType() || ''
41 }
42
43 envUpdateAppType(getAppTypeString(projectType))
44
45 return process.env.KOOT_PROJECT_TYPE
46}