frontity
Version:
Frontity cli and entry point to other packages
52 lines (51 loc) • 1.93 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const utils_1 = require("../utils");
const choosePort_1 = __importDefault(require("../utils/choosePort"));
const HOST = process.env.HOST || "0.0.0.0";
/**
* Start a server in development mode.
*
* This function is executed by the CLI when running the `npx frontity dev`
* command.
*
* @param options - Object of type {@link DevOptions}.
*/
const devCommand = async ({ production = false, port = 3000, https = false, target = "module", dontOpenBrowser = false, publicPath = "/static/", analyze = false, }) => {
// Try getting the `dev` function from `@frontity/core`.
let dev;
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
dev = require("@frontity/core").dev;
}
catch (error) {
const message = `Make sure that you are running ${chalk_1.default.green("frontity")} inside a Frontity project.\n` +
`If so try installing ${chalk_1.default.green("@frontity/core")} again with ${chalk_1.default.green("npm i @frontity/core")}.\n`;
(0, utils_1.errorLogger)(error, message);
}
// Generate options for the core's `dev` function.
const options = {
mode: production ? "production" : "development",
port,
isHttps: !!https,
target,
openBrowser: !dontOpenBrowser,
publicPath,
analyze,
};
try {
const port = await (0, choosePort_1.default)(HOST, options.port);
if (port === null) {
return;
}
await dev(Object.assign(Object.assign({}, options), { port }));
}
catch (error) {
(0, utils_1.errorLogger)(error);
}
};
exports.default = devCommand;