Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): support custom npm scripts prefix #4271

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions src/node/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export interface ScaffoldOptions {
theme: ScaffoldThemeType
useTs: boolean
injectNpmScripts: boolean
addNpmScriptsPrefix?: boolean
npmScriptsPrefix?: string
}

const getPackageManger = () => {
Expand All @@ -36,7 +38,7 @@ const getPackageManger = () => {
export async function init(root: string | undefined) {
intro(bold(cyan('Welcome to VitePress!')))

const options: ScaffoldOptions = await group(
const options: ScaffoldOptions = (await group(
{
root: async () => {
if (root) return root
Expand Down Expand Up @@ -93,15 +95,33 @@ export async function init(root: string | undefined) {
injectNpmScripts: () =>
confirm({
message: 'Add VitePress npm scripts to package.json?'
}),

addNpmScriptsPrefix: ({ results }) => {
if (!results.injectNpmScripts) return Promise.resolve(false)

return confirm({
message: 'Add a prefix for VitePress npm scripts?',
initialValue: true
})
},

npmScriptsPrefix: ({ results }) => {
if (!results.addNpmScriptsPrefix) return Promise.resolve('docs')

return text({
message: 'Prefix for VitePress npm scripts:',
placeholder: 'docs'
})
}
},
{
onCancel: () => {
cancel('Cancelled.')
process.exit(0)
}
}
)
)) as ScaffoldOptions

outro(scaffold(options))
}
Expand All @@ -112,7 +132,9 @@ export function scaffold({
description = 'A VitePress Site',
theme,
useTs,
injectNpmScripts
injectNpmScripts,
addNpmScriptsPrefix = true,
npmScriptsPrefix = 'docs'
}: ScaffoldOptions): string {
const resolvedRoot = path.resolve(root)
const templateDir = path.resolve(
Expand Down Expand Up @@ -200,15 +222,18 @@ export function scaffold({
const tip = tips.length ? yellow([`\n\nTips:`, ...tips].join('\n- ')) : ``

if (injectNpmScripts) {
const scripts = {
'docs:dev': `vitepress dev${dir}`,
'docs:build': `vitepress build${dir}`,
'docs:preview': `vitepress preview${dir}`
}
const scripts: Record<string, string> = {}

const prefix = addNpmScriptsPrefix ? `${npmScriptsPrefix}:` : ''

scripts[`${prefix}dev`] = `vitepress dev${dir}`
scripts[`${prefix}build`] = `vitepress build${dir}`
scripts[`${prefix}preview`] = `vitepress preview${dir}`

Object.assign(userPkg.scripts || (userPkg.scripts = {}), scripts)
fs.writeFileSync(pkgPath, JSON.stringify(userPkg, null, 2))
return `Done! Now run ${cyan(
`${getPackageManger()} run docs:dev`
`${getPackageManger()} run ${prefix}dev`
)} and start writing.${tip}`
} else {
const pm = getPackageManger()
Expand Down