import * as path from 'path'; import * as fs from 'fs'; import * as _ from 'lodash'; const DEFAULT_CONFIG = { 'port': '8000' }; export const DATA_PATH = path.join(__dirname, '../data'); if(!fs.existsSync(DATA_PATH)) { console.log(`${DATA_PATH} doesn't exist, creating`); fs.mkdirSync(DATA_PATH); } export const CSV_PATH = path.join(DATA_PATH, 'csv'); if(!fs.existsSync(CSV_PATH)) { console.log(`${CSV_PATH} doesn't exist, creating`); fs.mkdirSync(CSV_PATH); } 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 val; } export const port = getConfigField('port', '8000');