7 changed files with 51 additions and 15 deletions
@ -1,5 +1,5 @@
|
||||
node_modules/ |
||||
dist/ |
||||
exported/ |
||||
api-keys.json |
||||
config.json |
||||
package-lock.json |
||||
|
@ -1,3 +0,0 @@
|
||||
{ |
||||
"http://localhost:3500": "eyJrIjoiTjlUcmtLSFRNcTdqeXBaQjB5REk2TFkyUDBDM0Z1bWciLCJuIjoiZXhwb3J0LW1hbmFnZXIiLCJpZCI6MX0=" |
||||
} |
@ -0,0 +1,6 @@
|
||||
{ |
||||
"apiKeys" : { |
||||
"http://localhost:3000": "eyJrIjoiTjlUcmtLSFRNcTdqeXBaQjB5REk2TFkyUDBDM0Z1bWciLCJuIjoiZXhwb3J0LW1hbmFnZXIiLCJpZCI6MX0=" |
||||
}, |
||||
"port": "8000" |
||||
} |
@ -1,14 +1,41 @@
|
||||
import * as path from 'path'; |
||||
import * as fs from 'fs'; |
||||
import * as _ from 'lodash'; |
||||
|
||||
const DEFAULT_CONFIG = { |
||||
'apiKeys': { |
||||
'http://localhost:3000': '' |
||||
}, |
||||
'port': '8000' |
||||
}; |
||||
|
||||
export const EXPORTED_PATH = path.join(__dirname, '../exported'); |
||||
if(!fs.existsSync(EXPORTED_PATH)) { |
||||
console.log(`${EXPORTED_PATH} don't exist, creating`); |
||||
console.log(`${EXPORTED_PATH} doesn't exist, creating`); |
||||
fs.mkdirSync(EXPORTED_PATH); |
||||
} |
||||
|
||||
export function getApiKey(host) { |
||||
let data = fs.readFileSync(path.join(__dirname, '../api-keys.json'), 'utf8'); |
||||
function getConfigField(field: string, defaultVal?: any) { |
||||
let val = defaultVal; |
||||
let configFile = path.join(__dirname, '../config.json'); |
||||
|
||||
if(!fs.existsSync(configFile)) { |
||||
console.log(`${configFile} doesn't exist, creating`); |
||||
fs.writeFileSync(configFile, JSON.stringify(DEFAULT_CONFIG), 'utf8'); |
||||
} |
||||
|
||||
let data = fs.readFileSync(configFile, 'utf8'); |
||||
let configField = JSON.parse(data)[field]; |
||||
if(configField !== undefined) { |
||||
val = configField; |
||||
} |
||||
|
||||
if(val === undefined || val == '' || _.isEmpty(val)) { |
||||
throw new Error(`Please configure ${field} in ${configFile}`); |
||||
} |
||||
|
||||
return JSON.parse(data)[host] |
||||
return val; |
||||
} |
||||
|
||||
export const port = getConfigField('port', '8000'); |
||||
export const apiKeys = getConfigField('apiKeys'); |
||||
|
Loading…
Reference in new issue