@@ -12,6 +12,7 @@ import template from 'lodash.template'
12
12
import path from 'node:path'
13
13
import { fileURLToPath } from 'node:url'
14
14
import { bold , cyan , yellow } from 'picocolors'
15
+ import { slash } from '../shared'
15
16
16
17
export enum ScaffoldThemeType {
17
18
Default = 'default theme' ,
@@ -20,13 +21,13 @@ export enum ScaffoldThemeType {
20
21
}
21
22
22
23
export interface ScaffoldOptions {
23
- root : string
24
- srcDir : string
24
+ root ? : string
25
+ srcDir ? : string
25
26
title ?: string
26
27
description ?: string
27
- theme : ScaffoldThemeType
28
- useTs : boolean
29
- injectNpmScripts : boolean
28
+ theme ? : ScaffoldThemeType
29
+ useTs ? : boolean
30
+ injectNpmScripts ? : boolean
30
31
addNpmScriptsPrefix ?: boolean
31
32
npmScriptsPrefix ?: string
32
33
}
@@ -36,91 +37,99 @@ const getPackageManger = () => {
36
37
return name . split ( '/' ) [ 0 ]
37
38
}
38
39
39
- export async function init ( root : string | undefined ) {
40
+ export async function init ( root ? : string ) {
40
41
intro ( bold ( cyan ( 'Welcome to VitePress!' ) ) )
41
42
42
- const options : ScaffoldOptions = ( await group (
43
+ const options = await group (
43
44
{
44
45
root : async ( ) => {
45
46
if ( root ) return root
46
47
47
48
return text ( {
48
49
message : 'Where should VitePress initialize the config?' ,
49
50
initialValue : './' ,
51
+ defaultValue : './' ,
50
52
validate ( value ) {
51
53
// TODO make sure directory is inside
52
54
return undefined
53
55
}
54
56
} )
55
57
} ,
56
58
57
- srcDir : async ( ) => {
59
+ srcDir : async ( { results } : any ) => {
58
60
return text ( {
59
61
message : 'Where should VitePress look for your markdown files?' ,
60
- initialValue : './'
62
+ initialValue : results . root ,
63
+ defaultValue : results . root
61
64
} )
62
65
} ,
63
66
64
- title : ( ) =>
65
- text ( {
67
+ title : async ( ) => {
68
+ return text ( {
66
69
message : 'Site title:' ,
67
- placeholder : 'My Awesome Project'
68
- } ) ,
70
+ placeholder : 'My Awesome Project' ,
71
+ defaultValue : 'My Awesome Project'
72
+ } )
73
+ } ,
69
74
70
- description : ( ) =>
71
- text ( {
75
+ description : async ( ) => {
76
+ return text ( {
72
77
message : 'Site description:' ,
73
- placeholder : 'A VitePress Site'
74
- } ) ,
78
+ placeholder : 'A VitePress Site' ,
79
+ defaultValue : 'A VitePress Site'
80
+ } )
81
+ } ,
75
82
76
- theme : ( ) =>
77
- select ( {
83
+ theme : async ( ) => {
84
+ return select ( {
78
85
message : 'Theme:' ,
79
86
options : [
80
87
{
81
- // @ts -ignore
82
88
value : ScaffoldThemeType . Default ,
83
89
label : 'Default Theme' ,
84
90
hint : 'Out of the box, good-looking docs'
85
91
} ,
86
92
{
87
- // @ts -ignore
88
93
value : ScaffoldThemeType . DefaultCustom ,
89
94
label : 'Default Theme + Customization' ,
90
95
hint : 'Add custom CSS and layout slots'
91
96
} ,
92
97
{
93
- // @ts -ignore
94
98
value : ScaffoldThemeType . Custom ,
95
99
label : 'Custom Theme' ,
96
100
hint : 'Build your own or use external'
97
101
}
98
102
]
99
- } ) ,
103
+ } )
104
+ } ,
100
105
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
+ } ,
103
111
104
- injectNpmScripts : ( ) =>
105
- confirm ( {
112
+ injectNpmScripts : async ( ) => {
113
+ return confirm ( {
106
114
message : 'Add VitePress npm scripts to package.json?'
107
- } ) ,
115
+ } )
116
+ } ,
108
117
109
- addNpmScriptsPrefix : ( { results } ) => {
110
- if ( ! results . injectNpmScripts ) return Promise . resolve ( false )
118
+ addNpmScriptsPrefix : async ( { results } : any ) => {
119
+ if ( ! results . injectNpmScripts ) return false
111
120
112
121
return confirm ( {
113
- message : 'Add a prefix for VitePress npm scripts?' ,
114
- initialValue : true
122
+ message : 'Add a prefix for VitePress npm scripts?'
115
123
} )
116
124
} ,
117
125
118
- npmScriptsPrefix : ( { results } ) => {
119
- if ( ! results . addNpmScriptsPrefix ) return Promise . resolve ( 'docs' )
126
+ npmScriptsPrefix : async ( { results } : any ) => {
127
+ if ( ! results . addNpmScriptsPrefix ) return 'docs'
120
128
121
129
return text ( {
122
130
message : 'Prefix for VitePress npm scripts:' ,
123
- placeholder : 'docs'
131
+ placeholder : 'docs' ,
132
+ defaultValue : 'docs'
124
133
} )
125
134
}
126
135
} ,
@@ -130,31 +139,35 @@ export async function init(root: string | undefined) {
130
139
process . exit ( 0 )
131
140
}
132
141
}
133
- ) ) as ScaffoldOptions
142
+ )
134
143
135
144
outro ( scaffold ( options ) )
136
145
}
137
146
138
147
export function scaffold ( {
139
- root = './' ,
140
- srcDir = './' ,
148
+ root : root_ = './' ,
149
+ srcDir : srcDir_ = root_ ,
141
150
title = 'My Awesome Project' ,
142
151
description = 'A VitePress Site' ,
143
- theme,
144
- useTs,
145
- injectNpmScripts,
152
+ theme = ScaffoldThemeType . Default ,
153
+ useTs = true ,
154
+ injectNpmScripts = true ,
146
155
addNpmScriptsPrefix = true ,
147
156
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
+
151
164
const templateDir = path . resolve (
152
165
path . dirname ( fileURLToPath ( import . meta. url ) ) ,
153
166
'../../template'
154
167
)
155
168
156
169
const data = {
157
- srcDir : srcDir === './' ? undefined : JSON . stringify ( srcDir ) , // omit if default
170
+ srcDir : srcDir ? JSON . stringify ( srcDir ) : undefined , // omit if default
158
171
title : JSON . stringify ( title ) ,
159
172
description : JSON . stringify ( description ) ,
160
173
useTs,
@@ -214,34 +227,31 @@ export function scaffold({
214
227
renderFile ( file )
215
228
}
216
229
217
- const dir =
218
- root === './' ? '' : ` ${ root . replace ( / ^ \. \/ / , '' ) . replace ( / [ / \\ ] $ / , '' ) } `
219
- const gitignorePrefix = dir ? `${ dir } /.vitepress` : '.vitepress'
220
-
221
230
const tips = [ ]
231
+
232
+ const gitignorePrefix = root ? `${ slash ( root ) } /.vitepress` : '.vitepress'
222
233
if ( fs . existsSync ( '.git' ) ) {
223
234
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.`
227
236
)
228
237
}
238
+
229
239
if (
230
240
theme !== ScaffoldThemeType . Default &&
231
241
! userPkg . dependencies ?. [ 'vue' ] &&
232
242
! userPkg . devDependencies ?. [ 'vue' ]
233
243
) {
234
244
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.`
237
246
)
238
247
}
239
248
240
249
const tip = tips . length ? yellow ( [ `\n\nTips:` , ...tips ] . join ( '\n- ' ) ) : ``
250
+ const dir = root ? ' ' + root : ''
251
+ const pm = getPackageManger ( )
241
252
242
253
if ( injectNpmScripts ) {
243
254
const scripts : Record < string , string > = { }
244
-
245
255
const prefix = addNpmScriptsPrefix ? `${ npmScriptsPrefix } :` : ''
246
256
247
257
scripts [ `${ prefix } dev` ] = `vitepress dev${ dir } `
@@ -250,13 +260,9 @@ export function scaffold({
250
260
251
261
Object . assign ( userPkg . scripts || ( userPkg . scripts = { } ) , scripts )
252
262
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 } `
256
265
} 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 } `
261
267
}
262
268
}
0 commit comments