UNPKG

@curvenote/cli

Version:
214 lines (213 loc) 8.69 kB
import { tic } from 'myst-cli-utils'; import format from 'date-fns/format'; export function formatDate(date) { return format(new Date(date), 'dd MMM, yyyy HH:mm:ss'); } /** * Perform json GET request to `url` * * If request is successful, return the response json. * If request fails, throw an error. */ export async function getFromUrl(session, url) { session.log.debug('Getting from', url); const headers = await session.getHeaders(); const response = await session.fetch(url, { headers: { 'Content-Type': 'application/json', ...headers, }, }); if (response.ok) { const json = (await response.json()); return json; } else { session.log.debug('GET FAILED', url, response.status, response.statusText); throw new Error(`GET FAILED ${url}: ${response.status}\n\n${response.statusText} Please contact support@curvenote.com`); } } /** * Perform json GET request to `pathname` on the journals API * * If request is successful, return the response json. * If request fails, throw an error. */ export async function getFromJournals(session, pathname) { // TODO this could/should now just use session.get? and so const url = `${session.config.apiUrl}${pathname}`; return getFromUrl(session, url); } export async function postToUrl(session, url, body, opts = {}) { var _a, _b; session.log.debug(`${(_a = opts === null || opts === void 0 ? void 0 : opts.method) !== null && _a !== void 0 ? _a : 'POST'}ing to`, url); const method = (_b = opts === null || opts === void 0 ? void 0 : opts.method) !== null && _b !== void 0 ? _b : 'POST'; const headers = await session.getHeaders(); return session.fetch(url, { method, body: JSON.stringify(body), headers: { 'Content-Type': 'application/json', ...headers, }, }); } export async function postToJournals(session, pathname, body, opts = {}) { var _a; const url = `${(_a = session.config) === null || _a === void 0 ? void 0 : _a.apiUrl}${pathname}`; const resp = await postToUrl(session, url, body, opts); return resp; } export async function postNewWork(session, cdnKey, cdn, key) { var _a, _b; const toc = tic(); session.log.debug(`POST to ${(_a = session.config) === null || _a === void 0 ? void 0 : _a.apiUrl}/works with cdnKey: ${cdnKey}, cdn: ${cdn}${key ? `, key: ${key}` : ''}...`); const resp = await postToJournals(session, '/works', { cdn_key: cdnKey, cdn, key }); session.log.debug(`${resp.status} ${resp.statusText}`); if (resp.ok) { const json = (await resp.json()); const { id, version_id } = json; session.log.info(toc(`🚀 Created a new work in %s.`)); session.log.debug(`CDN key: ${cdnKey}`); session.log.debug(`Work Id: ${id}`); session.log.debug(`Work Version Id: ${version_id}`); return json; } else { const message = (_b = ((await resp.json()))) === null || _b === void 0 ? void 0 : _b.message; throw new Error(`Posting new work failed${message ? `: ${message}` : ''}`); } } export async function postNewWorkVersion(session, versionsUrl, cdnKey, cdn) { const toc = tic(); session.log.debug(`POST to ${versionsUrl} with cdnKey: ${cdnKey} and cdn: ${cdn}...`); const resp = await postToUrl(session, `${versionsUrl}`, { cdn_key: cdnKey, cdn }); session.log.debug(`${resp.status} ${resp.statusText}`); if (resp.ok) { const json = (await resp.json()); const { id, version_id } = json; session.log.info(toc(`🚀 Created a new work version in %s.`)); session.log.debug(`CDN key: ${cdnKey}`); session.log.debug(`Work Id: ${id}`); session.log.debug(`Work Version Id: ${version_id}`); return json; } else { throw new Error('Posting new version of the work failed'); } } export async function postNewCliCheckJob(session, payload, results) { var _a; const toc = tic(); const body = { job_type: 'CLI_CHECK', payload, results, }; session.log.debug(`POST to ${(_a = session.config) === null || _a === void 0 ? void 0 : _a.apiUrl}/jobs...`); const resp = await postToJournals(session, `/jobs`, body); session.log.debug(`${resp.status} ${resp.statusText}`); if (resp.ok) { const json = (await resp.json()); session.log.info(toc(`🎉 Posted a new job in %s.`)); session.log.debug(`Job id: ${json.id}`); session.log.debug(`Job status: ${json.status}`); return json; } else { throw new Error('Job creation failed'); } } export async function patchUpdateCliCheckJob(session, jobId, status, message, results) { var _a; const toc = tic(); const body = { status, message, results, }; session.log.debug(`PATCH to ${(_a = session.config) === null || _a === void 0 ? void 0 : _a.apiUrl}/jobs...`); const resp = await postToJournals(session, `/jobs/${jobId}`, body, { method: 'PATCH' }); session.log.debug(`${resp.status} ${resp.statusText}`); if (resp.ok) { const json = (await resp.json()); session.log.info(toc(`🎉 Updated a job in %s.`)); session.log.debug(`Job id: ${json.id}`); session.log.debug(`Job status: ${json.status}`); return json; } else { throw new Error('Job update failed'); } } export async function postNewSubmission(session, venue, collection_id, kind_id, work_version_id, draft, job_id) { var _a, _b; const toc = tic(); const submissionRequest = { work_version_id, collection_id, kind_id, draft, job_id, }; session.log.debug(`POST to ${(_a = session.config) === null || _a === void 0 ? void 0 : _a.apiUrl}/sites/${venue}/submissions...`); const resp = await postToJournals(session, `/sites/${venue}/submissions`, submissionRequest); session.log.debug(`${resp.status} ${resp.statusText}`); if (resp.ok) { const json = (await resp.json()); session.log.info(toc(`🚀 Submitted to venue "${venue}" in %s.`)); session.log.debug(`Submission id: ${json.id}`); session.log.debug(`Submitted by: ${(_b = json.submitted_by.name) !== null && _b !== void 0 ? _b : json.submitted_by.id}`); return json; } else { throw new Error('Creating new submission failed'); } } export async function postUpdateSubmissionWorkVersion(session, venue, versionsUrl, work_version_id, job_id) { var _a; const toc = tic(); const submissionRequest = { work_version_id, job_id }; session.log.debug(`POST to ${versionsUrl}...`); const resp = await postToUrl(session, versionsUrl, submissionRequest); session.log.debug(`${resp.status} ${resp.statusText}`); if (resp.ok) { const json = (await resp.json()); session.log.info(toc(`🚀 Updated submission accepted by "${venue}" in %s.`)); session.log.debug(`Submission id: ${json.submission_id}`); session.log.debug(`Submitted by: ${(_a = json.submitted_by.name) !== null && _a !== void 0 ? _a : json.submitted_by.id}`); return json; } else { throw new Error('Updating submission failed'); } } export async function patchUpdateSubmissionStatus(session, venue, submissionUrl, action, date) { var _a; const toc = tic(); session.log.debug(`GET to ${submissionUrl}...`); const submissionJson = await getFromUrl(session, submissionUrl); const updateUrl = (_a = submissionJson === null || submissionJson === void 0 ? void 0 : submissionJson.links) === null || _a === void 0 ? void 0 : _a[action]; if (!updateUrl) { throw new Error(`Action "${action}" not available for submission`); } session.log.debug(`PUT to ${updateUrl}...`); const resp = await postToUrl(session, updateUrl, { date }, { method: 'PUT' }); session.log.debug(`${resp.status} ${resp.statusText}`); if (resp.ok) { const json = (await resp.json()); session.log.info(toc(`🚀 Submission successfully ${action === 'publish' ? 'publishing to' : 'unpublishing from'} "${venue}" in %s.`)); session.log.debug(`Submission id: ${json.id}`); } else { throw new Error(`Submission failed to ${action}`); } } export function exitOnInvalidKeyOption(session, key) { session.log.debug(`Checking for valid key option: ${key}`); if (key.length < 8 || key.length > 128) { session.log.error(`⛔️ The key must be between 8 and 128 characters long.`); process.exit(1); } }