UNPKG

@curvenote/cli

Version:
42 lines (41 loc) 1.79 kB
import chalk from 'chalk'; import { checkVenueExists, ensureVenue } from './utils.js'; import { postToJournals } from '../utils/api.js'; import { tic } from 'myst-cli-utils'; import { getMyWorkFromKey, workKeyFromConfig } from '../works/utils.js'; export async function patchSiteWithContent(session, siteName, content) { const toc = tic(); session.log.debug(`PATCH to ${session.config?.apiUrl}/sites/${siteName} with work content: ${content}...`); const resp = await postToJournals(session, `/sites/${siteName}`, { content }, { method: 'PATCH' }); session.log.debug(`${resp.status} ${resp.statusText}`); if (resp.ok) { const json = (await resp.json()); session.log.info(toc(`🔧 Updated Site landing content in %s.`)); session.log.debug(`Site name: ${siteName}`); return json; } else { const message = (await resp.json())?.message; throw new Error(`Patching Site failed${message ? `: ${message}` : ''}`); } } export async function init(session, name, opts) { if (!opts?.setContent) { session.log.error(`📣 ${chalk.bold.red('Cannot initialize new Site from CLI.')}`); session.log.info('📨 Please contact support@curvenote.com'); process.exit(1); } name = await ensureVenue(session, name, { ...opts, action: 'set landing content' }); await checkVenueExists(session, name); let content; const key = workKeyFromConfig(session); if (key) { const work = await getMyWorkFromKey(session, key); content = work?.id; } if (!content) { session.log.error(`📣 ${chalk.bold.red('Unable to find work; you may need to run "curvenote work push"')}`); process.exit(1); } await patchSiteWithContent(session, name, content); }