feat: bru cli - specify env + completed vars runtime

This commit is contained in:
Anoop M D
2023-02-07 21:01:35 +05:30
parent 78ca6c5e96
commit 3f74178c81
7 changed files with 162 additions and 89 deletions

View File

@@ -1,27 +1,34 @@
const fs = require('fs');
const chalk = require('chalk');
const {
exists,
isFile,
isDirectory
} = require('../utils/filesystem');
const {
runSingleRequest
} = require('../runner/run-single-request');
const path = require('path');
const { exists, isFile } = require('../utils/filesystem');
const { runSingleRequest } = require('../runner/run-single-request');
const { bruToEnvJson, getEnvVars } = require('../utils/bru');
const command = 'run <filename>';
const desc = 'Run a request';
const builder = async (yargs) => {
yargs.example('$0 run request.bru', 'Run a request');
yargs
.option('env', {
describe: 'Environment variables',
type: 'string',
})
.example('$0 run request.bru', 'Run a request')
.example('$0 run request.bru --env local', 'Run a request with the environment set to local');
};
const handler = async function (argv) {
try {
const { filename } = argv;
const {
filename,
env
} = argv;
const pathExists = await exists(filename);
if(!pathExists) {
console.error(chalk.red(`File or directory ${filename} does not exist`));
return;
}
// todo
@@ -30,14 +37,29 @@ const handler = async function (argv) {
const collectionPath = process.cwd();
const collectionVariables = {};
let envVars = {};
if(env) {
const envFile = path.join(collectionPath, 'environments', `${env}.bru`);
const envPathExists = await exists(envFile);
if(!envPathExists) {
console.error(chalk.red(`Environment file not found: `) + chalk.dim(`environments/${env}.bru`));
return;
}
const envBruContent = fs.readFileSync(envFile, 'utf8');
const envJson = bruToEnvJson(envBruContent);
envVars = getEnvVars(envJson);
}
const _isFile = await isFile(filename);
if(_isFile) {
console.log(chalk.yellow('Running Request \n'));
await runSingleRequest(filename, collectionPath, collectionVariables);
await runSingleRequest(filename, collectionPath, collectionVariables, envVars);
console.log(chalk.green('\nDone!'));
}
} catch (err) {
console.error(err);
// console.error(err.message);
}
};