UNPKG

mk-mobile-command

Version:
58 lines (47 loc) 1.4 kB
'use strict'; const fs = require('fs'); const path = require('path'); const paths = require('./paths'); delete require.cache[require.resolve('./paths')]; const NODE_ENV = process.env.NODE_ENV; if (!NODE_ENV) { throw new Error( '必须指定NODE_ENV环境变量' ); } //环境文件 var dotenvFiles = [ `${paths.dotenv}.${NODE_ENV}.local`, `${paths.dotenv}.${NODE_ENV}`, paths.dotenv, ].filter(Boolean); //将.env文件中的环境变量加载到 process.env dotenvFiles.forEach(dotenvFile => { if (fs.existsSync(dotenvFile)) { require('dotenv-expand')( require('dotenv').config({ path: dotenvFile, }) ); } }); const appDirectory = fs.realpathSync(process.cwd()); process.env.NODE_PATH = (process.env.NODE_PATH || '') .split(path.delimiter) .filter(folder => folder && !path.isAbsolute(folder)) .map(folder => path.resolve(appDirectory, folder)) .join(path.delimiter); function getClientEnvironment(publicUrl) { const raw = { NODE_ENV: process.env.NODE_ENV || 'development', PUBLIC_URL: publicUrl, } const stringified = { 'process.env': Object.keys(raw).reduce((env, key) => { env[key] = JSON.stringify(raw[key]); return env; }, {}), }; return { raw, stringified }; } module.exports = getClientEnvironment;