@curvenote/cli
Version:
CLI Client library for Curvenote
48 lines (47 loc) • 1.6 kB
JavaScript
import chalk from 'chalk';
import inquirer from 'inquirer';
import { getFromJournals } from '../utils/api.js';
export async function ensureVenue(session, venue, opts = { action: 'submit' }) {
if (venue)
return venue;
if (opts?.yes) {
throw new Error(`⛔️ Site must be specified to continue`);
}
session.log.debug('No Site provided, prompting user...');
const answer = await inquirer.prompt([venueQuestion(session, opts.action)]);
return answer.venue;
}
export function venueQuestion(session, action = 'submit') {
return {
name: 'venue',
type: 'input',
message: `Enter the Site name you want to ${action} to?`,
filter: (venue) => venue.toLowerCase(),
validate: async (venue) => {
if (venue.length < 3) {
return 'Site name must be at least 3 characters';
}
try {
await getFromJournals(session, `/sites/${venue}`);
}
catch (err) {
return `Site "${venue}" not found.`;
}
return true;
},
};
}
/**
* Ensure that a `site` exists by performing a basic request to the Site
*/
export async function checkVenueExists(session, venue) {
try {
session.log.debug(`GET from journals API /sites/${venue}`);
await getFromJournals(session, `/sites/${venue}`);
session.log.debug(`found Site "${venue}"`);
}
catch {
session.log.error(`${chalk.red(`😟 Site "${venue}" not found.`)}`);
throw new Error(`Site "${venue}" not found.`);
}
}