From aac27b3f83264d81d330a4be9db6d0be53bf1fd8 Mon Sep 17 00:00:00 2001 From: undefined Date: Sat, 4 Feb 2023 02:18:55 +0800 Subject: [PATCH] core: cli: detect mongosh --- packages/hydrooj/bin/commands.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/hydrooj/bin/commands.ts b/packages/hydrooj/bin/commands.ts index 8c94845d..1ec11f30 100644 --- a/packages/hydrooj/bin/commands.ts +++ b/packages/hydrooj/bin/commands.ts @@ -60,7 +60,14 @@ if (!argv.args[0] || argv.args[0] === 'cli') { cli.command('db').action(() => { const dbConfig = fs.readFileSync(path.resolve(hydroPath, 'config.json'), 'utf-8'); const url = buildUrl(JSON.parse(dbConfig)); - child.spawn('mongo', [url], { stdio: 'inherit' }); + try { + console.log('Detecting mongosh...'); + const mongosh = child.execSync('mongosh --version').toString(); + if (/\d+\.\d+\.\d+/.test(mongosh)) child.spawn('mongosh', [url], { stdio: 'inherit' }); + } catch (e) { + console.log('Cannot run mongosh. Trying legacy mongo client...'); + child.spawn('mongo', [url], { stdio: 'inherit' }); + } }); cli.command('backup').action(() => { const dbConfig = fs.readFileSync(path.resolve(hydroPath, 'config.json'), 'utf-8');