feat(init): validation to make sure config directory is inside cwd

pull/4931/head
hyperz111 2 weeks ago
parent c2eaccd0d2
commit bc1d454c94

@ -49,9 +49,22 @@ export async function init(root?: string) {
message: 'Where should VitePress initialize the config?', message: 'Where should VitePress initialize the config?',
initialValue: './', initialValue: './',
defaultValue: './', defaultValue: './',
validate() { validate(value) {
// TODO make sure directory is inside const cwd = slash(process.cwd())
return undefined const dir = slash(value as string)
const cwdRE = new RegExp(`^${cwd}`, 'u')
// If give absolute path, (C: in Windows and / in POSIX like),
// use that path instead
const absolutePath =
(process.platform === 'win32' && /^[A-Z]:/i.test(dir)) ||
/^\//.test(dir)
? dir
: path.join(cwd, dir)
return !cwdRE.test(absolutePath)
? 'Please init your config inside this directory.'
: undefined
} }
}) })
}, },

Loading…
Cancel
Save