UNPKG

@lunora/cli

Version:

The Lunora CLI: init, dev, deploy, codegen, run, reset, and migrate commands

66 lines (63 loc) 2.09 kB
import { existsSync, rmSync } from 'node:fs'; import { join } from 'node:path'; import { writeLinkedProject, LINKED_PROJECT_FILE } from '@lunora/config'; import { d as defineHandler } from '../packem_shared/command-lYnl4QyF.mjs'; import { r as readWranglerName } from '../packem_shared/wrangler-name-cy4yhm9j.mjs'; const isValidWorkerUrl = (value) => { try { const { protocol } = new URL(value); return protocol === "http:" || protocol === "https:"; } catch { return false; } }; const runLinkRemove = (cwd, logger) => { const path = join(cwd, LINKED_PROJECT_FILE); if (!existsSync(path)) { logger.warn(`link: no ${LINKED_PROJECT_FILE} to remove`); return { code: 0 }; } rmSync(path); logger.success(`link: removed ${LINKED_PROJECT_FILE}`); return { code: 0 }; }; const runLinkCommand = (options) => { const cwd = options.cwd ?? process.cwd(); const { logger } = options; if (options.remove) { return runLinkRemove(cwd, logger); } if (options.url === void 0 || options.url === "") { logger.error("link requires a deployed Worker URL. Usage: lunora link --url <https://your-worker>"); return { code: 1 }; } if (!isValidWorkerUrl(options.url)) { logger.error(`link: invalid --url "${options.url}" — expected an http(s) URL`); return { code: 1 }; } const now = options.now ?? (() => (/* @__PURE__ */ new Date()).toISOString()); const link = { env: options.env, linkedAt: now(), workerName: options.name ?? readWranglerName(cwd), workerUrl: options.url }; const path = writeLinkedProject(cwd, link); logger.success(`link: ${link.workerName ?? "(unnamed worker)"} -> ${options.url}`); logger.info(`link: wrote ${path}`); if (link.env !== void 0) { logger.info(`link: environment "${link.env}"`); } return { code: 0, link }; }; const execute = defineHandler( ({ cwd, logger, options }) => runLinkCommand({ cwd, env: options.env, logger, name: options.name, remove: options.remove === true, url: options.url }) ); export { execute, runLinkCommand };