@mintlify/cli
Version:
The Mintlify CLI
94 lines (93 loc) • 4.37 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { getConfigPath } from '@mintlify/prebuild';
import { MintConfigUpdater } from '@mintlify/prebuild';
import { upgradeToDocsConfig } from '@mintlify/validation';
import Chalk from 'chalk';
import detect from 'detect-port';
import fse from 'fs-extra';
import fs from 'fs/promises';
import inquirer from 'inquirer';
import Ora from 'ora';
import path from 'path';
import { CMD_EXEC_PATH } from './constants.js';
export const checkPort = (argv) => __awaiter(void 0, void 0, void 0, function* () {
const initialPort = typeof argv.port === 'number' ? argv.port : 3000;
if (initialPort === (yield detect(initialPort)))
return initialPort;
for (let port = initialPort + 1; port < initialPort + 10; port++) {
console.log(`Port ${port - 1} is already in use. Trying ${port} instead.`);
if (port === (yield detect(port)))
return port;
}
});
export const checkNodeVersion = () => __awaiter(void 0, void 0, void 0, function* () {
let nodeVersionString = process.version;
if (nodeVersionString.charAt(0) === 'v') {
nodeVersionString = nodeVersionString.slice(1);
}
const versionArr = nodeVersionString.split('.');
const majorVersion = parseInt(versionArr[0], 10);
if (majorVersion < 18) {
const logger = buildLogger();
logger.fail(`Mintlify requires a node version >= 18.0.0 (current version ${nodeVersionString}). Try removing the mintlify package, upgrading node, reinstalling mintlify, and running again.`);
process.exit(1);
}
});
export const buildLogger = (startText = '') => {
const logger = Ora().start(startText);
return logger;
};
export const checkForMintJson = () => __awaiter(void 0, void 0, void 0, function* () {
return !!(yield getConfigPath(CMD_EXEC_PATH, 'mint'));
});
export const checkForDocsJson = () => __awaiter(void 0, void 0, void 0, function* () {
const docsJsonPath = path.join(CMD_EXEC_PATH, 'docs.json');
if (!(yield fse.pathExists(docsJsonPath))) {
console.log('New docs.json file is available.');
const promptResult = yield inquirer.prompt([
{
type: 'list',
name: 'action',
message: 'Would you like to upgrade your mint.json to docs.json?',
choices: [
{ name: 'Continue (use existing mint.json)', value: 'continue' },
{ name: 'Upgrade (migrate from mint.json -> docs.json)', value: 'upgrade' },
],
},
]);
const { action } = promptResult;
if (action === 'continue') {
console.log('Proceeding with the existing mint.json...');
}
if (action === 'upgrade') {
console.log('Upgrading docs.json...');
yield upgradeConfig();
}
}
});
export const upgradeConfig = () => __awaiter(void 0, void 0, void 0, function* () {
try {
const mintJsonPath = path.join(CMD_EXEC_PATH, 'mint.json');
const docsJsonPath = path.join(CMD_EXEC_PATH, 'docs.json');
const mintJsonFileContent = yield fs.readFile(mintJsonPath, 'utf8');
const validationResult = yield MintConfigUpdater.validateConfigJsonString(mintJsonFileContent);
const mintConfig = validationResult.data;
const upgradedDocsConfig = upgradeToDocsConfig(mintConfig, {
shouldUpgradeTheme: true,
});
yield fs.writeFile(docsJsonPath, JSON.stringify(upgradedDocsConfig, null, 2));
console.log(Chalk.green('✅ Your mint.json file has been upgraded to v2 (docs.json).'));
}
catch (err) {
console.error(Chalk.red(err));
process.exit(1);
}
});