@mintlify/previewing
Version:
Preview Mintlify docs locally
48 lines (47 loc) • 2.09 kB
JavaScript
import open from 'better-opn';
import Chalk from 'chalk';
import express from 'express';
import { createServer } from 'http';
import { Server as SocketServer } from 'socket.io';
import { NEXT_PUBLIC_PATH } from '../constants.js';
import { maybeFixMissingWindowsEnvVar } from '../util.js';
import listener from './listener/index.js';
import { setupNext } from './setupNext.js';
export const run = async (argv) => {
const port = argv.port || '3000';
const currentPort = parseInt(port, 10) || 3000;
const app = express();
const server = createServer(app);
const io = new SocketServer(server);
const requestHandler = await setupNext();
// next-server is bugged, public files added after starting aren't served
app.use('/', express.static(NEXT_PUBLIC_PATH));
app.all('*', (req, res) => requestHandler(req, res));
const onChange = () => {
io.emit('reload');
};
server.listen(currentPort, () => {
console.log(`${Chalk.green(`Your local preview is available at http://localhost:${port}`)}`);
// Note: We wait for this exact text to be sure the server is ready in the cli e2e test,
// if it changes, the test will fail/require an update.
console.log(`${Chalk.green('Press Ctrl+C any time to stop the local preview.')}`);
/**
* We're running into a known bug with the `open` package, where Windows machines error out because process.env.SYSTEMROOT is not set:
* https://github.com/sindresorhus/open/issues/292
*
* Let's use the same workaround that this project did:
* https://github.com/sanity-io/sanity/pull/4221/files#diff-aeb574e1becf61f21fdf87fbea709669c93d604d660dad4b0f9e24527a2fb54bR256-R262
*/
maybeFixMissingWindowsEnvVar();
if (argv.open) {
void open(`http://localhost:${port}`);
}
// exit with successful status
const onExit = () => {
process.exit(0);
};
process.on('SIGINT', onExit);
process.on('SIGTERM', onExit);
});
listener(onChange);
};