blodemd 0.0.14 → 0.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.mjs CHANGED
@@ -2556,6 +2556,19 @@ const createUploadBatches = async function* createUploadBatchesGenerator({ files
2556
2556
  };
2557
2557
  //#endregion
2558
2558
  //#region src/commands/push.ts
2559
+ const resolveAuthHeaders = async (apiKeyOption) => {
2560
+ const apiKey = (apiKeyOption ?? process.env["BLODEMD_API_KEY"])?.trim();
2561
+ if (apiKey) return {
2562
+ canAutoCreate: false,
2563
+ headers: { "x-admin-token": apiKey }
2564
+ };
2565
+ const resolved = await resolveAuthToken();
2566
+ if (!resolved?.token) throw new Error("Not logged in. Run \"blodemd login\" to authenticate, pass --api-key, or set BLODEMD_API_KEY.");
2567
+ return {
2568
+ canAutoCreate: true,
2569
+ headers: { Authorization: `Bearer ${resolved.token}` }
2570
+ };
2571
+ };
2559
2572
  const resolvePushConfig = async (config, options) => {
2560
2573
  const { project, usedLegacyNameFallback } = resolveProjectTarget({
2561
2574
  cliProject: options.project,
@@ -2563,7 +2576,6 @@ const resolvePushConfig = async (config, options) => {
2563
2576
  envProject: process.env[BLODE_PROJECT_ENV]
2564
2577
  });
2565
2578
  const apiUrl = options.apiUrl ?? process.env["BLODEMD_API_URL"] ?? "https://api.blode.md";
2566
- const authToken = (await resolveAuthToken())?.token;
2567
2579
  const branch = options.branch ?? process.env["BLODEMD_BRANCH"] ?? process.env.GITHUB_REF_NAME ?? readGitValue([
2568
2580
  "rev-parse",
2569
2581
  "--abbrev-ref",
@@ -2580,19 +2592,20 @@ const resolvePushConfig = async (config, options) => {
2580
2592
  if (usedLegacyNameFallback) throw new Error(`docs.json.name is not a valid deployment slug. Add "slug" to docs.json, pass --project, or set BLODEMD_PROJECT. ${projectSlugError}`);
2581
2593
  throw new Error(`Invalid project slug "${project}". ${projectSlugError}`);
2582
2594
  }
2583
- if (!authToken) throw new Error("Not logged in. Run \"blodemd login\" to authenticate.");
2595
+ const { headers: authHeaders, canAutoCreate } = await resolveAuthHeaders(options.apiKey);
2584
2596
  return {
2585
2597
  apiUrl,
2586
- authToken,
2598
+ authHeaders,
2587
2599
  branch,
2600
+ canAutoCreate,
2588
2601
  commitMessage,
2589
2602
  project,
2590
2603
  projectDisplayName: config.name?.trim() || project,
2591
2604
  usedLegacyNameFallback
2592
2605
  };
2593
2606
  };
2594
- const autoCreateProject = async (project, projectDisplayName, apiUrl, headers) => {
2595
- if (!(await readAuthFile())?.session) throw new Error(`Project "${project}" not found. Create it at blode.md or login with "blodemd login" to auto-create.`);
2607
+ const autoCreateProject = async (project, projectDisplayName, apiUrl, headers, canAutoCreate) => {
2608
+ if (!canAutoCreate) throw new Error(`Project "${project}" not found. Create it at blode.md or login with "blodemd login" to auto-create.`);
2596
2609
  const shouldCreate = await confirm({ message: `Project "${project}" doesn't exist. Create it?` });
2597
2610
  if (isCancel(shouldCreate) || !shouldCreate) return false;
2598
2611
  const createResult = await requestJson(new URL("/projects", apiUrl).toString(), {
@@ -2626,7 +2639,7 @@ const uploadFiles = async (files, root, apiPath, deploymentId, headers, s) => {
2626
2639
  s.stop(`Uploaded ${chalk.cyan(String(files.length))} files`);
2627
2640
  };
2628
2641
  const registerPushCommand = (program) => {
2629
- program.command("push").description("Deploy docs").argument("[dir]", "docs directory").option("--project <slug>", "project slug (env: BLODEMD_PROJECT)").option("--api-url <url>", "API URL (env: BLODEMD_API_URL)").option("--branch <name>", "git branch (env: BLODEMD_BRANCH)").option("--message <msg>", "deploy message (env: BLODEMD_COMMIT_MESSAGE)").action(async (dir, options) => {
2642
+ program.command("push").description("Deploy docs").argument("[dir]", "docs directory").option("--project <slug>", "project slug (env: BLODEMD_PROJECT)").option("--api-key <token>", "API key (env: BLODEMD_API_KEY)").option("--api-url <url>", "API URL (env: BLODEMD_API_URL)").option("--branch <name>", "git branch (env: BLODEMD_BRANCH)").option("--message <msg>", "deploy message (env: BLODEMD_COMMIT_MESSAGE)").action(async (dir, options) => {
2630
2643
  intro(chalk.bold("blodemd push"));
2631
2644
  const s = spinner();
2632
2645
  try {
@@ -2635,14 +2648,14 @@ const registerPushCommand = (program) => {
2635
2648
  const { config, warnings } = await loadValidatedSiteConfig(root);
2636
2649
  s.stop("Configuration valid");
2637
2650
  for (const warning of warnings) log.warn(warning);
2638
- const { project, projectDisplayName, apiUrl, authToken, branch, commitMessage, usedLegacyNameFallback } = await resolvePushConfig(config, options);
2651
+ const { project, projectDisplayName, apiUrl, authHeaders, canAutoCreate, branch, commitMessage, usedLegacyNameFallback } = await resolvePushConfig(config, options);
2639
2652
  if (usedLegacyNameFallback) log.warn(LEGACY_PROJECT_NAME_FALLBACK_WARNING);
2640
2653
  s.start("Collecting files");
2641
2654
  const files = await collectFiles(root);
2642
2655
  if (files.length === 0) throw new Error("No files found to deploy.");
2643
2656
  s.stop(`Found ${chalk.cyan(String(files.length))} files`);
2644
2657
  const headers = {
2645
- Authorization: `Bearer ${authToken}`,
2658
+ ...authHeaders,
2646
2659
  "Content-Type": "application/json"
2647
2660
  };
2648
2661
  const apiPath = (suffix) => new URL(`/projects/slug/${project}/deployments${suffix}`, apiUrl).toString();
@@ -2661,7 +2674,7 @@ const registerPushCommand = (program) => {
2661
2674
  } catch (error) {
2662
2675
  if (!(error instanceof Error ? error.message : "").includes("404")) throw error;
2663
2676
  s.stop("Project not found");
2664
- if (!await autoCreateProject(project, projectDisplayName, apiUrl, headers)) {
2677
+ if (!await autoCreateProject(project, projectDisplayName, apiUrl, headers, canAutoCreate)) {
2665
2678
  log.info("Cancelled");
2666
2679
  return;
2667
2680
  }