Skip to content

Commit d82ce61

Browse files
ENGG-4178: Add support to read selected folder contents and preview changes (#202)
1 parent ed7abdc commit d82ce61

2 files changed

Lines changed: 122 additions & 17 deletions

File tree

src/main/events.js

Lines changed: 115 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "path";
22
import { unescape } from "querystring";
33
import fs from "fs";
4+
import os from "os";
45
import { app, dialog, ipcMain } from "electron";
56
/** ACTIONS */
67
import startBackgroundProcess from "./actions/startBackgroundProcess";
@@ -35,6 +36,13 @@ const getFileCategory = (fileExtension) => {
3536
}
3637
};
3738

39+
const createFsResourceItem = (name, fsResourcePath, type) => ({
40+
name,
41+
path: fsResourcePath,
42+
type,
43+
...(type === "directory" && { contents: [] }),
44+
});
45+
3846
export async function trackRecentlyAccessedFile(filePath) {
3947
const fileExtension = path.extname(filePath);
4048

@@ -263,19 +271,21 @@ export const registerMainProcessEventsForWebAppWindow = (webAppWindow) => {
263271
export const registerMainProcessCommonEvents = () => {
264272
ipcMain.handle("open-file-dialog", async (event, options) => {
265273
const fileDialogPromise = dialog.showOpenDialog(options ?? {});
266-
return fileDialogPromise.then(result => {
267-
const { canceled, filePaths } = result;
268-
if (canceled || !filePaths?.length) {
269-
return {canceled: true, files: []};
270-
}
271-
const files = []
272-
for (const filePath of filePaths) {
273-
const { size } = fs.statSync(filePath);
274-
const name = path.basename(filePath);
275-
files.push({ path: filePath, name, size });
276-
}
277-
return { canceled, files };
278-
}).catch(console.error)
274+
return fileDialogPromise
275+
.then((result) => {
276+
const { canceled, filePaths } = result;
277+
if (canceled || !filePaths?.length) {
278+
return { canceled: true, files: [] };
279+
}
280+
const files = [];
281+
for (const filePath of filePaths) {
282+
const { size } = fs.statSync(filePath);
283+
const name = path.basename(filePath);
284+
files.push({ path: filePath, name, size });
285+
}
286+
return { canceled, files };
287+
})
288+
.catch(console.error);
279289
});
280290

281291
ipcMain.handle("open-folder-dialog", async (event, options = {}) => {
@@ -286,4 +296,96 @@ export const registerMainProcessCommonEvents = () => {
286296
const folderDialogPromise = await dialog.showOpenDialog(dialogOptions);
287297
return folderDialogPromise;
288298
});
299+
300+
ipcMain.handle(
301+
"get-workspace-folder-preview",
302+
async (event, payload = {}) => {
303+
try {
304+
let { folderPath } = payload;
305+
306+
if (!folderPath) {
307+
const homeDir = os.homedir();
308+
const documentsPath = path.join(homeDir, "Documents");
309+
310+
if (
311+
fs.existsSync(documentsPath) &&
312+
fs.statSync(documentsPath).isDirectory()
313+
) {
314+
folderPath = documentsPath;
315+
} else {
316+
folderPath = homeDir;
317+
}
318+
}
319+
320+
// Checking if workspace folder exists
321+
const folderExists = fs.existsSync(folderPath);
322+
const existingContents = [];
323+
324+
if (folderExists) {
325+
// Check if it's a directory
326+
const stats = fs.statSync(folderPath);
327+
if (!stats.isDirectory()) {
328+
throw new Error("Folder path is not a directory");
329+
}
330+
331+
// Read existing directory contents at root level of workspace
332+
const items = fs.readdirSync(folderPath);
333+
334+
for (const item of items) {
335+
const itemPath = path.join(folderPath, item);
336+
const itemStats = fs.statSync(itemPath);
337+
338+
existingContents.push(
339+
createFsResourceItem(
340+
item,
341+
itemPath,
342+
itemStats.isDirectory() ? "directory" : "file"
343+
)
344+
);
345+
}
346+
347+
existingContents.sort((a, b) => {
348+
const aIsDir = a.type === "directory";
349+
const bIsDir = b.type === "directory";
350+
if (aIsDir && !bIsDir) return -1;
351+
if (!aIsDir && bIsDir) return 1;
352+
return a.name.localeCompare(b.name);
353+
});
354+
}
355+
356+
const newAdditions = {
357+
name: "Workspace folder",
358+
path: folderPath,
359+
type: "directory",
360+
contents: [
361+
{
362+
name: "environments",
363+
path: path.join(folderPath, "environments"),
364+
type: "directory",
365+
contents: [],
366+
},
367+
{
368+
name: "requestly.json",
369+
path: path.join(folderPath, "requestly.json"),
370+
type: "file",
371+
},
372+
],
373+
};
374+
375+
return {
376+
success: true,
377+
folderPath,
378+
existingContents,
379+
newAdditions,
380+
};
381+
} catch (error) {
382+
console.error("Error getting folder preview:", error);
383+
return {
384+
success: false,
385+
error: error.message,
386+
folderPath: payload.folderPath,
387+
};
388+
}
389+
}
390+
);
289391
};

src/renderer/actions/local-sync/fs-utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,12 @@ export async function createWorkspaceFolder(
473473
workspacePath: string
474474
): Promise<FileSystemResult<{ name: string; id: string; path: string }>> {
475475
const sanitizedWorkspacePath = sanitizePath(workspacePath);
476+
477+
const workspaceFolderPath = appendPath(sanitizedWorkspacePath, name);
476478
const folderCreationResult = await createFolder(
477479
createFsResource({
478480
rootPath: sanitizedWorkspacePath,
479-
path: sanitizedWorkspacePath,
481+
path: workspaceFolderPath,
480482
type: "folder",
481483
}),
482484
{
@@ -487,10 +489,11 @@ export async function createWorkspaceFolder(
487489
if (folderCreationResult.type === "error") {
488490
return folderCreationResult;
489491
}
492+
490493
const configFileCreationResult = await writeContentRaw(
491494
createFsResource({
492-
rootPath: sanitizedWorkspacePath,
493-
path: appendPath(sanitizedWorkspacePath, "requestly.json"),
495+
rootPath: workspaceFolderPath,
496+
path: appendPath(workspaceFolderPath, "requestly.json"),
494497
type: "file",
495498
}),
496499
{
@@ -506,7 +509,7 @@ export async function createWorkspaceFolder(
506509

507510
return addWorkspaceToGlobalConfig({
508511
name,
509-
path: sanitizedWorkspacePath,
512+
path: workspaceFolderPath,
510513
});
511514
}
512515

0 commit comments

Comments
 (0)