feat: bruno can run a collection by specifying "bru run"

This commit is contained in:
Anoop M D 2023-02-08 03:27:27 +05:30
parent 411c06f4cb
commit 2b384656b6

View File

@ -7,7 +7,7 @@ const { bruToEnvJson, getEnvVars } = require('../utils/bru');
const { rpad } = require('../utils/common'); const { rpad } = require('../utils/common');
const { bruToJson } = require('../utils/bru'); const { bruToJson } = require('../utils/bru');
const command = 'run <filename>'; const command = 'run [filename]';
const desc = 'Run a request'; const desc = 'Run a request';
const printRunSummary = (assertionResults, testResults) => { const printRunSummary = (assertionResults, testResults) => {
@ -38,6 +38,8 @@ const printRunSummary = (assertionResults, testResults) => {
}; };
const getBruFilesRecursively = (dir) => { const getBruFilesRecursively = (dir) => {
const environmentsPath = 'environments';
const getFilesInOrder = (dir) => { const getFilesInOrder = (dir) => {
let bruJsons = []; let bruJsons = [];
@ -48,7 +50,12 @@ const getBruFilesRecursively = (dir) => {
const filePath = path.join(currentPath, file); const filePath = path.join(currentPath, file);
const stats = fs.lstatSync(filePath); const stats = fs.lstatSync(filePath);
if (stats.isDirectory()) { // todo: we might need a ignore config inside bruno.json
if (stats.isDirectory() &&
filePath !== environmentsPath &&
!filePath.startsWith(".git") &&
!filePath.startsWith("node_modules")
) {
traverse(filePath); traverse(filePath);
} }
} }
@ -105,25 +112,37 @@ const builder = async (yargs) => {
const handler = async function (argv) { const handler = async function (argv) {
try { try {
const { let {
filename, filename,
env, env,
r: recursive r: recursive
} = argv; } = argv;
const collectionPath = process.cwd();
// todo
// right now, bru must be run from the root of the collection
// will add support in the future to run it from anywhere inside the collection
const brunoJsonPath = path.join(collectionPath, 'bruno.json');
const brunoJsonExists = await exists(brunoJsonPath);
if(!brunoJsonExists) {
console.error(chalk.red(`You can run only at the root of a collection`));
return;
}
if(filename && filename.length) {
const pathExists = await exists(filename); const pathExists = await exists(filename);
if(!pathExists) { if(!pathExists) {
console.error(chalk.red(`File or directory ${filename} does not exist`)); console.error(chalk.red(`File or directory ${filename} does not exist`));
return; return;
} }
} else {
filename = "./";
recursive = true;
}
// todo
// right now, bru must be run from the root of the collection
// will add support in the future to run it from anywhere inside the collection
const collectionPath = process.cwd();
const collectionVariables = {}; const collectionVariables = {};
let envVars = {}; let envVars = {};
if(env) { if(env) {
const envFile = path.join(collectionPath, 'environments', `${env}.bru`); const envFile = path.join(collectionPath, 'environments', `${env}.bru`);
const envPathExists = await exists(envFile); const envPathExists = await exists(envFile);