UNPKG

firejsx

Version:

The React Framework for SSB, SSR and Serverless technologies

86 lines (85 loc) 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseConfig = exports.getUserConfig = void 0; const path_1 = require("path"); const yaml_1 = require("yaml"); const fs_extra_1 = require("fs-extra"); const fs_1 = require("fs"); function getUserConfig(path) { //check if path was given if (path) if (fs_1.existsSync(path = path_1.resolve(path))) if (path.endsWith(".yml")) return [path, yaml_1.parse(fs_1.readFileSync(path, "utf8").toString()) || {}]; else if (path.endsWith(".js")) return [path, require(path).default || {}]; else throw new Error("Unknown config file type. Expected [.js, .yml]"); else throw new Error(`Config not found at ${path}`); //if not then check defaults else if (fs_1.existsSync(path = path_1.resolve("firejsx.yml"))) return [path, yaml_1.parse(fs_1.readFileSync(path, "utf8").toString()) || {}]; else if (fs_1.existsSync(path = path_1.resolve("firejsx.js"))) return [path, require(path).default || {}]; else return ["default", {}]; } exports.getUserConfig = getUserConfig; function parseConfig(config = {}, args = { _: [] }, cli) { config.paths = config.paths || {}; config.devServer = config.devServer || {}; const out = makeDirIfNotFound(path_1.resolve(args["--out"] || config.paths.out || "out")); const staticDir = undefinedIfNotFound(config.paths.static || "src/static"); const prefix = args["--prefix"] || config.prefix || ""; const pages = throwIfNotFound("pages dir", path_1.resolve(args["--pages"] || config.paths.pages || "src/pages"), cli); return { outDir: makeDirIfNotFound(path_1.resolve(args["--disk"] ? config.paths.disk || `${out}/disk` : args["--export"] ? args["--dist"] || config.paths.dist || `${out}/dist` : args["--export-fly"] ? args["--fly"] || config.paths.fly || `${out}/fly` : config.paths.disk || `${out}/disk`)), cacheDir: makeDirIfNotFound(path_1.resolve(args["--cache"] || config.paths.cache || `${out}/.cache`)), pages, staticDir, lib: config.lib || "lib", prefix, staticPrefix: args["--static-prefix"] || config.staticPrefix || (staticDir ? (() => { const dirName = staticDir.substring(staticDir.lastIndexOf("/")); return prefix === "" ? dirName : (prefix + dirName); })() : ""), devServer: { gzip: args["--disable-gzip"] ? false : config.devServer.gzip === undefined ? true : config.devServer.gzip }, custom: config.custom || {}, plugins: args["--disable-plugins"] ? [] : config.plugins || [], app: (() => { if (config.paths.app) return throwIfNotFound("app file", path_1.resolve(config.paths.app), cli); const possible_app_path = path_1.join(pages, "../App.jsx"); if (fs_1.existsSync(possible_app_path)) { cli.warn("App.jsx found outside pages dir. It is a special file, make sure it is not a regular file.\n Rename it or set paths->app path in config file."); return possible_app_path; } return path_1.join(__dirname, "../web/App.js"); })() }; } exports.parseConfig = parseConfig; function throwIfNotFound(name, pathTo, cli) { if (!fs_1.existsSync(pathTo)) { cli.error(new Error(`${name} not found. ${pathTo}`)); process.exit(1); } return pathTo; } function undefinedIfNotFound(path) { return fs_1.existsSync(path) ? path : undefined; } function makeDirIfNotFound(path) { if (!fs_1.existsSync(path)) fs_extra_1.mkdirp(path, e => { if (e) throw e; }); return path; }