homegames-common 0.2.0 → 0.3.0

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.
Files changed (2) hide show
  1. package/index.js +73 -12
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -609,44 +609,105 @@ 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');
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');
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
+ const logPath = getConfigValue('LOG_PATH');
680
+
681
+ if (logLevel < required) {
682
+ return;
683
+ }
684
+
685
+ const msgString = `[HOMEGAMES-DEBUG][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
686
+ fs.appendFile(logPath, msgString, (err) => {
687
+ if (err) {
688
+ console.error('failed log');
689
+ console.log(err);
690
+ }
691
+ });
692
+
693
+ }
694
+
695
+ }
696
+
697
+
631
698
  module.exports = {
632
- guaranteeCerts,
633
- certInit,
634
- getCertData,
635
699
  signup,
636
700
  login,
637
701
  confirmUser,
638
- validateExistingCerts,
639
702
  getLoginInfo,
640
703
  verifyAccessToken,
641
704
  refreshAccessToken,
642
- storeCertData,
643
705
  linkInit,
644
- storeTokens,
645
- promptLogin,
646
706
  getUserHash,
647
707
  authWorkflow,
648
708
  guaranteeDir,
649
709
  getUrl,
650
- getConfigValue
710
+ getConfigValue,
711
+ log
651
712
  };
652
713
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homegames-common",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "Homegames common tools",
5
5
  "main": "index.js",
6
6
  "scripts": {