Skip to content

Commit 23d7511

Browse files
committedAug 8, 2023
fix(cli): generate mjs file on init if type: module is not present
1 parent 819eb51 commit 23d7511

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
"@types/debug": "^4.1.8",
125125
"@types/escape-html": "^1.0.2",
126126
"@types/fs-extra": "^11.0.1",
127+
"@types/lodash.template": "^4.5.1",
127128
"@types/mark.js": "^8.11.8",
128129
"@types/markdown-it": "^13.0.0",
129130
"@types/markdown-it-attrs": "^4.1.0",

‎pnpm-lock.yaml

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/node/init/init.ts

+29-28
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import fs from 'fs-extra'
1111
import path from 'path'
1212
import { black, cyan, bgCyan, bold, yellow } from 'picocolors'
1313
import { fileURLToPath } from 'url'
14-
// @ts-ignore
1514
import template from 'lodash.template'
1615

1716
export enum ScaffoldThemeType {
@@ -30,13 +29,13 @@ export interface ScaffoldOptions {
3029
}
3130

3231
export async function init() {
33-
intro(bgCyan(bold(black(` Welcome to VitePress! `))))
32+
intro(bgCyan(bold(black(' Welcome to VitePress! '))))
3433

3534
const options: ScaffoldOptions = await group(
3635
{
3736
root: () =>
3837
text({
39-
message: `Where should VitePress initialize the config?`,
38+
message: 'Where should VitePress initialize the config?',
4039
initialValue: './',
4140
validate(value) {
4241
// TODO make sure directory is inside
@@ -45,13 +44,13 @@ export async function init() {
4544

4645
title: () =>
4746
text({
48-
message: `Site title:`,
47+
message: 'Site title:',
4948
placeholder: 'My Awesome Project'
5049
}),
5150

5251
description: () =>
5352
text({
54-
message: `Site description:`,
53+
message: 'Site description:',
5554
placeholder: 'A VitePress Site'
5655
}),
5756

@@ -60,22 +59,19 @@ export async function init() {
6059
message: 'Theme:',
6160
options: [
6261
{
63-
// @ts-ignore
6462
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'
6765
},
6866
{
69-
// @ts-ignore
7067
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'
7370
},
7471
{
75-
// @ts-ignore
7672
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'
7975
}
8076
]
8177
}),
@@ -85,7 +81,7 @@ export async function init() {
8581

8682
injectNpmScripts: () =>
8783
confirm({
88-
message: `Add VitePress npm scripts to package.json?`
84+
message: 'Add VitePress npm scripts to package.json?'
8985
})
9086
},
9187
{
@@ -122,11 +118,21 @@ export function scaffold({
122118
theme === ScaffoldThemeType.DefaultCustom
123119
}
124120

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+
125128
const renderFile = (file: string) => {
126129
const filePath = path.resolve(templateDir, file)
127130
let targetPath = path.resolve(resolvedRoot, file)
131+
if (useMjs && targetPath.includes('.vitepress/config')) {
132+
targetPath = targetPath.replace(/\.js$/, '.mjs')
133+
}
128134
if (useTs) {
129-
targetPath = targetPath.replace(/\.js$/, '.ts')
135+
targetPath = targetPath.replace(/\.(m?)js$/, '.$1ts')
130136
}
131137
const src = fs.readFileSync(filePath, 'utf-8')
132138
const compiled = template(src)(data)
@@ -137,19 +143,19 @@ export function scaffold({
137143
'index.md',
138144
'api-examples.md',
139145
'markdown-examples.md',
140-
`.vitepress/config.js`
146+
'.vitepress/config.js'
141147
]
142148

143149
if (theme === ScaffoldThemeType.DefaultCustom) {
144150
filesToScaffold.push(
145-
`.vitepress/theme/index.js`,
146-
`.vitepress/theme/style.css`
151+
'.vitepress/theme/index.js',
152+
'.vitepress/theme/style.css'
147153
)
148154
} else if (theme === ScaffoldThemeType.Custom) {
149155
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'
153159
)
154160
}
155161

@@ -158,14 +164,9 @@ export function scaffold({
158164
}
159165

160166
const dir =
161-
root === './' ? `` : ` ${root.replace(/^\.\//, '').replace(/[/\\]$/, '')}`
167+
root === './' ? '' : ` ${root.replace(/^\.\//, '').replace(/[/\\]$/, '')}`
162168
const gitignorePrefix = dir ? `${dir}/.vitepress` : '.vitepress'
163169

164-
const pkgPath = path.resolve('package.json')
165-
const userPkg = fs.existsSync(pkgPath)
166-
? JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
167-
: {}
168-
169170
const tips = []
170171
if (fs.existsSync('.git')) {
171172
tips.push(

0 commit comments

Comments
 (0)
Please sign in to comment.