From bc1d454c9401002931e0049ad209a8fd8ef457b4 Mon Sep 17 00:00:00 2001 From: hyperz111 Date: Mon, 1 Sep 2025 21:40:43 +0700 Subject: [PATCH] feat(init): validation to make sure config directory is inside cwd --- src/node/init/init.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/node/init/init.ts b/src/node/init/init.ts index 71120541..2fd275e7 100644 --- a/src/node/init/init.ts +++ b/src/node/init/init.ts @@ -49,9 +49,22 @@ export async function init(root?: string) { message: 'Where should VitePress initialize the config?', initialValue: './', defaultValue: './', - validate() { - // TODO make sure directory is inside - return undefined + validate(value) { + const cwd = slash(process.cwd()) + 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 } }) },