From 518c0945f159aae679ef710bb48ae3ab3891cc9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Est=C3=A9ban?= Date: Sat, 15 Feb 2025 20:01:04 +0100 Subject: [PATCH] feat(cli): support custom `srcDir` (#4270) --- src/node/init/init.ts | 21 +++++++++++++++++++-- template/.vitepress/config.js | 4 +++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/node/init/init.ts b/src/node/init/init.ts index 6dddee1a..229dd010 100644 --- a/src/node/init/init.ts +++ b/src/node/init/init.ts @@ -21,6 +21,7 @@ export enum ScaffoldThemeType { export interface ScaffoldOptions { root: string + srcDir: string title?: string description?: string theme: ScaffoldThemeType @@ -53,6 +54,13 @@ export async function init(root: string | undefined) { }) }, + srcDir: async () => { + return text({ + message: 'Where should VitePress look for your markdown files?', + initialValue: './' + }) + }, + title: () => text({ message: 'Site title:', @@ -129,6 +137,7 @@ export async function init(root: string | undefined) { export function scaffold({ root = './', + srcDir = './', title = 'My Awesome Project', description = 'A VitePress Site', theme, @@ -138,12 +147,14 @@ export function scaffold({ npmScriptsPrefix = 'docs' }: ScaffoldOptions): string { const resolvedRoot = path.resolve(root) + const resolvedSrcDir = path.resolve(root, srcDir) const templateDir = path.resolve( path.dirname(fileURLToPath(import.meta.url)), '../../template' ) const data = { + srcDir: srcDir === './' ? undefined : JSON.stringify(srcDir), // omit if default title: JSON.stringify(title), description: JSON.stringify(description), useTs, @@ -162,14 +173,20 @@ export function scaffold({ const renderFile = (file: string) => { const filePath = path.resolve(templateDir, file) let targetPath = path.resolve(resolvedRoot, file) + if (useMjs && file === '.vitepress/config.js') { targetPath = targetPath.replace(/\.js$/, '.mjs') } if (useTs) { targetPath = targetPath.replace(/\.(m?)js$/, '.$1ts') } - const src = fs.readFileSync(filePath, 'utf-8') - const compiled = template(src)(data) + if (file.endsWith('.md')) { + targetPath = path.resolve(resolvedSrcDir, file) + } + + const content = fs.readFileSync(filePath, 'utf-8') + const compiled = template(content)(data) + fs.outputFileSync(targetPath, compiled) } diff --git a/template/.vitepress/config.js b/template/.vitepress/config.js index cf26cc23..33711d0b 100644 --- a/template/.vitepress/config.js +++ b/template/.vitepress/config.js @@ -1,7 +1,9 @@ import { defineConfig } from 'vitepress' // https://vitepress.dev/reference/site-config -export default defineConfig({ +export default defineConfig({<% if (srcDir) { %> + srcDir: <%= srcDir %>, + <% } %> title: <%= title %>, description: <%= description %><% if (defaultTheme) { %>, themeConfig: {