11const path = require ( 'path' ) ;
22const fs = require ( 'fs' ) ;
3- const os = require ( 'os' ) ;
4- // eslint-disable-next-line no-undef
5- const DEFAULT_PATH = path . join ( process . cwd ( ) , 'config' ) ;
6- const APP_DATA = path . join ( os . homedir ( ) , '/AppData/Roaming' ) ;
7- const { SUGAR_OPTION , CONFIG_GET } = require ( '../const' ) ;
8- global [ SUGAR_OPTION ] . configPath = DEFAULT_PATH ;
9- const config = { } ;
10- let hasInit = false ;
11- let appName = '' ;
3+ const { app } = require ( 'electron' ) ;
4+ const { CONFIG } = require ( '../const' ) ;
5+
126// 获取环境变量参数
137function getProcessArgv ( ) {
148 const argv = { } ;
@@ -24,15 +18,17 @@ function getProcessArgv() {
2418 return argv ;
2519}
2620// 从appData获取配置
27- function getConfigFromAppData ( appName ) {
28- const configPath = path . join ( APP_DATA , appName , 'config.json' ) ;
21+ function getConfigFromAppData ( useAppPathConfig ) {
2922 let config = { } ;
30- try {
31- // 从appData读取环境变量
32- const res = fs . readFileSync ( configPath ) ;
33- config = JSON . parse ( res . toString ( ) ) ;
34- } catch ( error ) {
35- console . error ( '获取appData配置失败,不影响使用' ) ;
23+ if ( useAppPathConfig ) {
24+ try {
25+ const configPath = path . join ( app . getPath ( 'userData' ) , 'config.json' ) ;
26+ // 从appData读取环境变量
27+ const res = fs . readFileSync ( configPath ) ;
28+ config = JSON . parse ( res . toString ( ) ) ;
29+ } catch ( error ) {
30+ console . log ( '[sugar-electron] get appData fail,can continue to use' ) ;
31+ }
3632 }
3733 return Object . assign ( { env : '' , config : { } } , config ) ;
3834}
@@ -41,7 +37,7 @@ function getLocalBaseConfig(configPath) {
4137 try {
4238 return require ( path . join ( configPath , 'config.base' ) ) || { }
4339 } catch ( error ) {
44- console . error ( error ) ;
40+ console . error ( '[sugar-electron]' , error ) ;
4541 return { } ;
4642 }
4743}
@@ -51,39 +47,23 @@ function getLocalConfig(configPath, env) {
5147 try {
5248 return require ( path . join ( configPath , configName ) ) || { } ;
5349 } catch ( error ) {
54- console . error ( error ) ;
50+ console . error ( '[sugar-electron]' , error ) ;
5551 return { } ;
5652 }
5753}
5854
59- const getConfig = global [ CONFIG_GET ] = function ( ) {
60- if ( hasInit === false ) {
61- const appData = getConfigFromAppData ( appName ) ;
55+ class Config {
56+ getConfig ( { useAppPathConfig , configPath } ) {
57+ const appData = getConfigFromAppData ( useAppPathConfig ) ;
6258 const argv = getProcessArgv ( ) ;
6359 const env = appData . env || argv . env || '' ;
64- const baseLocalConfig = getLocalBaseConfig ( global [ SUGAR_OPTION ] . configPath ) ;
65- const localConfig = getLocalConfig ( global [ SUGAR_OPTION ] . configPath , env ) ;
66- Object . assign ( config , { argv } , baseLocalConfig , localConfig , appData . config ) ;
67- hasInit = true ;
60+ const baseLocalConfig = getLocalBaseConfig ( configPath ) ;
61+ const localConfig = getLocalConfig ( configPath , env ) ;
62+ Object . assign ( this , { argv } , baseLocalConfig , localConfig , appData . config ) ;
63+ return this ;
6864 }
69- return config ;
7065}
7166
72- /**
73- * 设置参数
74- * @param {object } params
75- * option.appName 应用名
76- * option.configPath 默认配置目录路径,如果不传则自动加载根目录config目录
77- * */
78- const setOption = function ( params = { } ) {
79- appName = params . appName || '' ;
80- global [ SUGAR_OPTION ] . configPath = params . configPath ;
81- return getConfig ( ) ;
82- }
83-
84- config . getConfig = getConfig ;
85- config . setOption = setOption ;
86-
87- module . exports = config ;
88-
89-
67+ const config = new Config ( ) ;
68+ global [ CONFIG ] = config ;
69+ module . exports = config ;
0 commit comments