impl baseDir option for quartz build --serve for local testing

This commit is contained in:
Jacky Zhao 2023-08-19 18:04:17 -07:00
parent 6681f28af0
commit 7b8017413c
3 changed files with 37 additions and 6 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@jackyzha0/quartz", "name": "@jackyzha0/quartz",
"version": "4.0.7", "version": "4.0.8",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@jackyzha0/quartz", "name": "@jackyzha0/quartz",
"version": "4.0.7", "version": "4.0.8",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@clack/prompts": "^0.6.3", "@clack/prompts": "^0.6.3",

View File

@ -76,6 +76,7 @@ const BuildArgv = {
}, },
baseDir: { baseDir: {
string: true, string: true,
default: "",
describe: "base path to serve your local server on", describe: "base path to serve your local server on",
}, },
port: { port: {
@ -424,8 +425,26 @@ See the [documentation](https://quartz.jzhao.xyz) for how to get started.
wss.on("connection", (ws) => connections.push(ws)) wss.on("connection", (ws) => connections.push(ws))
const clientRefresh = () => connections.forEach((conn) => conn.send("rebuild")) const clientRefresh = () => connections.forEach((conn) => conn.send("rebuild"))
if (argv.baseDir !== "" && !argv.baseDir.startsWith("/")) {
argv.baseDir = "/" + argv.baseDir
}
await build(clientRefresh) await build(clientRefresh)
const server = http.createServer(async (req, res) => { const server = http.createServer(async (req, res) => {
if (argv.baseDir && !req.url?.startsWith(argv.baseDir)) {
console.log(
chalk.red(
`[404] ${req.url} (warning: link outside of site, this is likely a Quartz bug)`,
),
)
res.writeHead(404)
res.end()
return
}
// strip baseDir prefix
req.url = req.url?.slice(argv.baseDir.length)
const serve = async () => { const serve = async () => {
await serveHandler(req, res, { await serveHandler(req, res, {
public: argv.output, public: argv.output,
@ -434,14 +453,15 @@ See the [documentation](https://quartz.jzhao.xyz) for how to get started.
const status = res.statusCode const status = res.statusCode
const statusString = const statusString =
status >= 200 && status < 300 ? chalk.green(`[${status}]`) : chalk.red(`[${status}]`) status >= 200 && status < 300 ? chalk.green(`[${status}]`) : chalk.red(`[${status}]`)
console.log(statusString + chalk.grey(` ${req.url}`)) console.log(statusString + chalk.grey(` ${argv.baseDir}${req.url}`))
} }
const redirect = (newFp) => { const redirect = (newFp) => {
newFp = argv.baseDir + newFp
res.writeHead(302, { res.writeHead(302, {
Location: newFp, Location: newFp,
}) })
console.log(chalk.yellow("[302]") + chalk.grey(` ${req.url} -> ${newFp}`)) console.log(chalk.yellow("[302]") + chalk.grey(` ${argv.baseDir}${req.url} -> ${newFp}`))
res.end() res.end()
} }
@ -487,7 +507,11 @@ See the [documentation](https://quartz.jzhao.xyz) for how to get started.
return serve() return serve()
}) })
server.listen(argv.port) server.listen(argv.port)
console.log(chalk.cyan(`Started a Quartz server listening at http://localhost:${argv.port}`)) console.log(
chalk.cyan(
`Started a Quartz server listening at http://localhost:${argv.port}${argv.baseDir}`,
),
)
console.log("hint: exit with ctrl+c") console.log("hint: exit with ctrl+c")
chokidar chokidar
.watch(["**/*.ts", "**/*.tsx", "**/*.scss", "package.json"], { .watch(["**/*.ts", "**/*.tsx", "**/*.scss", "package.json"], {

View File

@ -6,7 +6,14 @@ import { pageResources, renderPage } from "../../components/renderPage"
import { ProcessedContent, defaultProcessedContent } from "../vfile" import { ProcessedContent, defaultProcessedContent } from "../vfile"
import { FullPageLayout } from "../../cfg" import { FullPageLayout } from "../../cfg"
import path from "path" import path from "path"
import { FilePath, FullSlug, SimpleSlug, _stripSlashes, joinSegments, simplifySlug } from "../../util/path" import {
FilePath,
FullSlug,
SimpleSlug,
_stripSlashes,
joinSegments,
simplifySlug,
} from "../../util/path"
import { defaultListPageLayout, sharedPageComponents } from "../../../quartz.layout" import { defaultListPageLayout, sharedPageComponents } from "../../../quartz.layout"
import { FolderContent } from "../../components" import { FolderContent } from "../../components"