UNPKG

@mintlify/previewing

Version:

Preview Mintlify docs locally

74 lines (73 loc) 2.69 kB
import fse from 'fs-extra'; import got from 'got'; import isOnline from 'is-online'; import { pipeline } from 'node:stream/promises'; import tar from 'tar'; import { DOT_MINTLIFY, DOT_MINTLIFY_LAST, VERSION_PATH, TAR_PATH, TARGET_MINT_VERSION_URL, } from '../constants.js'; import { restoreMintlifyLast, getTarUrl } from '../util.js'; export const getTargetMintVersion = async (logger) => { const hasInternet = await isOnline(); if (!hasInternet) { return undefined; } try { const response = await got(TARGET_MINT_VERSION_URL); return response.body; } catch (error) { logger.text = `Failed to fetch the latest Mintlify version: ${error instanceof Error ? error.message : 'Unknown error'}`; } return undefined; }; export const downloadTargetMint = async ({ logger, targetVersion, existingVersion, }) => { if (fse.existsSync(DOT_MINTLIFY)) { fse.moveSync(DOT_MINTLIFY, DOT_MINTLIFY_LAST, { overwrite: true }); } fse.ensureDirSync(DOT_MINTLIFY); logger.text = 'Downloading Mintlify framework...'; const tarUrl = getTarUrl(targetVersion); let currentVersion = targetVersion.trim(); try { await pipeline(got.stream(tarUrl), fse.createWriteStream(TAR_PATH)); } catch (error) { if (existingVersion) { logger.warn(`Failed to download Mintlify framework version ${currentVersion}, ${error}, falling back to existing version: ${existingVersion}`); currentVersion = existingVersion; restoreMintlifyLast(); return; } else { logger.fail(`Failed to download Mintlify framework version ${currentVersion}, ${error}`); process.exit(1); } } try { logger.text = 'Extracting Mintlify framework...'; tar.x({ sync: true, file: TAR_PATH, cwd: DOT_MINTLIFY, onwarn: (_code, message, _data) => { throw new Error(message); }, }); } catch (error) { if (existingVersion) { logger.warn(`Failed to extract Mintlify framework version ${currentVersion}, ${error}, using existing version: ${existingVersion}`); currentVersion = existingVersion; restoreMintlifyLast(); return; } else { logger.fail(`Failed to extract Mintlify framework version ${currentVersion}, ${error}`); process.exit(1); } } fse.removeSync(TAR_PATH); if (fse.existsSync(DOT_MINTLIFY_LAST)) { fse.removeSync(DOT_MINTLIFY_LAST); } fse.writeFileSync(VERSION_PATH, currentVersion); };