UNPKG

mew-time

Version:
79 lines (63 loc) 2.05 kB
import * as SERVER from "../constant/server.const"; const hasOwnProperty = (target, name) => { return Object.prototype.hasOwnProperty.call(target, name); }; const stringToBoolean = (name) => { if (typeof name === "boolean") { return name; } if (typeof name === "string") { switch (name.trim()) { case "true": return true; case "false": return false; default: return false; } } return false; }; export default function environment(conf) { const env = (() => { const result = { name: "", dev: false, prod: false, test: false }; if (conf.dev) { result.name = "development"; result.dev = true; return result; } if (conf.prod) { result.name = "production"; result.prod = true; return result; } if (conf.test) { result.name = "test"; result.test = true; return result; } return result; })(); const status = (() => { if (!["dev", "beta", "staging", "live"].includes(conf.status)) { throw new Error("The env.status is invalid."); } const result = { name: "", dev: false, beta: false, staging: false, live: false }; result.name = conf.status; result[conf.status] = true; return result; })(); const domain = (() => { const info = SERVER[status.name.toUpperCase()]; return { host_addr: !status.dev || env.prod ? `http://${info.host}` : `http://${info.host}:${info.port.livereload}`, host: info.host, port: info.port, server: info.server }; })(); this._conf = { env, status, domain, sourcemap: hasOwnProperty(conf, "sourcemap") ? stringToBoolean(conf.sourcemap) : env.dev, minify: hasOwnProperty(conf, "minify") ? stringToBoolean(conf.minify) : env.prod }; }