mirror of
https://github.com/usebruno/bruno.git
synced 2024-11-25 17:33:28 +01:00
feat: bruno cli can not run a folder recursively
This commit is contained in:
parent
03fa46d8b3
commit
411c06f4cb
@ -1,7 +1,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const chalk = require('chalk');
|
const chalk = require('chalk');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { exists, isFile, isDirectory } = require('../utils/filesystem');
|
const { exists, isFile, isDirectory, getSubDirectories } = require('../utils/filesystem');
|
||||||
const { runSingleRequest } = require('../runner/run-single-request');
|
const { runSingleRequest } = require('../runner/run-single-request');
|
||||||
const { bruToEnvJson, getEnvVars } = require('../utils/bru');
|
const { bruToEnvJson, getEnvVars } = require('../utils/bru');
|
||||||
const { rpad } = require('../utils/common');
|
const { rpad } = require('../utils/common');
|
||||||
@ -37,21 +37,78 @@ const printRunSummary = (assertionResults, testResults) => {
|
|||||||
console.log(chalk.bold(testSummary));
|
console.log(chalk.bold(testSummary));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const getBruFilesRecursively = (dir) => {
|
||||||
|
const getFilesInOrder = (dir) => {
|
||||||
|
let bruJsons = [];
|
||||||
|
|
||||||
|
const traverse = (currentPath) => {
|
||||||
|
const filesInCurrentDir = fs.readdirSync(currentPath);
|
||||||
|
|
||||||
|
for (const file of filesInCurrentDir) {
|
||||||
|
const filePath = path.join(currentPath, file);
|
||||||
|
const stats = fs.lstatSync(filePath);
|
||||||
|
|
||||||
|
if (stats.isDirectory()) {
|
||||||
|
traverse(filePath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentDirBruJsons = [];
|
||||||
|
for (const file of filesInCurrentDir) {
|
||||||
|
const filePath = path.join(currentPath, file);
|
||||||
|
const stats = fs.lstatSync(filePath);
|
||||||
|
|
||||||
|
if (!stats.isDirectory() && path.extname(filePath) === '.bru') {
|
||||||
|
const bruContent = fs.readFileSync(filePath, 'utf8');
|
||||||
|
const bruJson = bruToJson(bruContent);
|
||||||
|
currentDirBruJsons.push({
|
||||||
|
bruFilepath: filePath,
|
||||||
|
bruJson
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// order requests by sequence
|
||||||
|
currentDirBruJsons.sort((a, b) => {
|
||||||
|
const aSequence = a.bruJson.seq || 0;
|
||||||
|
const bSequence = b.bruJson.seq || 0;
|
||||||
|
return aSequence - bSequence;
|
||||||
|
});
|
||||||
|
|
||||||
|
bruJsons = bruJsons.concat(currentDirBruJsons);
|
||||||
|
};
|
||||||
|
|
||||||
|
traverse(dir);
|
||||||
|
return bruJsons;
|
||||||
|
};
|
||||||
|
|
||||||
|
const bruJsons = getFilesInOrder(dir);
|
||||||
|
return bruJsons;
|
||||||
|
};
|
||||||
|
|
||||||
const builder = async (yargs) => {
|
const builder = async (yargs) => {
|
||||||
yargs
|
yargs
|
||||||
|
.option('r', {
|
||||||
|
describe: 'Indicates a recursive run',
|
||||||
|
type: 'boolean',
|
||||||
|
default: false
|
||||||
|
})
|
||||||
.option('env', {
|
.option('env', {
|
||||||
describe: 'Environment variables',
|
describe: 'Environment variables',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
})
|
})
|
||||||
.example('$0 run request.bru', 'Run a request')
|
.example('$0 run request.bru', 'Run a request')
|
||||||
.example('$0 run request.bru --env local', 'Run a request with the environment set to local');
|
.example('$0 run request.bru --env local', 'Run a request with the environment set to local')
|
||||||
|
.example('$0 run folder', 'Run all requests in a folder')
|
||||||
|
.example('$0 run folder -r', 'Run all requests in a folder recursively')
|
||||||
};
|
};
|
||||||
|
|
||||||
const handler = async function (argv) {
|
const handler = async function (argv) {
|
||||||
try {
|
try {
|
||||||
const {
|
const {
|
||||||
filename,
|
filename,
|
||||||
env
|
env,
|
||||||
|
r: recursive
|
||||||
} = argv;
|
} = argv;
|
||||||
|
|
||||||
const pathExists = await exists(filename);
|
const pathExists = await exists(filename);
|
||||||
@ -97,27 +154,33 @@ const handler = async function (argv) {
|
|||||||
|
|
||||||
const _isDirectory = await isDirectory(filename);
|
const _isDirectory = await isDirectory(filename);
|
||||||
if(_isDirectory) {
|
if(_isDirectory) {
|
||||||
console.log(chalk.yellow('Running Collection \n'));
|
let bruJsons = [];
|
||||||
|
if(!recursive) {
|
||||||
|
console.log(chalk.yellow('Running Folder \n'));
|
||||||
|
const files = fs.readdirSync(filename);
|
||||||
|
const bruFiles = files.filter((file) => file.endsWith('.bru'));
|
||||||
|
|
||||||
const files = fs.readdirSync(filename);
|
for (const bruFile of bruFiles) {
|
||||||
const bruFiles = files.filter((file) => file.endsWith('.bru'));
|
const bruFilepath = path.join(filename, bruFile)
|
||||||
const bruJsons = [];
|
const bruContent = fs.readFileSync(bruFilepath, 'utf8');
|
||||||
for (const bruFile of bruFiles) {
|
const bruJson = bruToJson(bruContent);
|
||||||
const bruFilepath = path.join(filename, bruFile)
|
bruJsons.push({
|
||||||
const bruContent = fs.readFileSync(bruFilepath, 'utf8');
|
bruFilepath,
|
||||||
const bruJson = bruToJson(bruContent);
|
bruJson
|
||||||
bruJsons.push({
|
});
|
||||||
bruFilepath,
|
}
|
||||||
bruJson
|
|
||||||
|
// order requests by sequence
|
||||||
|
bruJsons.sort((a, b) => {
|
||||||
|
const aSequence = a.bruJson.seq || 0;
|
||||||
|
const bSequence = b.bruJson.seq || 0;
|
||||||
|
return aSequence - bSequence;
|
||||||
});
|
});
|
||||||
}
|
} else {
|
||||||
|
console.log(chalk.yellow('Running Folder Recursively \n'));
|
||||||
|
|
||||||
// order requests by sequence
|
bruJsons = await getBruFilesRecursively(filename);
|
||||||
bruJsons.sort((a, b) => {
|
}
|
||||||
const aSequence = a.bruJson.seq || 0;
|
|
||||||
const bSequence = b.bruJson.seq || 0;
|
|
||||||
return aSequence - bSequence;
|
|
||||||
});
|
|
||||||
|
|
||||||
let assertionResults = [];
|
let assertionResults = [];
|
||||||
let testResults = [];
|
let testResults = [];
|
||||||
|
@ -103,6 +103,21 @@ const stripExtension = (filename = '') => {
|
|||||||
return filename.replace(/\.[^/.]+$/, "");
|
return filename.replace(/\.[^/.]+$/, "");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getSubDirectories = (dir) => {
|
||||||
|
try {
|
||||||
|
const files = fs.readdirSync(dir);
|
||||||
|
const subDirectories = files
|
||||||
|
.filter((file) => {
|
||||||
|
return fs.lstatSync(path.join(dir, file)).isDirectory();
|
||||||
|
})
|
||||||
|
.sort();
|
||||||
|
|
||||||
|
return subDirectories;
|
||||||
|
} catch (err) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
exists,
|
exists,
|
||||||
isSymbolicLink,
|
isSymbolicLink,
|
||||||
@ -115,5 +130,6 @@ module.exports = {
|
|||||||
createDirectory,
|
createDirectory,
|
||||||
searchForFiles,
|
searchForFiles,
|
||||||
searchForBruFiles,
|
searchForBruFiles,
|
||||||
stripExtension
|
stripExtension,
|
||||||
|
getSubDirectories
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user