@@ -11,7 +11,6 @@ import fs from 'fs-extra'
11
11
import path from 'path'
12
12
import { black , cyan , bgCyan , bold , yellow } from 'picocolors'
13
13
import { fileURLToPath } from 'url'
14
- // @ts -ignore
15
14
import template from 'lodash.template'
16
15
17
16
export enum ScaffoldThemeType {
@@ -30,13 +29,13 @@ export interface ScaffoldOptions {
30
29
}
31
30
32
31
export async function init ( ) {
33
- intro ( bgCyan ( bold ( black ( ` Welcome to VitePress! ` ) ) ) )
32
+ intro ( bgCyan ( bold ( black ( ' Welcome to VitePress! ' ) ) ) )
34
33
35
34
const options : ScaffoldOptions = await group (
36
35
{
37
36
root : ( ) =>
38
37
text ( {
39
- message : ` Where should VitePress initialize the config?` ,
38
+ message : ' Where should VitePress initialize the config?' ,
40
39
initialValue : './' ,
41
40
validate ( value ) {
42
41
// TODO make sure directory is inside
@@ -45,13 +44,13 @@ export async function init() {
45
44
46
45
title : ( ) =>
47
46
text ( {
48
- message : ` Site title:` ,
47
+ message : ' Site title:' ,
49
48
placeholder : 'My Awesome Project'
50
49
} ) ,
51
50
52
51
description : ( ) =>
53
52
text ( {
54
- message : ` Site description:` ,
53
+ message : ' Site description:' ,
55
54
placeholder : 'A VitePress Site'
56
55
} ) ,
57
56
@@ -60,22 +59,19 @@ export async function init() {
60
59
message : 'Theme:' ,
61
60
options : [
62
61
{
63
- // @ts -ignore
64
62
value : ScaffoldThemeType . Default ,
65
- label : ` Default Theme` ,
66
- hint : ` Out of the box, good-looking docs`
63
+ label : ' Default Theme' ,
64
+ hint : ' Out of the box, good-looking docs'
67
65
} ,
68
66
{
69
- // @ts -ignore
70
67
value : ScaffoldThemeType . DefaultCustom ,
71
- label : ` Default Theme + Customization` ,
72
- hint : ` Add custom CSS and layout slots`
68
+ label : ' Default Theme + Customization' ,
69
+ hint : ' Add custom CSS and layout slots'
73
70
} ,
74
71
{
75
- // @ts -ignore
76
72
value : ScaffoldThemeType . Custom ,
77
- label : ` Custom Theme` ,
78
- hint : ` Build your own or use external`
73
+ label : ' Custom Theme' ,
74
+ hint : ' Build your own or use external'
79
75
}
80
76
]
81
77
} ) ,
@@ -85,7 +81,7 @@ export async function init() {
85
81
86
82
injectNpmScripts : ( ) =>
87
83
confirm ( {
88
- message : ` Add VitePress npm scripts to package.json?`
84
+ message : ' Add VitePress npm scripts to package.json?'
89
85
} )
90
86
} ,
91
87
{
@@ -122,11 +118,21 @@ export function scaffold({
122
118
theme === ScaffoldThemeType . DefaultCustom
123
119
}
124
120
121
+ const pkgPath = path . resolve ( 'package.json' )
122
+ const userPkg = fs . existsSync ( pkgPath )
123
+ ? JSON . parse ( fs . readFileSync ( pkgPath , 'utf-8' ) )
124
+ : { }
125
+
126
+ const useMjs = userPkg . type !== 'module'
127
+
125
128
const renderFile = ( file : string ) => {
126
129
const filePath = path . resolve ( templateDir , file )
127
130
let targetPath = path . resolve ( resolvedRoot , file )
131
+ if ( useMjs && targetPath . includes ( '.vitepress/config' ) ) {
132
+ targetPath = targetPath . replace ( / \. j s $ / , '.mjs' )
133
+ }
128
134
if ( useTs ) {
129
- targetPath = targetPath . replace ( / \. j s $ / , '.ts ' )
135
+ targetPath = targetPath . replace ( / \. ( m ? ) j s $ / , '.$1ts ' )
130
136
}
131
137
const src = fs . readFileSync ( filePath , 'utf-8' )
132
138
const compiled = template ( src ) ( data )
@@ -137,19 +143,19 @@ export function scaffold({
137
143
'index.md' ,
138
144
'api-examples.md' ,
139
145
'markdown-examples.md' ,
140
- ` .vitepress/config.js`
146
+ ' .vitepress/config.js'
141
147
]
142
148
143
149
if ( theme === ScaffoldThemeType . DefaultCustom ) {
144
150
filesToScaffold . push (
145
- ` .vitepress/theme/index.js` ,
146
- ` .vitepress/theme/style.css`
151
+ ' .vitepress/theme/index.js' ,
152
+ ' .vitepress/theme/style.css'
147
153
)
148
154
} else if ( theme === ScaffoldThemeType . Custom ) {
149
155
filesToScaffold . push (
150
- ` .vitepress/theme/index.js` ,
151
- ` .vitepress/theme/style.css` ,
152
- ` .vitepress/theme/Layout.vue`
156
+ ' .vitepress/theme/index.js' ,
157
+ ' .vitepress/theme/style.css' ,
158
+ ' .vitepress/theme/Layout.vue'
153
159
)
154
160
}
155
161
@@ -158,14 +164,9 @@ export function scaffold({
158
164
}
159
165
160
166
const dir =
161
- root === './' ? `` : ` ${ root . replace ( / ^ \. \/ / , '' ) . replace ( / [ / \\ ] $ / , '' ) } `
167
+ root === './' ? '' : ` ${ root . replace ( / ^ \. \/ / , '' ) . replace ( / [ / \\ ] $ / , '' ) } `
162
168
const gitignorePrefix = dir ? `${ dir } /.vitepress` : '.vitepress'
163
169
164
- const pkgPath = path . resolve ( 'package.json' )
165
- const userPkg = fs . existsSync ( pkgPath )
166
- ? JSON . parse ( fs . readFileSync ( pkgPath , 'utf-8' ) )
167
- : { }
168
-
169
170
const tips = [ ]
170
171
if ( fs . existsSync ( '.git' ) ) {
171
172
tips . push (
0 commit comments