fix(cli): plugin commands and info read the merged extends config; edits write only the user's entries; --serve watches local bases

This commit is contained in:
saberzero1 committed 2026-11-26 22:37:51 +01:00
1 parent a51b7df1ce
commit 9de19e3027
7 files changed
+353 -31

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
`extends` (experimental): `npx quartz plugin list`, `status`, `doctor`, `install`, `prune` and `npx quartz info` read the merged configuration, as the build does. Before, they saw only the entries in your own file, so `list` showed a handful of plugins and `doctor` warned that overlay entries without `enabled` are disabled. `enable`, `disable` and `config` now work on plugins from a base by adding or changing your own entry for them (only that change is written; the merged configuration never is), `config --unset` resets an option a base sets with `null`, and `remove` refuses a plugin that comes from a base. `npx quartz build --serve` also rebuilds when a local base file changes.
+4 -1
View File
@@ -50,8 +50,11 @@ plugins:
- Plugins run in `order` as before. Your file can change a default plugin's `order`. Where two plugins have the same order, the base config's list order decides, and added plugins come last.
- Your updates to `quartz.config.default.yaml` (for example after `npx quartz upgrade`) reach your site without copying them over.
- `npx quartz build --serve` also rebuilds when a local file your config extends changes.
- `npx quartz plugin` commands and `npx quartz info` read the merged configuration, as the build does: `plugin list`, `status`, `doctor`, `install` and `prune` see the plugins your bases configure. Commands that change plugins only ever write your own file, never the merged result: `enable`, `disable` and `config --set` add or change your entry for the plugin (`- source: …` with just that change), `config --unset` removes your value and, when a base sets the option, sets it to `null` so the plugin's default applies, and `add` appends a full entry. `remove` refuses a plugin that a base configures (it can't be removed from your file); disable it instead.
> [!warning] Experimental
> `npx quartz plugin` commands that edit plugin entries (`enable`, `disable`, `config`, …) only see the entries in your own file so far. Edit an extending `quartz.config.yaml` by hand.
> `extends` is experimental: the merge rules may still change.
## General Configuration
+12 -5
View File
@@ -39,6 +39,7 @@ import {
readPluginsJson,
writePluginsJson,
updateGlobalConfig,
extendedConfigFiles,
LOCKFILE_PATH,
} from "./plugin-data.js"
import {
@@ -619,11 +620,17 @@ export async function handleBuild(argv) {
"quartz.config.default.yaml",
])
const extraWatch = process.env.QUARTZ_CONFIG_PATH ? [process.env.QUARTZ_CONFIG_PATH] : []
chokidar
.watch([...paths, ...extraWatch], { ignoreInitial: true })
.on("add", () => build(clientRefresh))
.on("change", () => build(clientRefresh))
.on("unlink", () => build(clientRefresh))
// local files the config extends (and the files they extend); looked up again after each
// change, so a newly added base is watched too
const watcher = chokidar.watch([...paths, ...extraWatch, ...(await extendedConfigFiles())], {
ignoreInitial: true,
})
const rebuild = async () => {
await build(clientRefresh)
const bases = await extendedConfigFiles()
if (bases.length > 0) watcher.add(bases)
}
watcher.on("add", rebuild).on("change", rebuild).on("unlink", rebuild)
console.log(styleText("gray", "hint: exit with ctrl+c"))
}
+11 -2
View File
@@ -23,6 +23,7 @@ import { builtinKeyFor, describeBuiltins } from "./builtin-migration.js"
import { shortPluginName } from "./plugin-validation.js"
import {
readConfigWithDiagnostics,
resolveEffectiveConfig,
readLockfile,
extractPluginName,
formatSource,
@@ -56,10 +57,18 @@ function readPackage(dir) {
}
}
export function collectInfo(root = process.cwd()) {
export async function collectInfo(root = process.cwd()) {
const version = readQuartzVersion(root)
const checkout = describeQuartzCheckout(root)
const config = readConfigWithDiagnostics()
// with `extends`, the merged configuration the build uses
if (config.data && !config.error) {
try {
config.data = await resolveEffectiveConfig(config.data, config.path)
} catch (err) {
config.error = `extends: ${String(err?.message ?? err)}`
}
}
const lockfile = readLockfile()
const entries = Array.isArray(config.data?.plugins) ? config.data.plugins : []
@@ -176,7 +185,7 @@ export function formatInfoMarkdown(info) {
}
export async function handleInfo({ json = false } = {}) {
const info = collectInfo()
const info = await collectInfo()
if (json) {
console.log(JSON.stringify(info, null, 2))
return
+59
View File
@@ -158,6 +158,65 @@ export function readPluginsJson() {
return data
}
/** Whether a config (as read from its file) extends another (`extends:` with at least one base). */
export function configExtends(data) {
if (!isPlainObject(data)) return false
const value = data.extends
if (value === undefined || value === null) return false
return !Array.isArray(value) || value.length > 0
}
let configExtendsModule
/**
* The build's `extends` implementation (quartz/plugins/loader/config-extends.ts), loaded through
* tsx only when a config uses `extends`, so the CLI merges exactly as the build does.
*/
export function loadConfigExtendsModule() {
configExtendsModule ??= import("tsx/esm/api").then(({ tsImport }) =>
tsImport("../plugins/loader/config-extends.ts", import.meta.url),
)
return configExtendsModule
}
/**
* The configuration the build uses: the config file with its `extends` applied. A config
* without `extends` is returned as read. Read-only commands (list, status, doctor, info,
* install, prune) use this; commands that edit the file keep using readPluginsJson(), which
* returns only what the user's file contains. Throws a clear error when a base can't be loaded.
*/
export async function resolveEffectiveConfig(data, configPath = resolveConfigPath()) {
if (!configExtends(data)) return data
const { resolveExtends } = await loadConfigExtendsModule()
return resolveExtends(data, configPath)
}
/** readPluginsJson() with `extends` applied; reports a base that can't be loaded and returns null. */
export async function readEffectivePluginsJson() {
const data = readPluginsJson()
try {
return await resolveEffectiveConfig(data)
} catch (err) {
console.error(`✗ ${path.basename(resolveConfigPath())}: ${String(err?.message ?? err)}`)
process.exitCode = 1
return null
}
}
/**
* Local files the config extends (directly or through other bases), for `--serve` to watch.
* Empty when it extends nothing or a base can't be loaded (the build reports that).
*/
export async function extendedConfigFiles() {
const data = readPluginsJson()
if (!configExtends(data)) return []
try {
const { localExtendsFiles } = await loadConfigExtendsModule()
return await localExtendsFiles(data, resolveConfigPath())
} catch {
return []
}
}
/** Path of the config file the CLI reads from (may be a default/legacy file). */
export function getConfigReadPath() {
return resolveConfigPath()
+179 -22
View File
@@ -4,6 +4,11 @@ import os from "os"
import { styleText } from "util"
import {
readPluginsJson,
readEffectivePluginsJson,
resolveEffectiveConfig,
configExtends,
loadConfigExtendsModule,
getConfigReadPath,
writePluginsJson,
readLockfile,
writeLockfile,
@@ -128,6 +133,67 @@ function resolveConfigEntryOrReport(json, name) {
return null
}
/**
* For commands that edit quartz.config.yaml: `json` is the user's own file (what gets written
* back) and `effective` the configuration the build uses (with `extends` applied; the same
* object without it). Plugins are looked up in `effective`; changes go to the user's entry for
* the plugin (see overlayEntryFor), never the merged configuration. Returns null after
* reporting when a base can't be loaded.
*/
async function readConfigForEdit() {
const json = readPluginsJson()
if (!configExtends(json)) return { json, effective: json, extending: false }
try {
const effective = await resolveEffectiveConfig(json, getConfigReadPath())
const { pluginMatchKey } = await loadConfigExtendsModule()
return { json, effective, extending: true, matchKey: pluginMatchKey }
} catch (err) {
console.log(
styleText(
"red",
`✗ ${path.basename(getConfigReadPath())}: ${String(err?.message ?? err)} — nothing changed`,
),
)
markFailed()
return null
}
}
/**
* The user's entry for a plugin of the merged configuration (matched as `extends` merges
* entries; the last one wins when there are several). With `create`, an entry
* `{ source }` is added when the user's file has none, which then changes only what is set in it.
*/
function overlayEntryFor(edit, mergedEntry, create = false) {
if (!edit.extending) return mergedEntry
if (edit.json.plugins == null) edit.json.plugins = []
if (!Array.isArray(edit.json.plugins)) {
throw new Error(`"plugins" in ${path.basename(getConfigReadPath())} must be a list`)
}
const key = edit.matchKey(mergedEntry.source)
let entry = edit.json.plugins.findLast((e) => {
try {
return e?.source !== undefined && edit.matchKey(e.source) === key
} catch {
return false
}
})
if (!entry && create) {
entry = { source: mergedEntry.source }
edit.json.plugins.push(entry)
}
return entry
}
function hasOptionPath(obj, segments) {
let node = obj
for (const segment of segments) {
if (node === null || typeof node !== "object" || !(segment in node)) return false
node = node[segment]
}
return node !== undefined && node !== null
}
/** Read the manifest (`quartz` field of package.json) of a configured plugin, if installed. */
function readEntryManifest(source) {
const dir = resolvePluginDir(source)
@@ -427,7 +493,7 @@ async function regeneratePluginIndex() {
// npm plugins in the config are part of the index too (they used to be dropped here until the
// next build regenerated it); the generator is shared with the build, see plugin-index.js
const npmPackages = []
for (const entry of readPluginsJson()?.plugins ?? []) {
for (const entry of (await readEffectivePluginsJson())?.plugins ?? []) {
if (builtinMessage(entry.source)) continue
try {
const parsed = parseGitSource(entry.source)
@@ -455,7 +521,9 @@ export async function handlePluginInstallUnified({
// Plugins that are built into core now (crawl-links, remove-draft, content-index) are never
// cloned or built, even if the config or the lockfile still lists them.
const rawPluginsJson = readPluginsJson()
// with `extends`, the plugins of the merged configuration (as the build loads them)
const rawPluginsJson = await readEffectivePluginsJson()
if (!rawPluginsJson && configExtends(readPluginsJson())) return
const pluginsJson = rawPluginsJson && {
...rawPluginsJson,
plugins: (rawPluginsJson.plugins ?? []).filter((entry) => !builtinMessage(entry.source)),
@@ -1476,8 +1544,12 @@ export async function handlePluginAdd(
fs.mkdirSync(PLUGINS_DIR, { recursive: true })
}
const pluginsJson = readPluginsJson()
const configured = pluginsJson?.plugins ?? []
const edit = await readConfigForEdit()
if (!edit) return
// added entries go to the user's file; "already configured" also covers plugins that come
// from an `extends` base
const pluginsJson = edit.json
const configured = Array.isArray(edit.effective?.plugins) ? edit.effective.plugins : []
const addedPlugins = []
let hadFailure = false
@@ -1696,7 +1768,11 @@ export async function handlePluginAdd(
writeLockfile(lockfile)
}
if (pluginsJson?.plugins && addedPlugins.length > 0) {
// an extending config may have no `plugins:` yet
if (pluginsJson && pluginsJson.plugins == null && addedPlugins.length > 0) {
pluginsJson.plugins = []
}
if (Array.isArray(pluginsJson?.plugins) && addedPlugins.length > 0) {
for (const { pluginDir, source, configSource } of addedPlugins) {
const manifest = readManifestFromPackageJson(pluginDir)
const newEntry = {
@@ -1774,13 +1850,36 @@ export async function handlePluginAdd(
export async function handlePluginRemove(names) {
const lockfile = readLockfile() ?? { version: "1.0.0", plugins: {} }
const pluginsJson = readPluginsJson()
const edit = await readConfigForEdit()
if (!edit) return
const pluginsJson = edit.json
// With `extends`: the plugins the bases configure (without the user's entries). Those can't
// be removed from the user's file, only disabled.
const basePlugins = edit.extending
? ((await resolveEffectiveConfig({ ...pluginsJson, plugins: [] }, getConfigReadPath()))
.plugins ?? [])
: []
let lockfileChanged = false
let configChanged = false
let removedFiles = false
const removedSources = []
for (const name of names) {
const fromBase = findConfigEntries(basePlugins, name, nameHelpers).indices
if (fromBase.length > 0) {
const label = extractPluginName(basePlugins[fromBase[0]].source)
console.log(
styleText(
"red",
`✗ ${label} comes from the configuration ${path.basename(getConfigReadPath())} extends, so it can't be removed from your file.`,
),
)
console.log(
styleText("gray", ` Turn it off with 'npx quartz plugin disable ${label}' instead.`),
)
markFailed()
continue
}
// Prefer the config entry (accepts short names such as "explorer").
const { indices } = findConfigEntries(pluginsJson?.plugins ?? [], name, nameHelpers)
const configIndex = indices.length === 1 ? indices[0] : -1
@@ -1892,7 +1991,9 @@ export async function handlePluginRemove(names) {
}
async function setEnabled(names, enabled) {
const json = readPluginsJson()
const edit = await readConfigForEdit()
if (!edit) return
const json = edit.json
const verb = enabled ? "enable" : "disable"
if (!json) {
console.log(styleText("red", `✗ No quartz.config.yaml found. Cannot ${verb} plugins.`))
@@ -1903,12 +2004,13 @@ async function setEnabled(names, enabled) {
let changed = false
for (const name of names) {
const builtin = builtinMessage(name)
if (builtin && !(json.plugins ?? []).some((e) => officialPluginName(e.source) === name)) {
const plugins = Array.isArray(edit.effective.plugins) ? edit.effective.plugins : []
if (builtin && !plugins.some((e) => officialPluginName(e.source) === name)) {
console.log(styleText("yellow", `⚠ ${builtin}`))
markFailed()
continue
}
const resolved = resolveConfigEntryOrReport(json, name)
const resolved = resolveConfigEntryOrReport(edit.effective, name)
if (!resolved) continue
const { entry } = resolved
const label = extractPluginName(entry.source)
@@ -1917,7 +2019,8 @@ async function setEnabled(names, enabled) {
console.log(styleText("gray", `✓ ${label} is already ${verb}d`))
continue
}
entry.enabled = enabled
// with `extends`: set in the user's entry for the plugin (added if needed)
overlayEntryFor(edit, entry, true).enabled = enabled
changed = true
console.log(styleText("green", `✓ ${enabled ? "Enabled" : "Disabled"} ${label}`))
if (
@@ -1962,14 +2065,18 @@ function printOptionIssues(issues, label) {
}
export async function handlePluginConfig(name, options = {}) {
const json = readPluginsJson()
const edit = await readConfigForEdit()
if (!edit) return
const json = edit.json
if (!json) {
console.log(styleText("red", "✗ No quartz.config.yaml found."))
markFailed()
return
}
const resolved = resolveConfigEntryOrReport(json, name)
// `entry` is the plugin as the build sees it (merged with `extends` bases); changes go to
// `target`, the user's own entry for it
const resolved = resolveConfigEntryOrReport(edit.effective, name)
if (!resolved) return
const { entry } = resolved
const label = extractPluginName(entry.source)
@@ -1979,7 +2086,8 @@ export async function handlePluginConfig(name, options = {}) {
const unsets = [options.unset ?? []].flat().filter((v) => v !== undefined)
if (sets.length > 0 || unsets.length > 0) {
if (!entry.options || typeof entry.options !== "object") entry.options = {}
const target = overlayEntryFor(edit, entry, true)
if (!target.options || typeof target.options !== "object") target.options = {}
let changed = false
for (const assignment of sets) {
const eqIndex = String(assignment).indexOf("=")
@@ -1996,7 +2104,7 @@ export async function handlePluginConfig(name, options = {}) {
continue
}
const value = parseCliValue(assignment.slice(eqIndex + 1))
setOptionPath(entry.options, segments, value)
setOptionPath(target.options, segments, value)
changed = true
console.log(
styleText("green", `✓ Set ${label}.${segments.join(".")} = ${JSON.stringify(value)}`),
@@ -2010,7 +2118,29 @@ export async function handlePluginConfig(name, options = {}) {
}
for (const keyPath of unsets) {
const segments = parseOptionPath(String(keyPath))
if (unsetOptionPath(entry.options, segments)) {
if (edit.extending) {
// drop the user's value, then reset a value a base still sets with `null` (merging
// removes the key, so the plugin's default applies)
const hadOwn = unsetOptionPath(target.options, segments)
const merged = await resolveEffectiveConfig(json, getConfigReadPath())
const key = edit.matchKey(entry.source)
const now = (merged.plugins ?? []).findLast((e) => edit.matchKey(e.source) === key)
const fromBase = hasOptionPath(now?.options, segments)
if (fromBase) setOptionPath(target.options, segments, null)
if (hadOwn || fromBase) {
changed = true
console.log(
styleText(
"green",
`✓ Unset ${label}.${segments.join(".")} (plugin default applies${fromBase ? "; null overrides the extended config" : ""})`,
),
)
} else {
console.log(styleText("yellow", `⚠ ${label} has no option "${keyPath}" set`))
}
continue
}
if (unsetOptionPath(target.options, segments)) {
changed = true
console.log(
styleText("green", `✓ Unset ${label}.${segments.join(".")} (plugin default applies)`),
@@ -2102,7 +2232,8 @@ function collectUntrackedEntries(pluginsJson, lockfile) {
export async function handlePluginList() {
const lockfile = readLockfile()
const pluginsJson = readPluginsJson()
const pluginsJson = await readEffectivePluginsJson()
if (!pluginsJson && configExtends(readPluginsJson())) return
const { npm, missing } = collectUntrackedEntries(pluginsJson, lockfile)
const lockEntries = Object.entries(lockfile?.plugins ?? {})
@@ -2222,7 +2353,8 @@ export async function handlePluginList() {
export async function handlePluginStatus() {
const lockfile = readLockfile() ?? { version: "1.0.0", plugins: {} }
const pluginsJson = readPluginsJson()
const pluginsJson = await readEffectivePluginsJson()
if (!pluginsJson && configExtends(readPluginsJson())) return
const { npm } = collectUntrackedEntries(pluginsJson, lockfile)
if (Object.keys(lockfile.plugins).length === 0 && npm.length === 0) {
console.log(styleText("gray", "No plugins installed"))
@@ -2459,7 +2591,23 @@ export async function handlePluginDoctor({ verbose = false, fix = false } = {})
report("info", null, `using ${configLabel} (no quartz.config.yaml yet)`)
}
if (!Array.isArray(config.data?.plugins)) {
// With `extends`, check the merged configuration (what the build loads); --fix still edits
// only this file.
const extending = configExtends(config.data)
const effectiveOf = async (data) => (extending ? resolveEffectiveConfig(data, config.path) : data)
let effective
try {
effective = await effectiveOf(config.data)
} catch (err) {
report("error", configLabel, `extends: ${String(err?.message ?? err)}`)
markFailed()
return
}
if (extending) {
report("info", null, `${configLabel} uses extends; checking the merged configuration`)
}
if (!Array.isArray(effective?.plugins)) {
report("error", configLabel, "`plugins` must be a list")
markFailed()
return
@@ -2474,6 +2622,7 @@ export async function handlePluginDoctor({ verbose = false, fix = false } = {})
for (const change of changes) console.log(styleText("green", `✓ ${change}`))
console.log()
config.data = migrated
effective = await effectiveOf(migrated)
} else {
for (const { name, key } of migrations) {
report(
@@ -2487,9 +2636,11 @@ export async function handlePluginDoctor({ verbose = false, fix = false } = {})
}
}
const absorbed = new Set(
migrations.filter((m) => m.name !== "note-properties").map((m) => m.entry),
findBuiltinMigrations(effective)
.filter((m) => m.name !== "note-properties")
.map((m) => m.entry),
)
const plugins = config.data.plugins.filter((entry) => !absorbed.has(entry))
const plugins = effective.plugins.filter((entry) => !absorbed.has(entry))
const lockfile = readLockfile()
if (fs.existsSync(LOCKFILE_PATH) && !lockfile) {
@@ -2669,7 +2820,7 @@ export async function handlePluginDoctor({ verbose = false, fix = false } = {})
// satisfied by the built-in: its entries were absorbed above.
const builtinKey = builtinKeyFor(dep)
if (builtinKey) {
const builtin = describeBuiltins(config.data.configuration).find(
const builtin = describeBuiltins(effective.configuration).find(
(b) => b.key === builtinKey,
)
if (builtin && !builtin.enabled) {
@@ -2771,7 +2922,13 @@ export async function handlePluginPrune({ dryRun = false } = {}) {
return
}
const pluginsJson = readPluginsJson()
// plugins of the merged configuration: a plugin from an `extends` base is not an orphan
const pluginsJson = await readEffectivePluginsJson()
if (!pluginsJson && configExtends(readPluginsJson())) {
console.log(styleText("red", "✗ Not pruning: the configuration could not be resolved."))
markFailed()
return
}
const configuredNames = new Set(
(pluginsJson?.plugins ?? []).map((entry) => extractPluginName(entry.source)),
)
+83 -1
View File
@@ -1,10 +1,11 @@
import test, { describe, beforeEach, afterEach } from "node:test"
import assert from "node:assert"
import { execSync } from "child_process"
import { execSync, spawnSync } from "child_process"
import fs from "fs"
import os from "os"
import path from "path"
import { pathToFileURL } from "url"
import YAML from "yaml"
import {
buildPluginAsync,
checkoutPinnedCommitAsync,
@@ -212,3 +213,84 @@ describe("checkoutPinnedCommitAsync", () => {
assert.strictEqual(fs.readFileSync(path.join(dir, "package-lock.json"), "utf-8"), `{ "v": 1 }`)
})
})
describe("plugin commands with an extending config", () => {
const cli = path.resolve(import.meta.dirname, "..", "bootstrap-cli.mjs")
// stdout and stderr, whatever the exit code (doctor fails on npm plugins that aren't installed)
const run = (dir, args) => {
const result = spawnSync(process.execPath, [cli, ...args.split(" ")], {
cwd: dir,
encoding: "utf-8",
timeout: 60_000,
env: { ...process.env, QUARTZ_CONFIG_PATH: "" },
})
return { output: result.stdout + result.stderr, status: result.status }
}
const site = () => {
const dir = path.join(root, "site")
fs.mkdirSync(dir, { recursive: true })
fs.writeFileSync(path.join(dir, "package.json"), `{ "name": "site", "version": "5.3.0" }`)
fs.writeFileSync(
path.join(dir, "quartz.config.default.yaml"),
[
"configuration:",
" pageTitle: Default",
"plugins:",
' - source: "@quartz-community/graph"',
" enabled: true",
" options: { depth: 1, scale: 2 }",
' - source: "@quartz-community/comments"',
" enabled: false",
"",
].join("\n"),
)
fs.writeFileSync(
path.join(dir, "quartz.config.yaml"),
[
"extends: default",
"plugins:",
' - source: "@quartz-community/graph"',
" options: { depth: 3 }",
"",
].join("\n"),
)
return dir
}
const read = (dir) => fs.readFileSync(path.join(dir, "quartz.config.yaml"), "utf-8")
test("list and doctor read the merged configuration", () => {
const dir = site()
const list = run(dir, "plugin list").output
assert.match(list, /@quartz-community\/comments .*disabled/)
assert.match(list, /@quartz-community\/graph .*enabled/)
const doctor = run(dir, "plugin doctor").output
assert.match(doctor, /Checking plugins/)
assert.doesNotMatch(doctor, /no `enabled` field/)
})
test("enable, disable and config change only the user's entries", () => {
const dir = site()
run(dir, "plugin enable comments")
run(dir, "plugin disable graph")
run(dir, "plugin config graph --unset scale")
run(dir, "plugin config graph --unset depth")
const out = read(dir)
assert.match(out, /^extends: default$/m)
assert.doesNotMatch(out, /pageTitle/) // the merged configuration is not written back
const data = YAML.parse(out)
assert.deepStrictEqual(data.plugins, [
// the base sets both: null drops them from the merged options, so the plugin's defaults
// apply (the user's own depth: 3 is gone too)
{ source: "@quartz-community/graph", options: { depth: null, scale: null }, enabled: false },
{ source: "@quartz-community/comments", enabled: true },
])
})
test("remove refuses a plugin that comes from a base", () => {
const dir = site()
const { output, status } = run(dir, "plugin remove comments")
assert.match(output, /comes from the configuration/)
assert.strictEqual(status, 1)
assert.doesNotMatch(read(dir), /comments/)
})
})