homegames-common 0.2.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +74 -12
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -609,44 +609,106 @@ const getConfig = () => {
|
|
|
609
609
|
}
|
|
610
610
|
|
|
611
611
|
const options = [process.cwd(), require.main.filename, process.mainModule.filename, __dirname]
|
|
612
|
-
let baseDir = process.cwd();
|
|
613
612
|
let _config = {};
|
|
614
613
|
|
|
615
614
|
for (let i = 0; i < options.length; i++) {
|
|
616
615
|
if (fs.existsSync(`${options[i]}/config.json`)) {
|
|
617
616
|
console.log(`Using config at ${options[i]}`);
|
|
618
617
|
_config = JSON.parse(fs.readFileSync(`${options[i]}/config.json`));
|
|
618
|
+
break;
|
|
619
619
|
}
|
|
620
620
|
}
|
|
621
621
|
|
|
622
|
-
console.log('using this config data');
|
|
623
|
-
console.log(_config);
|
|
624
|
-
|
|
625
622
|
cachedConfig = _config;
|
|
626
623
|
|
|
627
624
|
return _config;
|
|
628
625
|
}
|
|
629
626
|
|
|
630
627
|
|
|
628
|
+
const getLogLevel = (logLevel = null) => {
|
|
629
|
+
const _logLevel = logLevel || getConfigValue('LOG_LEVEL', 'INFO');
|
|
630
|
+
const levelList = ['DISABLED', 'INFO', 'DEBUG'];
|
|
631
|
+
|
|
632
|
+
return levelList.indexOf(_logLevel);
|
|
633
|
+
};
|
|
634
|
+
|
|
635
|
+
const msgToString = (msg) => {
|
|
636
|
+
return typeof msg === 'object' ? JSON.stringify(msg) : msg;
|
|
637
|
+
};
|
|
638
|
+
|
|
639
|
+
const log = {
|
|
640
|
+
info: (msg, explanation = null) => {
|
|
641
|
+
const logLevel = getLogLevel();
|
|
642
|
+
const required = getLogLevel('INFO');
|
|
643
|
+
|
|
644
|
+
if (logLevel < required) {
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
const logPath = getConfigValue('LOG_PATH', 'hg_log.txt');
|
|
649
|
+
|
|
650
|
+
const msgString = `[HOMEGAMES-INFO][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
|
|
651
|
+
fs.appendFile(logPath, msgString, (err) => {
|
|
652
|
+
if (err) {
|
|
653
|
+
console.error('failed log');
|
|
654
|
+
console.log(err);
|
|
655
|
+
}
|
|
656
|
+
});
|
|
657
|
+
},
|
|
658
|
+
error: (msg, explanation) => {
|
|
659
|
+
const logLevel = getLogLevel();
|
|
660
|
+
const required = getLogLevel('INFO');
|
|
661
|
+
|
|
662
|
+
if (logLevel < required) {
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const logPath = getConfigValue('LOG_PATH', 'hg_log.txt');
|
|
667
|
+
|
|
668
|
+
const msgString = `[HOMEGAMES-ERROR][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
|
|
669
|
+
fs.appendFile(logPath, msgString, (err) => {
|
|
670
|
+
if (err) {
|
|
671
|
+
console.error('failed log');
|
|
672
|
+
console.log(err);
|
|
673
|
+
}
|
|
674
|
+
});
|
|
675
|
+
},
|
|
676
|
+
debug: (msg, explanation) => {
|
|
677
|
+
const logLevel = getLogLevel();
|
|
678
|
+
const required = getLogLevel('DEBUG');
|
|
679
|
+
|
|
680
|
+
const logPath = getConfigValue('LOG_PATH', 'hg_log.txt');
|
|
681
|
+
|
|
682
|
+
if (logLevel < required) {
|
|
683
|
+
return;
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
const msgString = `[HOMEGAMES-DEBUG][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
|
|
687
|
+
fs.appendFile(logPath, msgString, (err) => {
|
|
688
|
+
if (err) {
|
|
689
|
+
console.error('failed log');
|
|
690
|
+
console.log(err);
|
|
691
|
+
}
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
|
|
631
699
|
module.exports = {
|
|
632
|
-
guaranteeCerts,
|
|
633
|
-
certInit,
|
|
634
|
-
getCertData,
|
|
635
700
|
signup,
|
|
636
701
|
login,
|
|
637
702
|
confirmUser,
|
|
638
|
-
validateExistingCerts,
|
|
639
703
|
getLoginInfo,
|
|
640
704
|
verifyAccessToken,
|
|
641
705
|
refreshAccessToken,
|
|
642
|
-
storeCertData,
|
|
643
706
|
linkInit,
|
|
644
|
-
storeTokens,
|
|
645
|
-
promptLogin,
|
|
646
707
|
getUserHash,
|
|
647
708
|
authWorkflow,
|
|
648
709
|
guaranteeDir,
|
|
649
710
|
getUrl,
|
|
650
|
-
getConfigValue
|
|
711
|
+
getConfigValue,
|
|
712
|
+
log
|
|
651
713
|
};
|
|
652
714
|
|