Skip to content

Commit 4f3f5ea

Browse files
committed
chore: resolve srcDir relative to cwd when initializing
1 parent 518c094 commit 4f3f5ea

File tree

2 files changed

+98
-78
lines changed

2 files changed

+98
-78
lines changed

docs/snippets/init.ansi

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
1-
┌ Welcome to VitePress!
2-
│
3-
◇ Where should VitePress initialize the config?
4-
│ ./docs
5-
│
6-
◇ Site title:
7-
│ My Awesome Project
8-
│
9-
◇ Site description:
10-
│ A VitePress Site
11-
│
12-
◆ Theme:
13-
│ ● Default Theme (Out of the box, good-looking docs)
14-
│ ○ Default Theme + Customization
15-
│ ○ Custom Theme
16-
└
1+
┌ Welcome to VitePress!
2+
│
3+
◇ Where should VitePress initialize the config?
4+
│ ./docs
5+
│
6+
◇ Where should VitePress look for your markdown files?
7+
│ ./docs
8+
│
9+
◇ Site title:
10+
│ My Awesome Project
11+
│
12+
◇ Site description:
13+
│ A VitePress Site
14+
│
15+
◇ Theme:
16+
│ Default Theme
17+
│
18+
◇ Use TypeScript for config and theme files?
19+
│ Yes
20+
│
21+
◇ Add VitePress npm scripts to package.json?
22+
│ Yes
23+
│
24+
◇ Add a prefix for VitePress npm scripts?
25+
│ Yes
26+
│
27+
◇ Prefix for VitePress npm scripts:
28+
│ docs
29+
│
30+
└ Done! Now run pnpm run docs:dev and start writing.

src/node/init/init.ts

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import template from 'lodash.template'
1212
import path from 'node:path'
1313
import { fileURLToPath } from 'node:url'
1414
import { bold, cyan, yellow } from 'picocolors'
15+
import { slash } from '../shared'
1516

