amiddy
Version:
Middleware server with high configurability for development
1 lines • 1.5 kB
JavaScript
import config from"./config/index.js";import debug from"./debug.js";import setup from"./setup/index.js";import server from"./server.js";const privateApi={};privateApi.argsMapWithVal={c:"config",config:"config",t:"tokens",tokens:"tokens"},privateApi.argsMapWithoutVal={d:"debug",debug:"debug"},privateApi.getArgs=()=>process.argv.slice(2),privateApi.extractArgs=args=>{const acceptedArgsWithVal=Object.keys(privateApi.argsMapWithVal),argWithValRegExp=new RegExp(`^\\s?--?(${acceptedArgsWithVal.join("|")})=(.*)`),acceptedArgsWithoutVal=Object.keys(privateApi.argsMapWithoutVal),argWithoutValRegExp=new RegExp(`^\\s?--?(${acceptedArgsWithoutVal.join("|")}).*`);return args.reduce((acc,arg)=>{let matches=arg.match(argWithValRegExp);if(matches&&3===matches.length){const argKey=privateApi.argsMapWithVal[matches[1].trim()],argVal=matches[2].trim();argKey&&argVal&&(acc[argKey]=argVal)}else if(matches=arg.match(argWithoutValRegExp),matches&&2===matches.length){const argKey=privateApi.argsMapWithoutVal[matches[1].trim()];acc[argKey]=!0}return acc},{config:".amiddy"})};const service={run:()=>{const args=privateApi.getArgs(),filteredArgs=privateApi.extractArgs(args),hasDebugProp=Object.prototype.hasOwnProperty.call(filteredArgs,"debug");hasDebugProp&&debug.activate();const configObj=config.get(filteredArgs);debug.block("\nUsing options:",filteredArgs,"\nUsing configuration:",JSON.stringify(configObj)),setup.init(configObj),server.create(configObj)}};export{privateApi};export default service;