@@ -7,116 +7,90 @@ const { parseRequest, parseCollection, parseFolder, stringifyCollection, stringi
77const constants = require ( '../constants' ) ;
88const chalk = require ( 'chalk' ) ;
99
10- const createCollectionJsonFromPathname = ( collectionPath ) => {
11- const environmentsPath = path . join ( collectionPath , `environments` ) ;
10+ const FORMAT_CONFIG = {
11+ yml : { ext : '.yml' , collectionFile : 'opencollection.yml' , folderFile : 'folder.yml' } ,
12+ bru : { ext : '.bru' , collectionFile : 'collection.bru' , folderFile : 'folder.bru' }
13+ } ;
1214
13- // get the collection bruno json config [<collection-path>/bruno.json]
14- const brunoConfig = getCollectionBrunoJsonConfig ( collectionPath ) ;
15+ const getCollectionFormat = ( collectionPath ) => {
16+ if ( fs . existsSync ( path . join ( collectionPath , 'opencollection.yml' ) ) ) return 'yml' ;
17+ if ( fs . existsSync ( path . join ( collectionPath , 'bruno.json' ) ) ) return 'bru' ;
18+ return null ;
19+ } ;
1520
16- // get the collection root [<collection-path>/collection.bru]
17- const collectionRoot = getCollectionRoot ( collectionPath ) ;
21+ const getCollectionConfig = ( collectionPath , format ) => {
22+ if ( format === 'yml' ) {
23+ const content = fs . readFileSync ( path . join ( collectionPath , 'opencollection.yml' ) , 'utf8' ) ;
24+ const parsed = parseCollection ( content , { format : 'yml' } ) ;
25+ return { brunoConfig : parsed . brunoConfig , collectionRoot : parsed . collectionRoot || { } } ;
26+ }
27+ const brunoConfig = JSON . parse ( fs . readFileSync ( path . join ( collectionPath , 'bruno.json' ) , 'utf8' ) ) ;
28+ const collectionBruPath = path . join ( collectionPath , 'collection.bru' ) ;
29+ const collectionRoot = fs . existsSync ( collectionBruPath )
30+ ? parseCollection ( fs . readFileSync ( collectionBruPath , 'utf8' ) , { format : 'bru' } )
31+ : { } ;
32+ return { brunoConfig, collectionRoot } ;
33+ } ;
34+
35+ const getFolderRoot = ( dir , format ) => {
36+ const folderPath = path . join ( dir , FORMAT_CONFIG [ format ] . folderFile ) ;
37+ if ( ! fs . existsSync ( folderPath ) ) return null ;
38+ return parseFolder ( fs . readFileSync ( folderPath , 'utf8' ) , { format } ) ;
39+ } ;
40+
41+ const createCollectionJsonFromPathname = ( collectionPath ) => {
42+ const format = getCollectionFormat ( collectionPath ) ;
43+ if ( ! format ) {
44+ console . error ( chalk . red ( `You can run only at the root of a collection` ) ) ;
45+ process . exit ( constants . EXIT_STATUS . ERROR_NOT_IN_COLLECTION ) ;
46+ }
47+
48+ const { brunoConfig, collectionRoot } = getCollectionConfig ( collectionPath , format ) ;
49+ const { ext, collectionFile, folderFile } = FORMAT_CONFIG [ format ] ;
50+ const environmentsPath = path . join ( collectionPath , 'environments' ) ;
1851
19- // get the collection items recursively
2052 const traverse = ( currentPath ) => {
21- const filesInCurrentDir = fs . readdirSync ( currentPath ) ;
22- if ( currentPath . includes ( 'node_modules' ) ) {
23- return ;
24- }
53+ if ( currentPath . includes ( 'node_modules' ) ) return [ ] ;
2554 const currentDirItems = [ ] ;
26- for ( const file of filesInCurrentDir ) {
55+
56+ for ( const file of fs . readdirSync ( currentPath ) ) {
2757 const filePath = path . join ( currentPath , file ) ;
2858 const stats = fs . lstatSync ( filePath ) ;
59+
2960 if ( stats . isDirectory ( ) ) {
30- if ( filePath === environmentsPath ) continue ;
31- if ( filePath . startsWith ( '.git' ) || filePath . startsWith ( 'node_modules' ) ) continue ;
32-
33- // get the folder root
34- let folderItem = { name : file , pathname : filePath , type : 'folder' , items : traverse ( filePath ) } ;
35- const folderBruJson = getFolderRoot ( filePath ) ;
36- if ( folderBruJson ) {
37- folderItem . root = folderBruJson ;
38- folderItem . seq = folderBruJson . meta . seq ;
61+ if ( filePath === environmentsPath || file === '.git' || file === 'node_modules' ) continue ;
62+ const folderItem = { name : file , pathname : filePath , type : 'folder' , items : traverse ( filePath ) } ;
63+ const folderRoot = getFolderRoot ( filePath , format ) ;
64+ if ( folderRoot ) {
65+ folderItem . root = folderRoot ;
66+ folderItem . seq = folderRoot . meta ?. seq ;
3967 }
4068 currentDirItems . push ( folderItem ) ;
4169 } else {
42- if ( [ 'collection.bru' , 'folder.bru' ] . includes ( file ) ) continue ;
43- if ( path . extname ( filePath ) !== '.bru' ) continue ;
44-
45- // get the request item
70+ if ( file === collectionFile || file === folderFile || path . extname ( filePath ) !== ext ) continue ;
4671 try {
47- const bruContent = fs . readFileSync ( filePath , 'utf8' ) ;
48- const requestItem = parseRequest ( bruContent ) ;
49- currentDirItems . push ( {
50- name : file ,
51- pathname : filePath ,
52- ...requestItem
53- } ) ;
72+ const requestItem = parseRequest ( fs . readFileSync ( filePath , 'utf8' ) , { format } ) ;
73+ currentDirItems . push ( { name : file , ...requestItem , pathname : filePath } ) ;
5474 } catch ( err ) {
55- // Log warning for invalid .bru file but continue processing
5675 console . warn ( chalk . yellow ( `Warning: Skipping invalid file ${ filePath } \nError: ${ err . message } ` ) ) ;
57- // Track skipped files for later reporting
58- if ( ! global . brunoSkippedFiles ) {
59- global . brunoSkippedFiles = [ ] ;
60- }
76+ global . brunoSkippedFiles = global . brunoSkippedFiles || [ ] ;
6177 global . brunoSkippedFiles . push ( { path : filePath , error : err . message } ) ;
6278 }
6379 }
6480 }
65- let currentDirFolderItems = currentDirItems ?. filter ( ( iter ) => iter . type === 'folder' ) ;
66- let sortedFolderItems = sortByNameThenSequence ( currentDirFolderItems ) ;
6781
68- let currentDirRequestItems = currentDirItems ?. filter ( ( iter ) => iter . type !== 'folder' ) ;
69- let sortedRequestItems = currentDirRequestItems ?. sort ( ( a , b ) => a . seq - b . seq ) ;
70-
71- return sortedFolderItems ?. concat ( sortedRequestItems ) ;
82+ const folders = sortByNameThenSequence ( currentDirItems . filter ( ( i ) => i . type === 'folder' ) ) ;
83+ const requests = currentDirItems . filter ( ( i ) => i . type !== 'folder' ) . sort ( ( a , b ) => a . seq - b . seq ) ;
84+ return folders . concat ( requests ) ;
7285 } ;
73- let collectionItems = traverse ( collectionPath ) ;
7486
75- let collection = {
87+ return {
7688 brunoConfig,
89+ format,
7790 root : collectionRoot ,
7891 pathname : collectionPath ,
79- items : collectionItems
92+ items : traverse ( collectionPath )
8093 } ;
81-
82- return collection ;
83- } ;
84-
85- const getCollectionBrunoJsonConfig = ( dir ) => {
86- // right now, bru must be run from the root of the collection
87- // will add support in the future to run it from anywhere inside the collection
88- const brunoJsonPath = path . join ( dir , 'bruno.json' ) ;
89- const brunoJsonExists = fs . existsSync ( brunoJsonPath ) ;
90- if ( ! brunoJsonExists ) {
91- console . error ( chalk . red ( `You can run only at the root of a collection` ) ) ;
92- process . exit ( constants . EXIT_STATUS . ERROR_NOT_IN_COLLECTION ) ;
93- }
94-
95- const brunoConfigFile = fs . readFileSync ( brunoJsonPath , 'utf8' ) ;
96- const brunoConfig = JSON . parse ( brunoConfigFile ) ;
97- return brunoConfig ;
98- } ;
99-
100- const getCollectionRoot = ( dir ) => {
101- const collectionRootPath = path . join ( dir , 'collection.bru' ) ;
102- const exists = fs . existsSync ( collectionRootPath ) ;
103- if ( ! exists ) {
104- return { } ;
105- }
106-
107- const content = fs . readFileSync ( collectionRootPath , 'utf8' ) ;
108- return parseCollection ( content ) ;
109- } ;
110-
111- const getFolderRoot = ( dir ) => {
112- const folderRootPath = path . join ( dir , 'folder.bru' ) ;
113- const exists = fs . existsSync ( folderRootPath ) ;
114- if ( ! exists ) {
115- return null ;
116- }
117-
118- const content = fs . readFileSync ( folderRootPath , 'utf8' ) ;
119- return parseFolder ( content ) ;
12094} ;
12195
12296const mergeHeaders = ( collection , request , requestTreePath ) => {
@@ -612,6 +586,8 @@ const sortByNameThenSequence = (items) => {
612586} ;
613587
614588module . exports = {
589+ FORMAT_CONFIG ,
590+ getCollectionFormat,
615591 createCollectionJsonFromPathname,
616592 mergeHeaders,
617593 mergeVars,
0 commit comments