1617
export enum ScaffoldThemeType {
1718
Default = 'default theme',
@@ -20,13 +21,13 @@ export enum ScaffoldThemeType {
2021
}
2122

2223
export interface ScaffoldOptions {
23-
root: string
24-
srcDir: string
24+
root?: string
25+
srcDir?: string
2526
title?: string
2627
description?: string
27-
theme: ScaffoldThemeType
28-
useTs: boolean
29-
injectNpmScripts: boolean
28+
theme?: ScaffoldThemeType
29+
useTs?: boolean
30+
injectNpmScripts?: boolean
3031
addNpmScriptsPrefix?: boolean
3132
npmScriptsPrefix?: string
3233
}
@@ -36,91 +37,99 @@ const getPackageManger = () => {
3637
return name.split('/')[0]
3738
}
3839

39-
export async function init(root: string | undefined) {
40+
export async function init(root?: string) {
4041
intro(bold(cyan('Welcome to VitePress!')))
4142

42-
const options: ScaffoldOptions = (await group(
43+
const options = await group(
4344
{
4445
root: async () => {
4546
if (root) return root
4647

4748
return text({
4849
message: 'Where should VitePress initialize the config?',
4950
initialValue: './',
51+
defaultValue: './',
5052
validate(value) {
5153
// TODO make sure directory is inside
5254
return undefined
5355
}
5456
})
5557
},
5658

57-
srcDir: async () => {
59+
srcDir: async ({ results }: any) => {
5860
return text({
5961
message: 'Where should VitePress look for your markdown files?',
60-
initialValue: './'
62+
initialValue: results.root,
63+
defaultValue: results.root
6164
})
6265
},
6366

64-
title: () =>
65-
text({
67+
title: async () => {
68+
return text({
6669
message: 'Site title:',
67-
placeholder: 'My Awesome Project'
68-
}),
70+
placeholder: 'My Awesome Project',
71+
defaultValue: 'My Awesome Project'
72+
})
73+
},
6974

70-
description: () =>
71-
text({
75+
description: async () => {
76+
return text({
7277
message: 'Site description:',
73-
placeholder: 'A VitePress Site'
74-
}),
78+
placeholder: 'A VitePress Site',
79+
defaultValue: 'A VitePress Site'
80+
})
81+
},
7582

76-
theme: () =>
77-
select({
83+
theme: async () => {
84+
return select({
7885
message: 'Theme:',
7986
options: [
8087
{
81-
// @ts-ignore
8288
value: ScaffoldThemeType.Default,
8389
label: 'Default Theme',
8490
hint: 'Out of the box, good-looking docs'
8591
},
8692
{
87-
// @ts-ignore
8893
value: ScaffoldThemeType.DefaultCustom,
8994
label: 'Default Theme + Customization',
9095
hint: 'Add custom CSS and layout slots'
9196
},
9297
{
93-
// @ts-ignore
9498
value: ScaffoldThemeType.Custom,
9599
label: 'Custom Theme',
96100
hint: 'Build your own or use external'
97101
}
98102
]
99-
}),
103+
})
104+
},
100105

101-
useTs: () =>
102-
confirm({ message: 'Use TypeScript for config and theme files?' }),
106+
useTs: async () => {
107+
return confirm({
108+
message: 'Use TypeScript for config and theme files?'
109+
})
110+
},
103111

104-
injectNpmScripts: () =>
105-
confirm({
112+
injectNpmScripts: async () => {
113+
return confirm({
106114
message: 'Add VitePress npm scripts to package.json?'
107-
}),
115+
})
116+
},
108117

109-
addNpmScriptsPrefix: ({ results }) => {
110-
if (!results.injectNpmScripts) return Promise.resolve(false)
118+
addNpmScriptsPrefix: async ({ results }: any) => {
119+
if (!results.injectNpmScripts) return false
111120

112121
return confirm({
113-
message: 'Add a prefix for VitePress npm scripts?',
114-
initialValue: true
122+
message: 'Add a prefix for VitePress npm scripts?'
115123
})
116124
},
117125

118-
npmScriptsPrefix: ({ results }) => {
119-
if (!results.addNpmScriptsPrefix) return Promise.resolve('docs')
126+
npmScriptsPrefix: async ({ results }: any) => {
127+
if (!results.addNpmScriptsPrefix) return 'docs'
120128

121129
return text({
122130
message: 'Prefix for VitePress npm scripts:',
123-
placeholder: 'docs'
131+
placeholder: 'docs',
132+
defaultValue: 'docs'
124133
})
125134
}
126135
},
@@ -130,31 +139,35 @@ export async function init(root: string | undefined) {
130139
process.exit(0)
131140
}
132141
}
133-
)) as ScaffoldOptions
142+
)
134143

135144
outro(scaffold(options))
136145
}
137146

138147
export function scaffold({
139-
root = './',
140-
srcDir = './',
148+
root: root_ = './',
149+
srcDir: srcDir_ = root_,
141150
title = 'My Awesome Project',
142151
description = 'A VitePress Site',
143-
theme,
144-
useTs,
145-
injectNpmScripts,
152+
theme = ScaffoldThemeType.Default,
153+
useTs = true,
154+
injectNpmScripts = true,
146155
addNpmScriptsPrefix = true,
147156
npmScriptsPrefix = 'docs'
148-
}: ScaffoldOptions): string {
149-
const resolvedRoot = path.resolve(root)
150-
const resolvedSrcDir = path.resolve(root, srcDir)
157+
}: ScaffoldOptions) {
158+
const resolvedRoot = path.resolve(root_)
159+
const root = path.relative(process.cwd(), resolvedRoot)
160+
161+
const resolvedSrcDir = path.resolve(srcDir_)
162+
const srcDir = path.relative(resolvedRoot, resolvedSrcDir)
163+
151164
const templateDir = path.resolve(
152165
path.dirname(fileURLToPath(import.meta.url)),
153166
'../../template'
154167
)
155168

156169
const data = {
157-
srcDir: srcDir === './' ? undefined : JSON.stringify(srcDir), // omit if default
170+
srcDir: srcDir ? JSON.stringify(srcDir) : undefined, // omit if default
158171
title: JSON.stringify(title),
159172
description: JSON.stringify(description),
160173
useTs,
@@ -214,34 +227,31 @@ export function scaffold({
214227
renderFile(file)
215228
}
216229

217-
const dir =
218-
root === './' ? '' : ` ${root.replace(/^\.\//, '').replace(/[/\\]$/, '')}`
219-
const gitignorePrefix = dir ? `${dir}/.vitepress` : '.vitepress'
220-
221230
const tips = []
231+
232+
const gitignorePrefix = root ? `${slash(root)}/.vitepress` : '.vitepress'
222233
if (fs.existsSync('.git')) {
223234
tips.push(
224-
`Make sure to add ${cyan(`${gitignorePrefix}/dist`)} and ` +
225-
`${cyan(`${gitignorePrefix}/cache`)} to your ` +
226-
`${cyan(`.gitignore`)} file.`
235+
`Make sure to add ${cyan(`${gitignorePrefix}/dist`)} and ${cyan(`${gitignorePrefix}/cache`)} to your ${cyan(`.gitignore`)} file.`
227236
)
228237
}
238+
229239
if (
230240
theme !== ScaffoldThemeType.Default &&
231241
!userPkg.dependencies?.['vue'] &&
232242
!userPkg.devDependencies?.['vue']
233243
) {
234244
tips.push(
235-
`Since you've chosen to customize the theme, ` +
236-
`you should also explicitly install ${cyan(`vue`)} as a dev dependency.`
245+
`Since you've chosen to customize the theme, you should also explicitly install ${cyan(`vue`)} as a dev dependency.`
237246
)
238247
}
239248

240249
const tip = tips.length ? yellow([`\n\nTips:`, ...tips].join('\n- ')) : ``
250+
const dir = root ? ' ' + root : ''
251+
const pm = getPackageManger()
241252

242253
if (injectNpmScripts) {
243254
const scripts: Record<string, string> = {}
244-
245255
const prefix = addNpmScriptsPrefix ? `${npmScriptsPrefix}:` : ''
246256

247257
scripts[`${prefix}dev`] = `vitepress dev${dir}`
@@ -250,13 +260,9 @@ export function scaffold({
250260

251261
Object.assign(userPkg.scripts || (userPkg.scripts = {}), scripts)
252262
fs.writeFileSync(pkgPath, JSON.stringify(userPkg, null, 2))
253-
return `Done! Now run ${cyan(
254-
`${getPackageManger()} run ${prefix}dev`
255-
)} and start writing.${tip}`
263+
264+
return `Done! Now run ${cyan(`${pm} run ${prefix}dev`)} and start writing.${tip}`
256265
} else {
257-
const pm = getPackageManger()
258-
return `You're all set! Now run ${cyan(
259-
`${pm === 'npm' ? 'npx' : pm} vitepress dev${dir}`
260-
)} and start writing.${tip}`
266+
return `You're all set! Now run ${cyan(`${pm === 'npm' ? 'npx' : pm} vitepress dev${dir}`)} and start writing.${tip}`
261267
}
262268
}

0 commit comments

Comments
 (0)