Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): support custom srcDir #4270

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/node/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export enum ScaffoldThemeType {

export interface ScaffoldOptions {
root: string
srcDir: string
title?: string
description?: string
theme: ScaffoldThemeType
Expand Down Expand Up @@ -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: () =>
text({
message: 'Site title:',
Expand Down Expand Up @@ -108,19 +116,22 @@ export async function init(root: string | undefined) {

export function scaffold({
root = './',
srcDir = './',
title = 'My Awesome Project',
description = 'A VitePress Site',
theme,
useTs,
injectNpmScripts
}: 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,
Expand All @@ -139,14 +150,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)
}

Expand Down
4 changes: 3 additions & 1 deletion template/.vitepress/config.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down