@mintlify/previewing
Version:
Preview Mintlify docs locally
62 lines (61 loc) • 3.04 kB
JavaScript
import { prebuild } from '@mintlify/prebuild';
import fse, { pathExists } from 'fs-extra';
import isOnline from 'is-online';
import { CLIENT_PATH, DOT_MINTLIFY, CMD_EXEC_PATH, VERSION_PATH, NEXT_PUBLIC_PATH, NEXT_PROPS_PATH, } from '../constants.js';
import { buildLogger } from '../util.js';
import { getTargetMintVersion, downloadTargetMint } from './client.js';
import { run } from './run.js';
const dev = async (argv) => {
// Note: We wait for specific text in the logger to be sure the server is ready when we e2e test the cli.
// If the cli output does not exactly match:
// "- Preparing local Mintlify instance...\n✔ Local Mintlify instance is ready. Launching your site...\nYour local preview is available at http://localhost:3000\nPress Ctrl+C any time to stop the local preview."
// the test will fail/require an update.
const logger = buildLogger('Preparing local Mintlify instance...');
const hasInternet = await isOnline();
const localSchema = argv['local-schema'];
const clientVersion = argv['client-version'];
const packageName = argv.packageName;
await fse.ensureDir(DOT_MINTLIFY);
const versionString = (await pathExists(VERSION_PATH))
? fse.readFileSync(VERSION_PATH, 'utf8')
: null;
if (!versionString && !hasInternet) {
logger.fail(`Running ${packageName} dev after updating requires an internet connection.`);
process.exit(1);
}
const targetMintVersion = await getTargetMintVersion(logger);
if (!targetMintVersion) {
logger.stopAndPersist({ symbol: '✓' });
logger.warn(`Failed to get latest Mintlify client version. Your current version is: ${versionString?.trim()}, which may not be the latest Mintlify client version.`);
}
// update the client if the user has provided a version with --client-version
// or if there is no version and there is internet and the target version is available
if (clientVersion !== undefined || (!versionString && hasInternet && targetMintVersion)) {
const version = clientVersion ?? targetMintVersion;
if (version) {
await downloadTargetMint({
logger,
targetVersion: version,
existingVersion: versionString,
});
}
}
if (versionString && targetMintVersion && versionString.trim() !== targetMintVersion.trim()) {
logger.warn(`An update is available. Run \`${packageName} update\` to update to the latest version.`);
}
// clear preexisting prebuild files
fse.emptyDirSync(NEXT_PUBLIC_PATH);
fse.emptyDirSync(NEXT_PROPS_PATH);
process.chdir(CLIENT_PATH);
try {
await prebuild(CMD_EXEC_PATH, { localSchema });
}
catch (err) {
const errorText = err instanceof Error && err.message ? err.message : 'Prebuild step failed';
logger.fail(errorText);
process.exit(1);
}
logger.succeed('Local Mintlify instance is ready. Launching your site...');
await run(argv);
};
export default dev;