Skip to content
← Public packages

@kentcdodds/photo-portfolio

A fast, photo-first portfolio site you update by chatting with your agent: galleries, about, contact, and one-step rollback.

FollowFork

Files

publish-changes.ts

58 lines · 1.7 KB · TypeScript
import { packageStorage } from 'kody:runtime'
import * as s from 'remix/data-schema'
import { longText, parseInput, strictObject } from './domain/validation.ts'
import { nowIso, siteLinks } from './site-links.ts'
import { pendingChanges, publishDraft, readDraft } from './store/site-store.ts'

export type PublishChangesInput = { note?: string; dryRun?: boolean }

const publishInput = strictObject({
	note: s.optional(longText(200)),
	dryRun: s.optional(s.boolean()),
})

/**
 * Make the draft live and save it as a numbered version (for rollback).
 * Use `dryRun: true` to show her what will change first.
 *
 * @param raw - `{ note?: string, dryRun?: boolean }`
 * @returns `{ published, version?, changes, liveUrl }`. `published: false` when dryRun or nothing changed.
 *
 * @example
 * import publishChanges from 'kody:@kentcdodds/photo-portfolio/publish-changes'
 * await publishChanges({ note: 'Added Smith family gallery' })
 */
export default async function publishChanges(raw: PublishChangesInput = {}) {
	const input = parseInput<PublishChangesInput>(
		publishInput,
		raw,
		'publish-changes',
	)
	const storage = packageStorage()
	const changes = await pendingChanges(storage)
	const { liveUrl } = await siteLinks(
		storage,
		(await readDraft(storage)).config,
	)
	if (input.dryRun || !changes.hasChanges) {
		return {
			published: false,
			changes,
			liveUrl,
			next: changes.hasChanges
				? 'Call again without dryRun to publish.'
				: 'Nothing to publish: the draft matches the live site.',
		}
	}
	const result = await publishDraft(storage, {
		note: input.note,
		now: nowIso(),
	})
	return {
		published: true,
		version: result.entry.version,
		changes,
		liveUrl,
		cleanedUpPhotos: result.cleanup.deletedPhotos,
	}
}