Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
290 changes: 146 additions & 144 deletions packages/bruno-electron/src/app/collection-watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const {
isWSLPath,
normalizeAndResolvePath,
sizeInMB,
getCollectionFormat
getCollectionFormat,
clearCollectionFormatCache
} = require('../utils/filesystem');
const {
parseEnvironment,
Expand Down Expand Up @@ -48,12 +49,18 @@ const isEnvironmentsFolder = (pathname, collectionPath) => {

const isFolderRootFile = (pathname, collectionPath) => {
const basename = path.basename(pathname);
const format = getCollectionFormat(collectionPath);

if (format === 'yml') {
return basename === 'folder.yml';
} else if (format === 'bru') {
return basename === 'folder.bru';
try {
const format = getCollectionFormat(collectionPath);

if (format === 'yml') {
return basename === 'folder.yml';
} else if (format === 'bru') {
return basename === 'folder.bru';
}
} catch (err) {
// Collection config file may have been deleted, can't determine format
return false;
}

return false;
Expand Down Expand Up @@ -224,17 +231,17 @@ const add = async (win, pathname, collectionUid, collectionPath, useWorkerThread
}

if (isCollectionRootFile(pathname, collectionPath)) {
const format = getCollectionFormat(collectionPath);
const file = {
meta: {
collectionUid,
pathname,
name: path.basename(pathname),
collectionRoot: true
}
};

try {
const format = getCollectionFormat(collectionPath);
const file = {
meta: {
collectionUid,
pathname,
name: path.basename(pathname),
collectionRoot: true
}
};

let content = fs.readFileSync(pathname, 'utf8');
let parsed = await parseCollection(content, { format });

Expand Down Expand Up @@ -297,127 +304,135 @@ const add = async (win, pathname, collectionUid, collectionPath, useWorkerThread
}
}

const format = getCollectionFormat(collectionPath);
if (hasRequestExtension(pathname, format)) {
watcher.addFileToProcessing(collectionUid, pathname);
try {
const format = getCollectionFormat(collectionPath);
if (hasRequestExtension(pathname, format)) {
watcher.addFileToProcessing(collectionUid, pathname);

const file = {
meta: {
collectionUid,
pathname,
name: path.basename(pathname)
}
};
const file = {
meta: {
collectionUid,
pathname,
name: path.basename(pathname)
}
};

const fileStats = fs.statSync(pathname);
let content = fs.readFileSync(pathname, 'utf8');
const fileStats = fs.statSync(pathname);
let content = fs.readFileSync(pathname, 'utf8');

// If worker thread is not used, we can directly parse the file
if (!useWorkerThread) {
try {
file.data = await parseRequest(content, { format });
file.partial = false;
file.loading = false;
file.size = sizeInMB(fileStats?.size);
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
} catch (error) {
console.error(error);
} finally {
watcher.markFileAsProcessed(win, collectionUid, pathname);
// If worker thread is not used, we can directly parse the file
if (!useWorkerThread) {
try {
file.data = await parseRequest(content, { format });
file.partial = false;
file.loading = false;
file.size = sizeInMB(fileStats?.size);
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
} catch (error) {
console.error(error);
} finally {
watcher.markFileAsProcessed(win, collectionUid, pathname);
}
return;
}
return;
}

try {
// we need to send a partial file info to the UI
// so that the UI can display the file in the collection tree
file.data = {
name: path.basename(pathname),
type: 'http-request'
};

const metaJson = parseFileMeta(content, format);
file.data = metaJson;
file.partial = true;
file.loading = false;
file.size = sizeInMB(fileStats?.size);
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
try {
// we need to send a partial file info to the UI
// so that the UI can display the file in the collection tree
file.data = {
name: path.basename(pathname),
type: 'http-request'
};

if (fileStats.size < MAX_FILE_SIZE) {
// This is to update the loading indicator in the UI
const metaJson = parseFileMeta(content, format);
file.data = metaJson;
file.partial = false;
file.loading = true;
file.partial = true;
file.loading = false;
file.size = sizeInMB(fileStats?.size);
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);

// This is to update the file info in the UI
file.data = await parseRequestViaWorker(content, {
format,
filename: pathname
});
file.partial = false;
if (fileStats.size < MAX_FILE_SIZE) {
// This is to update the loading indicator in the UI
file.data = metaJson;
file.partial = false;
file.loading = true;
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);

// This is to update the file info in the UI
file.data = await parseRequestViaWorker(content, {
format,
filename: pathname
});
file.partial = false;
file.loading = false;
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
}
} catch (error) {
file.data = {
name: path.basename(pathname),
type: 'http-request'
};
file.error = {
message: error?.message
};
file.partial = true;
file.loading = false;
file.size = sizeInMB(fileStats?.size);
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
} finally {
watcher.markFileAsProcessed(win, collectionUid, pathname);
}
} catch (error) {
file.data = {
name: path.basename(pathname),
type: 'http-request'
};
file.error = {
message: error?.message
};
file.partial = true;
file.loading = false;
file.size = sizeInMB(fileStats?.size);
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'addFile', file);
} finally {
watcher.markFileAsProcessed(win, collectionUid, pathname);
}
} catch (err) {
console.error(`Error in add handler for ${pathname}:`, err);
}
};

const addDirectory = async (win, pathname, collectionUid, collectionPath) => {
const envDirectory = path.join(collectionPath, 'environments');
try {
const envDirectory = path.join(collectionPath, 'environments');

if (path.normalize(pathname) === path.normalize(envDirectory)) {
return;
}
if (path.normalize(pathname) === path.normalize(envDirectory)) {
return;
}

let name = path.basename(pathname);
let seq;
let name = path.basename(pathname);
let seq;

const format = getCollectionFormat(collectionPath);
const folderFilePath = path.join(pathname, `folder.${format}`);
const format = getCollectionFormat(collectionPath);
const folderFilePath = path.join(pathname, `folder.${format}`);

try {
if (fs.existsSync(folderFilePath)) {
let folderFileContent = fs.readFileSync(folderFilePath, 'utf8');
let folderData = await parseFolder(folderFileContent, { format });
name = folderData?.meta?.name || name;
seq = folderData?.meta?.seq;
try {
if (fs.existsSync(folderFilePath)) {
let folderFileContent = fs.readFileSync(folderFilePath, 'utf8');
let folderData = await parseFolder(folderFileContent, { format });
name = folderData?.meta?.name || name;
seq = folderData?.meta?.seq;
}
} catch (error) {
console.error(`Error occured while parsing folder.${format} file`);
console.error(error);
}
} catch (error) {
console.error(`Error occured while parsing folder.${format} file`);
console.error(error);
}

const directory = {
meta: {
collectionUid,
pathname,
name,
seq,
uid: getRequestUid(pathname)
}
};
const directory = {
meta: {
collectionUid,
pathname,
name,
seq,
uid: getRequestUid(pathname)
}
};

win.webContents.send('main:collection-tree-updated', 'addDir', directory);
win.webContents.send('main:collection-tree-updated', 'addDir', directory);
} catch (error) {
console.error(`Error in addDirectory handler for ${pathname}:`, error);
}
};

const change = async (win, pathname, collectionUid, collectionPath) => {
Expand Down Expand Up @@ -522,9 +537,9 @@ const change = async (win, pathname, collectionUid, collectionPath) => {
}
}

const format = getCollectionFormat(collectionPath);
if (hasRequestExtension(pathname, format)) {
try {
try {
const format = getCollectionFormat(collectionPath);
if (hasRequestExtension(pathname, format)) {
const file = {
meta: {
collectionUid,
Expand All @@ -545,30 +560,22 @@ const change = async (win, pathname, collectionUid, collectionPath) => {
file.size = sizeInMB(fileStats?.size);
hydrateRequestWithUuid(file.data, pathname);
win.webContents.send('main:collection-tree-updated', 'change', file);
} catch (err) {
console.error(err);
}
} catch (err) {
console.error(err);
}
};

const unlink = (win, pathname, collectionUid, collectionPath) => {
try {
if (!fs.existsSync(collectionPath)) {
return;
}
console.log(`watcher unlink: ${pathname}`);
console.log(`watcher unlink: ${pathname}`);

try {
if (isEnvironmentsFolder(pathname, collectionPath)) {
return unlinkEnvironmentFile(win, pathname, collectionUid);
}
console.log(`watcher unlink: ${pathname}`);

let format;
try {
format = getCollectionFormat(collectionPath);
} catch (error) {
console.error(`Error getting collection format for: ${collectionPath}`, error);
return;
}
const format = getCollectionFormat(collectionPath);
if (hasRequestExtension(pathname, format)) {
const basename = path.basename(pathname);
const dirname = path.dirname(pathname);
Expand All @@ -586,32 +593,22 @@ const unlink = (win, pathname, collectionUid, collectionPath) => {
};
win.webContents.send('main:collection-tree-updated', 'unlink', file);
}
} catch (err) {
console.error(`Error processing unlink event for: ${pathname}`, err);
} catch (error) {
console.error(`Error in unlink handler for ${pathname}:`, error);
}
};

const unlinkDir = async (win, pathname, collectionUid, collectionPath) => {
try {
if (!fs.existsSync(collectionPath)) {
return;
}
const envDirectory = path.join(collectionPath, 'environments');

if (path.normalize(pathname) === path.normalize(envDirectory)) {
return;
}

let format;
try {
format = getCollectionFormat(collectionPath);
} catch (error) {
console.error(`Error getting collection format for: ${collectionPath}`, error);
return;
}
const folderFilePath = path.join(pathname, `folder.${format}`);

let name = path.basename(pathname);
const format = getCollectionFormat(collectionPath);
const folderFilePath = path.join(pathname, `folder.${format}`);

if (fs.existsSync(folderFilePath)) {
let folderFileContent = fs.readFileSync(folderFilePath, 'utf8');
Expand All @@ -627,8 +624,13 @@ const unlinkDir = async (win, pathname, collectionUid, collectionPath) => {
}
};
win.webContents.send('main:collection-tree-updated', 'unlinkDir', directory);
} catch (err) {
console.error(`Error processing unlinkDir event for: ${pathname}`, err);

// Clear format cache when collection root is deleted
if (path.normalize(pathname) === path.normalize(collectionPath)) {
clearCollectionFormatCache(collectionPath);
}
} catch (error) {
console.error(`Error in unlinkDir handler for ${pathname}:`, error);
}
};

Expand Down
Loading
Loading