Skip to content

Commit

Permalink
feat: basic config watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 21, 2024
1 parent eca841f commit c6e8d20
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app/modules/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import type { ModuleOptions as ContentOptions } from '@nuxt/content'
import type { DocsConfig } from '../../../schema/config'

export default defineNuxtModule({
async setup(_, nuxt) {
setup(_, nuxt) {
if (nuxt.options._prepare) {
return
}

const contentConfig = (nuxt.options as any).content as ContentOptions
const docsConfig = (nuxt.options as any).docs as DocsConfig

if (docsConfig.landing === false) {
Expand All @@ -21,6 +20,7 @@ export default defineNuxtModule({
})
}

const contentConfig = (nuxt.options as any).content as ContentOptions
contentConfig.sources = {
...contentConfig.sources,
content: {
Expand Down
Binary file modified bun.lockb
Binary file not shown.
40 changes: 35 additions & 5 deletions cli/cli.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { defineCommand, runMain } from 'citty'
import consola from 'consola'
// import { relative } from "pathe"
import { getContext } from "unctx"
import { setupDocs } from './setup.mjs'

export function createCLI(opts) {
Expand All @@ -18,11 +21,35 @@ export function createCLI(opts) {
},
args: { ...sharedArgs },
async setup({ args }) {
const { appDir, nuxtConfig } = await setupDocs(args.dir, { ...opts.setup, dev: true })
// const cwd = process.cwd()
const logger = consola.withTag('undocs')
const { tryUse: tryUseNuxt } = getContext('nuxt')

const { appDir, nuxtConfig, unwatch } = await setupDocs(args.dir, {
...opts.setup, dev: true, watch: {
// onWatch: (event) => {
// logger.info(`Config file ${event.type} \`${relative(cwd, event.path)}\``)
// },
acceptHMR({ getDiff }) {
const diff = getDiff().filter(entry => entry.key !== 'dir')
if (diff.length === 0) {
return true;
}
},
onUpdate({ getDiff, newConfig: { config } }) {
const diff = getDiff().filter(entry => entry.key !== 'dir');
logger.info("Config updated:\n" + diff.map((i) => ' - ' + i.toJSON()).join("\n"));
Object.assign(nuxtConfig.docs, config)
tryUseNuxt()?.callHook('restart')
},
}
})

process.chdir(appDir)
await import('nuxi').then((nuxi) =>
nuxi.runCommand('dev', [appDir, '--no-fork', '--port', '4000'], { overrides: nuxtConfig }),
)
process.on('exit', () => unwatch())

const { runCommand } = await import('nuxi')
await runCommand('dev', [appDir, '--no-fork', '--port', process.env.PORT || '4000'], { overrides: nuxtConfig })
},
})

Expand All @@ -34,8 +61,11 @@ export function createCLI(opts) {
args: { ...sharedArgs },
async setup({ args }) {
const { appDir, nuxtConfig } = await setupDocs(args.dir, opts.setup)

process.chdir(appDir)
await import('nuxi').then((nuxi) => nuxi.runCommand('generate', [appDir], { overrides: nuxtConfig }))

const { runCommand } = await import('nuxi')
await runCommand('generate', [appDir], { overrides: nuxtConfig })
},
})

Expand Down
29 changes: 12 additions & 17 deletions cli/setup.mjs
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { fileURLToPath } from 'node:url'
import { resolve } from 'node:path'
import { getColors } from 'theme-colors'
import { loadConfig } from 'c12'
import { loadConfig, watchConfig } from 'c12'

const appDir = fileURLToPath(new URL('../app', import.meta.url))
const appDirUnjs = fileURLToPath(new URL('../app/.unjs', import.meta.url))

const pkgDir = fileURLToPath(new URL('..', import.meta.url))

export async function setupDocs(docsDir, opts = {}) {
// Try to load docs config
const docsconfig = (await loadDocsConfig(docsDir, opts.defaults)) || {}
// Load config
const { unwatch, config: docsconfig } = await (opts.watch ? watchConfig : loadConfig)({
name: 'docs',
cwd: docsDir,
defaults: {
url: inferSiteURL(),
...opts.defaults,
},
...opts.watch
})

// Normalize dir
docsconfig.dir = docsDir = resolve(docsconfig.dir || docsDir)
Expand Down Expand Up @@ -75,23 +83,10 @@ export async function setupDocs(docsDir, opts = {}) {
docsDir,
appDir,
nuxtConfig,
unwatch,
}
}

async function loadDocsConfig(dir, defaults = {}) {
const { config } = await loadConfig({
name: 'docs',
cwd: dir,
defaults: {
url: inferSiteURL(),
...defaults,
},
overrides: { dir },
})

return config
}

function inferSiteURL() {
// https://github.com/unjs/std-env/issues/59
return (
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"scule": "^1.3.0",
"tailwindcss": "^3.4.1",
"theme-colors": "^0.1.0",
"unctx": "^2.3.1",
"unstorage": "^1.10.1",
"vue": "~3.3.13",
"vue-router": "^4.2.5"
Expand Down

0 comments on commit c6e8d20

Please sign in to comment.