-
-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from oslabs-beta/master
SeeQR 8.0
- Loading branch information
Showing
39 changed files
with
44,780 additions
and
11,700 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-disable func-names */ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { LogType } from '../BE_types'; | ||
|
||
interface Logger { | ||
log: (message: string, logType?: LogType, opt1?: any, opt2?: any) => void; | ||
} | ||
|
||
// Helper functions! | ||
const saveLogMessage = (message) => { | ||
const time = new Date().toLocaleDateString('en-US', { timeZone: 'UTC' }); | ||
}; | ||
|
||
// Export | ||
const logger = function ( | ||
message: string, | ||
logType: LogType = LogType.NORMAL, | ||
opt1?, | ||
opt2? | ||
) { | ||
// Code for the log color | ||
let colorCode = 0; | ||
|
||
// Prefix for the message | ||
let logString: any = logType; | ||
|
||
switch (logType) { | ||
case LogType.ERROR: // RED | ||
colorCode = 31; | ||
break; | ||
|
||
case LogType.SUCCESS: // GREEN | ||
colorCode = 32; | ||
break; | ||
|
||
case LogType.WARNING: // YELLOW | ||
colorCode = 33; | ||
break; | ||
|
||
case LogType.RECEIVE: // BLUE | ||
colorCode = 34; | ||
logString = 'SERVER_IPC_RECEIVE'; | ||
break; | ||
|
||
case LogType.SEND: // MAGENTA | ||
colorCode = 35; | ||
logString = 'SERVER_IPC_SEND'; | ||
break; | ||
|
||
case LogType.NORMAL: // WHITE (And change logType string) | ||
colorCode = 0; | ||
logString = 'LOG'; | ||
break; | ||
|
||
default: | ||
colorCode = 0; | ||
break; | ||
} | ||
|
||
let moreText = ''; | ||
|
||
if (opt1) moreText += JSON.stringify(opt1); | ||
if (opt2) moreText += JSON.stringify(opt2); | ||
|
||
console.log( | ||
`\u001b[1;${colorCode}m ${`[${logType}] ${message + moreText}`}` + | ||
`\u001b[1;0m` | ||
); | ||
saveLogMessage(`[${logType}] ${message}`); | ||
}; | ||
|
||
export default logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* eslint-disable no-throw-literal */ | ||
/* eslint-disable no-shadow */ | ||
/* eslint-disable object-shorthand */ | ||
// import path from 'path'; | ||
import fs from 'fs'; | ||
import os from 'os'; | ||
import { DBType, DocConfigFile, LogType } from './BE_types'; | ||
import logger from './Logging/masterlog'; | ||
|
||
const home = `${os.homedir()}/Documents/SeeQR`; | ||
const configFile = `config.json`; | ||
const configPath = `${home}/${configFile}`; | ||
|
||
const writeConfigDefault = function (): DocConfigFile { | ||
logger('Could not find config file. Creating default', LogType.WARNING); | ||
|
||
const defaultFile: DocConfigFile = { | ||
mysql_user: 'mysql', | ||
mysql_pass: 'mysql', | ||
mysql_port: 3306, | ||
pg_user: 'postgres', | ||
pg_pass: 'postgres', | ||
pg_port: 5432 | ||
}; | ||
|
||
fs.writeFileSync(configPath, JSON.stringify(defaultFile)); | ||
|
||
return defaultFile; | ||
}; | ||
|
||
const readConfigFile = function (): DocConfigFile { | ||
if (fs.existsSync(configPath)) { | ||
try { | ||
const text = fs.readFileSync(configPath, 'utf-8'); | ||
return JSON.parse(text) as DocConfigFile; | ||
} catch (err: any) { | ||
throw `Error parsing config file: ${err.message}`; | ||
} | ||
} else { | ||
return writeConfigDefault(); | ||
} | ||
}; | ||
|
||
interface DocConfig { | ||
getConfigFolder: () => string; | ||
getCredentials: (dbType: DBType) => { user: string; pass: string, port: number | string }; | ||
getFullConfig: () => Object; | ||
saveConfig: (config: Object) => void; | ||
} | ||
|
||
const docConfig: DocConfig = { | ||
getConfigFolder: function () { | ||
if (fs.existsSync(home)) { | ||
logger(`Found documents directory: ${home}`, LogType.SUCCESS); | ||
} else { | ||
logger( | ||
`Could not find documents directory. Creating at: ${home}`, | ||
LogType.WARNING | ||
); | ||
fs.mkdirSync(home); | ||
} | ||
return home; | ||
}, | ||
|
||
getCredentials: function (dbType: DBType) { | ||
this.getConfigFolder(); | ||
|
||
let configFile: DocConfigFile; | ||
try { | ||
configFile = readConfigFile(); | ||
} catch (err: any) { | ||
logger(err.message, LogType.WARNING); | ||
return { user: 'none', pass: 'none', port: 1 }; | ||
} | ||
|
||
if (dbType === DBType.Postgres) { | ||
return { user: configFile.pg_user, pass: configFile.pg_pass, port: configFile.pg_port }; | ||
} | ||
if (dbType === DBType.MySQL) { | ||
return { user: configFile.mysql_user, pass: configFile.mysql_pass, port: configFile.mysql_port }; | ||
} | ||
logger('Could not get credentials of DBType: ', LogType.ERROR, dbType); | ||
return { user: 'none', pass: 'none', port: 1 }; | ||
}, | ||
|
||
getFullConfig: function() { | ||
this.getConfigFolder(); | ||
let configFile: DocConfigFile; | ||
try { | ||
configFile = readConfigFile(); | ||
return configFile; | ||
} catch (err: any) { | ||
logger(err.message, LogType.WARNING); | ||
return { | ||
mysql_user: 'Failed to retrieve data.', mysql_pass: 'Failed to retrieve data.', mysql_port: 'Failed to retrieve data.', | ||
pg_user: 'Failed to retrieve data.', pg_pass: 'Failed to retrieve data.', pg_port: 'Failed to retrieve data.' | ||
}; | ||
} | ||
}, | ||
|
||
saveConfig: function(config: Object) { | ||
try { | ||
fs.writeFileSync(configPath, JSON.stringify(config)); | ||
logger('Saved new config: ', LogType.NORMAL, config); | ||
} catch(err: any) { | ||
logger(err.message, LogType.WARNING); | ||
} | ||
|
||
} | ||
}; | ||
|
||
module.exports = docConfig; |
Oops, something went wrong.