Jacky Zhao
2025-03-09 9c8fec06d2b58e4e3bbe280ddc665a99fcc4878c
quartz/cli/handlers.js
@@ -15,6 +15,7 @@
import { randomUUID } from "crypto"
import { Mutex } from "async-mutex"
import { CreateArgv } from "./args.js"
import { globby } from "globby"
import {
  exitIfCancel,
  escapePath,
@@ -33,13 +34,22 @@
} from "./constants.js"
/**
 * Resolve content directory path
 * @param contentPath path to resolve
 */
function resolveContentPath(contentPath) {
  if (path.isAbsolute(contentPath)) return path.relative(cwd, contentPath)
  return path.join(cwd, contentPath)
}
/**
 * Handles `npx quartz create`
 * @param {*} argv arguments for `create`
 */
export async function handleCreate(argv) {
  console.log()
  intro(chalk.bgGreen.black(` Quartz v${version} `))
  const contentFolder = path.join(cwd, argv.directory)
  const contentFolder = resolveContentPath(argv.directory)
  let setupStrategy = argv.strategy?.toLowerCase()
  let linkResolutionStrategy = argv.links?.toLowerCase()
  const sourceDirectory = argv.source
@@ -290,8 +300,8 @@
    }
    if (cleanupBuild) {
      await cleanupBuild()
      console.log(chalk.yellow("Detected a source code change, doing a hard rebuild..."))
      await cleanupBuild()
    }
    const result = await ctx.rebuild().catch((err) => {
@@ -355,6 +365,15 @@
              source: "**/*.*",
              headers: [{ key: "Content-Disposition", value: "inline" }],
            },
            {
              source: "**/*.webp",
              headers: [{ key: "Content-Type", value: "image/webp" }],
            },
            // fixes bug where avif images are displayed as text instead of images (future proof)
            {
              source: "**/*.avif",
              headers: [{ key: "Content-Type", value: "image/avif" }],
            },
          ],
        })
        const status = res.statusCode
@@ -423,13 +442,12 @@
      ),
    )
    console.log("hint: exit with ctrl+c")
    const paths = await globby(["**/*.ts", "**/*.tsx", "**/*.scss", "package.json"])
    chokidar
      .watch(["**/*.ts", "**/*.tsx", "**/*.scss", "package.json"], {
        ignoreInitial: true,
      })
      .on("all", async () => {
        build(clientRefresh)
      })
      .watch(paths, { ignoreInitial: true })
      .on("add", () => build(clientRefresh))
      .on("change", () => build(clientRefresh))
      .on("unlink", () => build(clientRefresh))
  } else {
    await build(() => {})
    ctx.dispose()
@@ -441,7 +459,7 @@
 * @param {*} argv arguments for `update`
 */
export async function handleUpdate(argv) {
  const contentFolder = path.join(cwd, argv.directory)
  const contentFolder = resolveContentPath(argv.directory)
  console.log(chalk.bgGreen.black(`\n Quartz v${version} \n`))
  console.log("Backing up your content")
  execSync(
@@ -493,7 +511,7 @@
 * @param {*} argv arguments for `restore`
 */
export async function handleRestore(argv) {
  const contentFolder = path.join(cwd, argv.directory)
  const contentFolder = resolveContentPath(argv.directory)
  await popContentFolder(contentFolder)
}
@@ -502,7 +520,7 @@
 * @param {*} argv arguments for `sync`
 */
export async function handleSync(argv) {
  const contentFolder = path.join(cwd, argv.directory)
  const contentFolder = resolveContentPath(argv.directory)
  console.log(chalk.bgGreen.black(`\n Quartz v${version} \n`))
  console.log("Backing up your content")