feat(cli): support custom `srcDir`

pull/4270/head
barbapapazes 4 weeks ago
parent 3e8fc40c36
commit 23f4d07262

@ -21,6 +21,7 @@ export enum ScaffoldThemeType {
export interface ScaffoldOptions { export interface ScaffoldOptions {
root: string root: string
srcDir: string
title?: string title?: string
description?: string description?: string
theme: ScaffoldThemeType theme: ScaffoldThemeType
@ -50,6 +51,13 @@ export async function init(root: string | undefined) {
}) })
}, },
srcDir: async () => {
return text({
message: 'Where should VitePress look for your markdown files?',
initialValue: './'
})
},
title: () => title: () =>
text({ text({
message: 'Site title:', message: 'Site title:',
@ -108,6 +116,7 @@ export async function init(root: string | undefined) {
export function scaffold({ export function scaffold({
root = './', root = './',
srcDir = './',
title = 'My Awesome Project', title = 'My Awesome Project',
description = 'A VitePress Site', description = 'A VitePress Site',
theme, theme,
@ -115,12 +124,14 @@ export function scaffold({
injectNpmScripts injectNpmScripts
}: ScaffoldOptions): string { }: ScaffoldOptions): string {
const resolvedRoot = path.resolve(root) const resolvedRoot = path.resolve(root)
const resolvedSrcDir = path.resolve(root, srcDir)
const templateDir = path.resolve( const templateDir = path.resolve(
path.dirname(fileURLToPath(import.meta.url)), path.dirname(fileURLToPath(import.meta.url)),
'../../template' '../../template'
) )
const data = { const data = {
srcDir: srcDir === './' ? undefined : JSON.stringify(srcDir), // omit if default
title: JSON.stringify(title), title: JSON.stringify(title),
description: JSON.stringify(description), description: JSON.stringify(description),
useTs, useTs,
@ -139,14 +150,20 @@ export function scaffold({
const renderFile = (file: string) => { const renderFile = (file: string) => {
const filePath = path.resolve(templateDir, file) const filePath = path.resolve(templateDir, file)
let targetPath = path.resolve(resolvedRoot, file) let targetPath = path.resolve(resolvedRoot, file)
if (useMjs && file === '.vitepress/config.js') { if (useMjs && file === '.vitepress/config.js') {
targetPath = targetPath.replace(/\.js$/, '.mjs') targetPath = targetPath.replace(/\.js$/, '.mjs')
} }
if (useTs) { if (useTs) {
targetPath = targetPath.replace(/\.(m?)js$/, '.$1ts') targetPath = targetPath.replace(/\.(m?)js$/, '.$1ts')
} }
const src = fs.readFileSync(filePath, 'utf-8') if (file.endsWith('.md')) {
const compiled = template(src)(data) targetPath = path.resolve(resolvedSrcDir, file)
}
const content = fs.readFileSync(filePath, 'utf-8')
const compiled = template(content)(data)
fs.outputFileSync(targetPath, compiled) fs.outputFileSync(targetPath, compiled)
} }

@ -1,7 +1,9 @@
import { defineConfig } from 'vitepress' import { defineConfig } from 'vitepress'
// https://vitepress.dev/reference/site-config // https://vitepress.dev/reference/site-config
export default defineConfig({ export default defineConfig({<% if (srcDir) { %>
srcDir: <%= srcDir %>,
<% } %>
title: <%= title %>, title: <%= title %>,
description: <%= description %><% if (defaultTheme) { %>, description: <%= description %><% if (defaultTheme) { %>,
themeConfig: { themeConfig: {

Loading…
Cancel
Save