Commit c21f0998 authored by Jiwon Kwon's avatar Jiwon Kwon
Browse files

refactor: create file when output path is given and fetching jobs are successfully done

parent 1397a76e
Loading
Loading
Loading
Loading
+27 −27
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ import {
  traverseCollection,
} from "@fedify/fedify";
import { getLogger } from "@logtape/logtape";
import path from "node:path/win32";
import { dirname, isAbsolute, resolve } from "@std/path";
import util from "node:util";
import ora from "ora";
import { getContextLoader, getDocumentLoader } from "./docloader.ts";
@@ -81,21 +81,6 @@ export const command = new Command()
      );
      Deno.exit(1);
    }
    if (options.output) {
      try {
        const parentDir = path.dirname(options.output);
        await Deno.mkdir(parentDir, { recursive: true });

        const file = await Deno.create(options.output);
        file.close();
        console.log(
          `Output will be written to ${colors.green(options.output)}.`,
        );
      } catch (err) {
        console.error("Unexpected error while checking output:", err);
        Deno.exit(1);
      }
    }

    const spinner = ora({
      text: `Looking up the ${
@@ -315,8 +300,19 @@ export const command = new Command()
          : "Successfully fetched the object.",
      );
    }
    // If output is specified and fetching objects are successful, write or overwrite the contents to the file.
    if (options.output && success) {
      try {
        spinner.start(`Writing output to ${colors.green(options.output)}...`);

        const outputPath = isAbsolute(options.output)
          ? options.output
          : resolve(Deno.env.get("PWD") || Deno.cwd(), options.output);
        const parentDir = dirname(outputPath);
        await Deno.mkdir(parentDir, { recursive: true });
        const file = await Deno.create(options.output);
        file.close();

    if (options.output && fileContents.length > 0) {
        const output = fileContents.map((item) =>
          item instanceof Object
            ? util.inspect(item, {
@@ -327,7 +323,11 @@ export const command = new Command()
        ).join(options.separator);

        await Deno.writeTextFile(options.output, output);
      console.log(`Output written to ${colors.green(options.output)}.`);
        spinner.succeed(`Output written to ${colors.green(options.output)}.`);
      } catch (err) {
        console.error("Unexpected error occurred while writing output:", err);
        Deno.exit(1);
      }
    }
    await server?.close();
    if (!success) {