Skip to content

Commit

Permalink
fix(cli): generate mjs file on init if type: module is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Aug 8, 2023
1 parent 819eb51 commit 23d7511
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -124,6 +124,7 @@
"@types/debug": "^4.1.8",
"@types/escape-html": "^1.0.2",
"@types/fs-extra": "^11.0.1",
"@types/lodash.template": "^4.5.1",
"@types/mark.js": "^8.11.8",
"@types/markdown-it": "^13.0.0",
"@types/markdown-it-attrs": "^4.1.0",
Expand Down
13 changes: 13 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 29 additions & 28 deletions src/node/init/init.ts
Expand Up @@ -11,7 +11,6 @@ import fs from 'fs-extra'
import path from 'path'
import { black, cyan, bgCyan, bold, yellow } from 'picocolors'
import { fileURLToPath } from 'url'
// @ts-ignore
import template from 'lodash.template'

export enum ScaffoldThemeType {
Expand All @@ -30,13 +29,13 @@ export interface ScaffoldOptions {
}

export async function init() {
intro(bgCyan(bold(black(` Welcome to VitePress! `))))
intro(bgCyan(bold(black(' Welcome to VitePress! '))))

const options: ScaffoldOptions = await group(
{
root: () =>
text({
message: `Where should VitePress initialize the config?`,
message: 'Where should VitePress initialize the config?',
initialValue: './',
validate(value) {
// TODO make sure directory is inside
Expand All @@ -45,13 +44,13 @@ export async function init() {

title: () =>
text({
message: `Site title:`,
message: 'Site title:',
placeholder: 'My Awesome Project'
}),

description: () =>
text({
message: `Site description:`,
message: 'Site description:',
placeholder: 'A VitePress Site'
}),

Expand All @@ -60,22 +59,19 @@ export async function init() {
message: 'Theme:',
options: [
{
// @ts-ignore
value: ScaffoldThemeType.Default,
label: `Default Theme`,
hint: `Out of the box, good-looking docs`
label: 'Default Theme',
hint: 'Out of the box, good-looking docs'
},
{
// @ts-ignore
value: ScaffoldThemeType.DefaultCustom,
label: `Default Theme + Customization`,
hint: `Add custom CSS and layout slots`
label: 'Default Theme + Customization',
hint: 'Add custom CSS and layout slots'
},
{
// @ts-ignore
value: ScaffoldThemeType.Custom,
label: `Custom Theme`,
hint: `Build your own or use external`
label: 'Custom Theme',
hint: 'Build your own or use external'
}
]
}),
Expand All @@ -85,7 +81,7 @@ export async function init() {

injectNpmScripts: () =>
confirm({
message: `Add VitePress npm scripts to package.json?`
message: 'Add VitePress npm scripts to package.json?'
})
},
{
Expand Down Expand Up @@ -122,11 +118,21 @@ export function scaffold({
theme === ScaffoldThemeType.DefaultCustom
}

const pkgPath = path.resolve('package.json')
const userPkg = fs.existsSync(pkgPath)
? JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
: {}

const useMjs = userPkg.type !== 'module'

const renderFile = (file: string) => {
const filePath = path.resolve(templateDir, file)
let targetPath = path.resolve(resolvedRoot, file)
if (useMjs && targetPath.includes('.vitepress/config')) {
targetPath = targetPath.replace(/\.js$/, '.mjs')
}
if (useTs) {
targetPath = targetPath.replace(/\.js$/, '.ts')
targetPath = targetPath.replace(/\.(m?)js$/, '.$1ts')
}
const src = fs.readFileSync(filePath, 'utf-8')
const compiled = template(src)(data)
Expand All @@ -137,19 +143,19 @@ export function scaffold({
'index.md',
'api-examples.md',
'markdown-examples.md',
`.vitepress/config.js`
'.vitepress/config.js'
]

if (theme === ScaffoldThemeType.DefaultCustom) {
filesToScaffold.push(
`.vitepress/theme/index.js`,
`.vitepress/theme/style.css`
'.vitepress/theme/index.js',
'.vitepress/theme/style.css'
)
} else if (theme === ScaffoldThemeType.Custom) {
filesToScaffold.push(
`.vitepress/theme/index.js`,
`.vitepress/theme/style.css`,
`.vitepress/theme/Layout.vue`
'.vitepress/theme/index.js',
'.vitepress/theme/style.css',
'.vitepress/theme/Layout.vue'
)
}

Expand All @@ -158,14 +164,9 @@ export function scaffold({
}

const dir =
root === './' ? `` : ` ${root.replace(/^\.\//, '').replace(/[/\\]$/, '')}`
root === './' ? '' : ` ${root.replace(/^\.\//, '').replace(/[/\\]$/, '')}`
const gitignorePrefix = dir ? `${dir}/.vitepress` : '.vitepress'

const pkgPath = path.resolve('package.json')
const userPkg = fs.existsSync(pkgPath)
? JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
: {}

const tips = []
if (fs.existsSync('.git')) {
tips.push(
Expand Down

0 comments on commit 23d7511

Please sign in to comment.